Current location: Hot Scripts Forums » Programming Languages » PHP » PHP - Unable to remove items from a file


PHP - Unable to remove items from a file

Reply
  #1 (permalink)  
Old 03-14-06, 09:10 AM
AdrianLewis AdrianLewis is offline
Newbie Coder
 
Join Date: Mar 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Question PHP - Unable to remove items from a file

Hi All,

I am at my wits end with some code for a website I am working on. The page in question is simply for a user to add items to a list of data. The data is stored in a temp file and later added to a database. I have successfull set the page to add items to the list of data. My issue is with trying to remove a line of data from the file. The following snippet of code runs if a link has been clicked on the line of data to be removed which reloads the page running this code which is intended to remove that line...

PHP Code:

if ( isset($RemoveLine) ) {

    
//file() opens a file and puts the content in an array 
    //each element of the array is a line 
    
$fileText file($filename);
    
$CheckLine 0;
    
//open the file again 
    
$f fopen($filename'w');
    foreach(
$fileText as $line){
        
$Checkline $Checkline 1;
        if (
$CheckLine <> $RemoveLine){
               
fputs($f,$line); //place $line back in file 
               
}
    }
    
fclose($f);

    unset(
$RemoveLine);

Before clicking the link to remove a line my page displays the following detail for example...
Qty Code Description Unit Price (£) Total (£) + V.A.T
2 2 2 2 2 0.33 Remove Item
3 3 3 3 3 0.495 Remove Item
4 4 4 4 4 0.66 Remove Item
However after clicking Remove Item (for this example I clicked Remove Item on the second line) the page reloads with the following...
Qty Code Description Unit Price (£) Total (£) + V.A.T
2 2 2 2 2 0.33 Remove Item
3 3 3 3 3 0.495 Remove Item
4 4 4 4 4 0.66 Remove Item
4 4 4 4 0 Remove Item
I have checked the temp file being used and the data matches that displayed...
2, 2, 2, 2, 2, 17.5
3, 3, 3, 3, 3, 17.5
4, 4, 4, 4, 4, 17.5
4, 4, 4, 4, ,
Annoyingly my remove item link is adding another line instead of removing the chosen line.

Any help with this would be fantastic.

Thanks

Adrian
Reply With Quote
  #2 (permalink)  
Old 03-14-06, 12:47 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Why not use a temp table to store the data? It woul eliminate a whole buch of the issues you're dealing with.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #3 (permalink)  
Old 03-16-06, 07:23 PM
AdrianLewis AdrianLewis is offline
Newbie Coder
 
Join Date: Mar 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for your tip. will consider that as an option, just seemed a simpler option for a number of reasons.

Ta
Reply With Quote
  #4 (permalink)  
Old 03-16-06, 10:55 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
I tried a version of your code and it works.

I could not duplicate your problem (of adding a line at the end of the file), but I suspect that what it is doing is reading all the file into the 1st element of the array. I think the end of lines in the file are not being recognized. To check this I would add the following after the file() statement -
PHP Code:

echo count($fileText)."<br />"// show how many lines it found 

Another possibility is that the file is still open (there is no fclose($f); following the file() statement) when you try to open it for writing. On my system this did not cause a problem. On your system, who knows?

Another problem is in your checkline variable. It changes between CheckLine and Checkline several times. This should have caused an error (it did on my system), but the error level setting would affect if you get an error.

Lastly, there could be a problem with the file permissions. As always, function calls should have error checking and reporting done on the results.

Here is the slightly modified version of your code that I tested with -
PHP Code:

<?php


$filename 
"atext.txt";
$Removeline 7;
$f fopen($filename"r") or die("could not open $filename for read<br />");

$fileText file($filename); 
echo 
count($fileText)."<br />"// show how many lines it found
fclose ($f);

$Checkline 0
$f fopen($filename'w') or die ("could not open $filename for write<br />");

foreach(
$fileText as $line){ 
$Checkline $Checkline 1
    if (
$Checkline <> $Removeline){ 
        
fputs($f,$line); //place $line back in file 
    


fclose($f); 
echo 
"done";
?>
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #5 (permalink)  
Old 03-17-06, 04:34 AM
AdrianLewis AdrianLewis is offline
Newbie Coder
 
Join Date: Mar 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Mab,

Fantastic, schoolboy error really but you spotted it. It was the variable $CheckLine.

As for my code adding that additional line each time. I think it is coming from another part of my page but I'm tracking it down.

Thanks for your help with this and your time. Much appreciated.

Adrian
Reply With Quote
  #6 (permalink)  
Old 03-17-06, 05:14 AM
AdrianLewis AdrianLewis is offline
Newbie Coder
 
Join Date: Mar 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
All sorted. Managed to find my dodgy code that was adding another line.

Just wanted to thank you again, been stuck with this for ages. Needed another persons eye to spot the terrible coding.

:¬)

Thanks

Adrian
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Write to php file with php script or create new file, it is possible? Oskare100 PHP 10 08-14-08 09:03 AM
create php file on the fly and delete when session ends recedo PHP 0 01-06-06 10:28 PM
PHP multi-dimensional array sorting issue aqw PHP 2 06-24-05 11:09 PM
Need Epinions-lite system in PHP & MYSQL wali001 Job Offers & Assistance 4 01-12-04 06:02 AM
Upload file to table so ONLY files tied to primary key are displayed in record? grafixDummy PHP 4 12-20-03 04:28 PM


All times are GMT -5. The time now is 06:38 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.