SitePoint Sponsor |
|
User Tag List
Results 1 to 16 of 16
Thread: multiple line deletion
-
Sep 3, 2007, 04:09 #1
multiple line deletion
Hello, i've got a simple problem i can't figure out. I've got a text file database, i need to delete lines with a certain detail.
My file looks like this:
Code:<?php die(); ?> a|1|September 3rd 07|1|1 a|2|September 3rd 07|2|1 a|2|September 3rd 07|2|1 a|3|September 3rd 07|3|1
Code:$filearray = file($commentfile); unset($filearray[0]); foreach($filearray as $lnum => $thisline){ $break = "|"; list($name_comm, $comment, $date, $id, $status)=explode($break,$thisline); if($_GET['delete'] == $id){ unset($filearray[$lnum]); } } $fd = fopen ($commentfile, "w"); fwrite ($fd, "<?php die(); ?>\n"); $all = count($filearray); for($i = 1; $i < $all; $i++) { fwrite ($fd, $filearray[$i]); } fclose($fd);
Thanks
-
Sep 3, 2007, 04:36 #2
- Join Date
- Aug 2007
- Location
- GR
- Posts
- 352
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I would read each line of the file and put it into an array. Then loop the array and whatever doesn't match put it in another array, then write to file.
-
Sep 3, 2007, 04:39 #3
Thought it'd be that simple. Thanks
-
Sep 3, 2007, 04:44 #4
- Join Date
- Oct 2004
- Location
- Birtley, UK
- Posts
- 2,439
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Is there a reason for using a Text based DB? Would it not be better to use XML ?
If you must use text based, then i'd load each line into an array, and then write the file back but use an if to only write back if it doesn't match the id.
ie..
Code:<?php $lines = array(); // this would contain all the lines from the file. foreach($lines as $line) { $lineData = explode('|', $line); if(lineData[0] != $_GET['delete']) fwrite($fd, $line); } fclose($fd); ?>
-
Sep 3, 2007, 04:48 #5
I'll try it.
I don't have/ need an SQL database.
-
Sep 3, 2007, 04:52 #6
- Join Date
- Oct 2004
- Location
- Birtley, UK
- Posts
- 2,439
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Your script removes from teh array, the one I posted does not. It simply goes through the array and writes to the file but ignores any lines that match your criteria
-
Sep 3, 2007, 04:57 #7
It still has the same problem, although it seems impossible. I must have an error somewhere else.
-
Sep 3, 2007, 05:07 #8
- Join Date
- Oct 2004
- Location
- Birtley, UK
- Posts
- 2,439
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
There must be an error with your data then. As long as your data is seperated by new lines, and each line is then in turn seperated by pipes (|) then the script will work.
-
Sep 3, 2007, 05:11 #9
- Join Date
- Oct 2002
- Location
- Scotland
- Posts
- 3,631
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:$filearray = file($commentfile);
echo "<pre>";
print_r ($filearray);
echo "</pre>";
-
Sep 3, 2007, 05:27 #10
I think i've found the problem. $_GET['delete'] wasn't the id, it was the line number.
-
Sep 3, 2007, 09:18 #11
Text File DBs are so inefficient. Not to mention old very very old.
No wonder man invent better DB system then plain old text files.
-
Sep 3, 2007, 09:20 #12
Sorry but i'm not the type of person who buys things because they're trendy
I'm not paying 1/3 more for my hosting until i *actually* need a database. I'm handling 600+ members, 2500 links and 40 tutorials very well for now.
-
Sep 3, 2007, 09:28 #13
What? buy what things? MySQL is free, SQLite is free, XML is free. What do you mean by "buy" you are not buying a thing. They are trendy for a reason they work really really damn well.
You need to get your facts straight. If they were trendy they would not be around for as long as they are. So they are not trendy they are here until someone finds something better. And plain text is not that.
Oh also Orcal has afree developer edition.
Microsoft has a free version of SQL Server 2005.
There are so many free Relational database systems you have nothing to buy.
-
Sep 3, 2007, 09:30 #14
Not everyone on this planet has a server. I have to rent space on someone elses. This means paying more for features.
-
Sep 3, 2007, 09:34 #15
If your server does not come with PHP and MySQL you are being ripped off.
The cheapest web host on the plant package PHP and MySQL together. If your host doesn't give that to you find a new host because seriously I've not seen a hosting package that doesn't have those two item in it.
I was paying less then $80 bucks a year and I got that I could probably pay less and still get it.
-
Sep 3, 2007, 09:42 #16
Well i'm paying about $40 (£20). This is english, i'm sure i can pay dirt for a host in america. I can upgrade to a package with mysql for £30 ($60) but as i said, i DO NOT NEED IT (please read twice).
Bookmarks