Log in

View Full Version : Line deleting using Array_Diff or str_replace();?



Fsoft
05-20-2008, 08:20 PM
HI,

I am new here. I need help> I am stuck in my project

My work, I mean I am trying to do is that first show all the contents of my text file. With each having a Text [Delete!] and after, once any one of them is clicked, It will delete that line from the text file database and imploded(); the lines again and write back to database. Now the code I am using is this ;



<?php

$db = "commentsdb.txt";


if($_GET["delete"])
{
$data = file($db);

foreach($data as $entry)
{
$data_remove = array_diff($entry , array($data));
}
implode("",$data_remove);
$handle = fopen($db,'w');
fwrite($handle,$data_remove);
fclose($handle);

}else

$data = file($db);

foreach($data as $entry)
{
echo $entry . "<a href='moderation.php?delete=" . $entry . "'>[Delete!]</a>";
}
?>


All goes Ok, It shows as I want. But the problem is once I hit a delete on next page I get these errors




Warning: array_diff() [function.array-diff]: Argument #1 is not an array in C:\server\htdocs\moderation.php on line 12

Warning: array_diff() [function.array-diff]: Argument #1 is not an array in C:\server\htdocs\moderation.php on line 12

Warning: array_diff() [function.array-diff]: Argument #1 is not an array in C:\server\htdocs\moderation.php on line 12

Warning: implode() [function.implode]: Invalid arguments passed in C:\server\htdocs\moderation.php on line 14


Thanks i Badly need help

Thanks a lot :)

edit,
I just saw a thread saying that this work can done with str_replace(); and it's more easier So I don't mind. If the array_diff(); Don't work :) I can easily work with str_replace(); can any one suggest me how can I use that function according to my script needs :) Thanks a lot :)

FAISAL!

Fsoft
05-21-2008, 01:09 PM
Hi thanks :) A new update, I used this code, str_replace(); in my code, Once I hit that, it deletes all the database contents, All the file contents and file becomes Blank :( Please help me> I just only want to delete the file / the line that is clicked to be deleted :)

Thanks here is the code,



<?php

$db = "commentsdb.txt";


if($_GET["delete"])
{
$data = file($db);

foreach($data as $entry)
{
# $data_remove = array_diff($data, array($entry));
$data_remove = str_replace($data,"",$entry);
#$data_remove = explode($data,"",$entry);
}


$handle = fopen($db,'w');
fwrite($handle, $data_remove);
fclose($handle);


}else

$data = file($db);

foreach($data as $entry)
{
echo $entry . "<a href='moderation.php?delete=" . $entry . "'>[Delete!]</a>";
}
?>



Looking forward for help :)

FAISAL!