read-delete lines from a text file

Reply

Join Date: Jan 2008
Posts: 4
Reputation: wrstrong is an unknown quantity at this point 
Solved Threads: 0
wrstrong wrstrong is offline Offline
Newbie Poster

read-delete lines from a text file

 
0
  #1
Jan 2nd, 2008
Hello everyone!

I'm have a text file's contents, its output in the browser and a php file (which I'm including below.) What I need to do is search for a specific line in the text file and delete it. The problem is that the end result will have a text file with user's comments and a page will be generated that will show all comments, a check box for marking any particular comment for deletion and when the submit box will be pressed, the entry will be deleted (the entire line of the file.)

Here's what I have so far...

php file to read the comments.txt
  1. <?
  2. // get comments file
  3. $filename = 'comments.txt';
  4. $fp = fopen( $filename, 'r' );
  5. $file_contents = @fread( $fp, filesize( $filename ) );
  6. fclose( $fp );
  7.  
  8. // print comments file's file name (not going to be used in the end product)
  9. print ("filename<br>");
  10. print $filename;
  11. print ("<br><hr><br>");
  12. // demo to show how the file is visible
  13. print ("show file<br>");
  14. include 'comments.txt';
  15.  
  16. // the problem comes when you look at the actual file and its formatting compared to the output
  17. ?>

The comments.txt file itself (it's ONLY for testing, not the actual end result file)

1|<b>this is a test</b>|<br><i>12.31.07 1:45 pm</i>|<br>comment #1<br><br>
1|<b>this is another test</b>|<br><i>12.31.07 2:14 pm</i>|<br>comment #2<br><br>
1|<b>this is yet another test</b>|<br><i>12.31.07 2:14 pm</i>|<br>comment #3<br><br>
1|<b>this is and yet another test</b>|<br><i>12.31.07 1:45 pm</i>|<br>comment #4<br><br>
1|<b>this is still another test</b>|<br><i>12.31.07 2:14 pm</i>|<br>comment #5<br><br>
1|<b>this is the last test</b>|<br><i>12.31.07 2:14 pm</i>|<br>comment #6<br><br>

The output on the browser
filename
comments.txt

--------------------------------------------------------------------------------

show file
1|this is a test|
12.31.07 1:45 pm|
comment #1

1|this is another test|
12.31.07 2:14 pm|
comment #2

1|this is yet another test|
12.31.07 2:14 pm|
comment #3

1|this is and yet another test|
12.31.07 1:45 pm|
comment #4

1|this is still another test|
12.31.07 2:14 pm|
comment #5

1|this is the last test|
12.31.07 2:14 pm|
comment #6

The output is in the following format:

1|this is a test| ( and ones following )
Date time|
comment #x ( actual comments )

I figured out how to separate the actual file into the component parts with the following script:
  1. <?
  2. $filename = 'comments.txt';
  3. $fp = fopen( $filename, 'r' );
  4. $file_contents = @fread( $fp, filesize( $filename ) );
  5. fclose( $fp );
  6. $lines = explode("\n", $file_contents);
  7. $numlines = count($lines);
  8. for ($i = 0; $i < $numlines; $i++) {
  9. $comment = explode("|", $lines[$i]);
  10. print "This is the comment title:";
  11. print $comment[1];
  12. print "<br>";
  13. print "This is the comment date - time:";
  14. print $comment[2];
  15. print "<br>";
  16. print "This is the comment text:";
  17. print $comment[3];
  18. print "<br>";
  19. }
  20. ?>


As it is unknown what the actual comments (date/time/actual comment) will be, how do I search & delete something that I do not know what the contents are? I guess what I need to do now is to search for the matching article, delete the entire line & then rewrite the file. I can search (as noted in above code snippets); however, how do I take that info, find the appropriate line, delete the entire line and then rewrite the file?? Where do I go from here??? Any help would be GREATLY appreciated. Thank you in advance!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 30
Reputation: chrelad is an unknown quantity at this point 
Solved Threads: 5
chrelad's Avatar
chrelad chrelad is offline Offline
Light Poster

Re: read-delete lines from a text file

 
0
  #2
Jan 2nd, 2008
Hi wrstrong,

Looks like someone else is having the same problem you are... This is the thread that was produced for review:

http://www.webmasterworld.com/php/3304270.htm

And here is the final result PHP that was used to manipulate the flat file database:

  1. <?php
  2.  
  3. $selLine = $_GET['id'];
  4. $delType = $_GET['type'];
  5. $file = "store/" . $delType . ".txt";
  6. $news = file($file);
  7. $cnt = 0;
  8.  
  9. foreach ($news as $key => $line) {
  10. if ($key!= $selLine) { $result[] = $line; }
  11. $cnt+1;
  12. }
  13. if ($key!= 0) {
  14. $fh = fopen($file, "w");
  15. foreach ($result as $nyhet) {
  16. fwrite($fh, $nyhet);
  17. }
  18. fclose($fh);
  19. }
  20. elseif ($key == 0) {
  21. $fh = fopen($file, "w");
  22. fwrite($fh, "");
  23. fclose($fh);
  24. }
  25.  
  26. ?>

Nice solution as far as I can tell, hope it helps you out.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,739
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: read-delete lines from a text file

 
0
  #3
Jan 2nd, 2008
Someone had asked the same question here.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 4
Reputation: wrstrong is an unknown quantity at this point 
Solved Threads: 0
wrstrong wrstrong is offline Offline
Newbie Poster

Re: read-delete lines from a text file

 
0
  #4
Jan 6th, 2008
Thanks for the quick reply! Although it might seem like it's that simple, I must be really dense tonight (or overly tired). LOL Here's where I am so far...

As I already know how to put the text into the comments.txt, I'm not going to include it here.

comments.txt
  1. Here is the first title!~12.31.2007~2:14 pm~Here is the first entry!
  2. Here is the second title!~12.31.2007~2:14 pm~Here is the second entry!
  3. Here is the third title!~12.31.2007~2:14 pm~Here is the third entry!
  4. Here is the fourth title!~12.31.2007~2:14 pm~Here is the fourth entry!
  5. Here is the fifth title!~12.31.2007~2:14 pm~Here is the fifth entry!
  6. Here is the sixth title!~12.31.2007~2:14 pm~Here is the sixth entry!
  7. Here is the seventh title!~12.31.2007~2:14 pm~Here is the seventh entry!
  8. Here is the eighth title!~12.31.2007~2:14 pm~Here is the eighth entry!
  9. Here is the ninth title!~12.31.2007~2:14 pm~Here is the nineth entry!
  10. Here is the tenth title!~12.31.2007~2:14 pm~Here is the tenth entry!

readtest.php (simply reads the file, displays it & provides a link for deletion)
  1. <?
  2. print ("<table border=0>");
  3. $filename = 'comments.txt';
  4. $fp = fopen( $filename, 'r' );
  5. $file_contents = @fread( $fp, filesize( $filename ) );
  6. fclose( $fp );
  7. $lines = explode("\n", $file_contents);
  8. $numlines = count($lines);
  9. for ($i = 0; $i < $numlines-1; $i++) {
  10. $comment = explode("~", $lines[$i]);
  11. $new_entry[$i] = $comment[0] . "<br>" . $comment[1] . "&nbsp;@&nbsp;" . $comment[2] . "<br>" . $comment[3] . "<br>";
  12. print ("<tr>
  13. <td width=60%>$new_entry[$i]</td>
  14. <td>
  15. <p align=center><a href=delete_entry.php?entry=");
  16. print $i+1;
  17. print (">delete</a></td>
  18. </tr>
  19. <tr>
  20. <td colspan=2><hr size=1></td>
  21. </tr>");
  22. }
  23. print ("</table>");
  24. ?>

delete_entry.php (When used as delete_entry.php?entry=3, for example, it should delete the third entry.)
  1. <?
  2. $filename = 'comments.txt';
  3. $fp = fopen( $filename, 'r+' );
  4. $file_contents = @fread( $fp, filesize( $filename ) );
  5.  
  6. $lines = explode("\n", $file_contents);
  7. $numlines = count($lines);
  8. for ($i = 0; $i < $numlines-1; $i++)
  9. {
  10. $comment = explode("~", $lines[$i]);
  11. if (!($comment[$i] == $entry))
  12. {
  13. $together .= $comment[$i];
  14. if (!($i = $numlines))
  15. {
  16. $together .= "~";
  17. }
  18. }
  19. else
  20. {
  21. }
  22. }
  23. fwrite ( $filename , $together );
  24. fclose( $fp );
  25. ?>

So far, the best that I can get as far as output is...
Warning: fwrite(): supplied argument is not a valid stream resource in {path}\delete_entry.php on line 23
{path} is the actual path on the server (not important).

Have I missed something? I need to use a flat file instead of a mysql database or a microsoft office mdb file because it needs to be quickly editable by ANY system at will and the EASIEST solution in that case was to make sure that everyone who needs to edit it will have the tools that they need WITHOUT installing anything or knowing any type of, 'special something' in order to do what they need to do.

I am usually a quick learner; however, I've searched & read everything I can on the subject and I'm stuck at this point.

Thank you for all your help thus far!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,739
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: read-delete lines from a text file

 
0
  #5
Jan 6th, 2008
fwrite ( $filename , $together );
You write using a file pointer and not directly to a file.

$filename = 'comments.txt';
$fp = fopen( $filename, 'w+' );
fwrite($fp,$together);
fclose($fp);
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 4
Reputation: wrstrong is an unknown quantity at this point 
Solved Threads: 0
wrstrong wrstrong is offline Offline
Newbie Poster

Re: read-delete lines from a text file

 
0
  #6
Jan 7th, 2008
What's the difference between::

  1. $filename = 'comments.txt';
  2. $fp = fopen( $filename, 'r+' );
  3.  
  4. :::: Then stuff in between ::::
  5.  
  6. fwrite ( $filename , $together );
  7. fclose( $fp );

and

  1. $filename = 'comments.txt';
  2. $fp = fopen( $filename, 'w+' );
  3. fwrite($fp,$together);
  4. fclose($fp);

Other than the obvious that there is space in the one where it's not in the other and that the r+ says read and write from the beginning of the file & the w+ says read & write from the erasing of the entire file? Why are the differences so critical??
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,739
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 330
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: read-delete lines from a text file

 
0
  #7
Jan 7th, 2008
In your case, you are trying to write the contents of $together directly to a file which wouldn't work. In my case, I am writing it to the file using the file pointer $fp. Using modes r+, w+ depends on how you want to handle the file.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 4
Reputation: wrstrong is an unknown quantity at this point 
Solved Threads: 0
wrstrong wrstrong is offline Offline
Newbie Poster

Re: read-delete lines from a text file

 
0
  #8
Jan 9th, 2008
I decided to use a database instead. I had it to the point where I was able to see & write to the file; however, I was unable to edit or delete lines from the text file.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC