Forum Moderators: coopster

Message Too Old, No Replies

Delete Specific Entry in Flatfile

         

clown

2:30 pm on Apr 6, 2007 (gmt 0)

10+ Year Member



Hi again!

I'm working on a CPanel on my website that gives me the posibility to edit both the blog, news and shoutbox. So far I'm done with adding new: news, shout & blog. But I'm stuck when it comes to deleting. Like if someone is spamming my shoutbox I want to be able to log in and delete the spam shout.

I wish I had some code to show you for the delete part, but I dont even know where to start..

Anyone have any good solutions for this or some guidelines?

Regards,
Clown

clown

4:24 pm on Apr 6, 2007 (gmt 0)

10+ Year Member



nothing? :(

eelixduppy

4:27 pm on Apr 6, 2007 (gmt 0)



How are you storing the information? Database? Flatfile?

clown

5:05 pm on Apr 6, 2007 (gmt 0)

10+ Year Member



i'm storing the info in a textfile.. is that the same as a flatfile?

joelgreen

9:52 pm on Apr 6, 2007 (gmt 0)

10+ Year Member



You cannot simply delete a line from text file. You have to read whole file into memory, delete line, and save whole file to disk.

something like

$text = file('textfile');

find the line to delete, unset() it

$f = fopen('textfile','w');

fputs($f, join('',$text));

fclose($f);

clown

3:32 am on Apr 7, 2007 (gmt 0)

10+ Year Member



ok.. i now have a file that show's sorts out each news, so I get them up with a "Delete" link next to them:


<?php

$file = "store/news.txt";
$news = file($file);

foreach ($news as $key => $value) {
$key = $key+1;
echo "<table border=0 cellpadding=0 cellspacing=1 bgcolor=" . $td[0] . "><tr><td bgcolor=" . $td[3] . ">" . $value . "</td><td bgcolor=" . $td[3] . " align=center><a href=delete.php?type=news&id=" . $key . ">Delete</a></td></tr></table>";
}

?>

and I'm now working on the delete.php file.. What I've got so far is:

<?php

$file = "store/news.txt";
$selLine = $_GET['id'];
$news = file($file);

?>

So what I wonder now is. On the delete link I have numbers for what line each news are on in the news.txt file. But in the delete.php, how do I pull out that single line and just overwrite it with a empty space?

Do I have to add an foreach and when I come to the selected number I just skip that one, and at the end I write the everything back to the news.txt file?

clown

4:28 am on Apr 7, 2007 (gmt 0)

10+ Year Member



Well, after some trying and failing i got it to work:

This is the code I used:
I fixed it with the following code:
---
<?php

$selLine = $_GET['id'];
$delType = $_GET['type'];
$file = "store/" . $delType . ".txt";
$news = file($file);
$cnt = 0;

foreach ($news as $key => $line) {
if ($key!= $selLine) { $result[] = $line; }
$cnt+1;
}
if ($key!= 0) {
$fh = fopen($file, "w");
foreach ($result as $nyhet) {
fwrite($fh, $nyhet);
}
fclose($fh);
}
elseif ($key == 0) {
$fh = fopen($file, "w");
fwrite($fh, "");
fclose($fh);
}

?>
---