Forum Moderators: coopster

Message Too Old, No Replies

Delete row from flat db

         

Alexphp

8:56 am on Aug 22, 2009 (gmt 0)

10+ Year Member



Hi everybody! This is my first post here and I want to salute you all and thank you for the help.
First of all, I am new to php. Second, i have to use php 4.1.2 because my ISP doesn't want to upgrade. There is nothing I can do about...

Anyway, i need to delete unwanted messages from my guestbook, (flat db) and i need some help. I want to use the row number to identify the row to delete but i don't understand how to delete the row and save the file.

Here is the code:

<?
//####################################################### resolve compatibility between php4 and php5
function get_file_contents($filename)
/* Returns the contents of file name passed
*/
{
if (!function_exists('file_get_contents'))
{
$fhandle = fopen($filename, "r");
$fcontents = fread($fhandle, filesize($filename));
fclose($fhandle);
}
else
{
$fcontents = file_get_contents($filename);
}
return $fcontents;
}
//##########################
// my code...
$action = 0;
$action = $_GET["action"];
$my_rownumber = $_GET["rownumber"];
if ($action == 1)
{
$file = 'guestbook.txt';
$lines = explode("\n", get_file_contents($file));
foreach($lines as $num => $val)
{
if ( $my_rownumber == $num )
{

// what i have to do here to delete the $val of the row and save the file?

}
}
}

// future edit entries code...
//if ($action == 2)
//{
//}

// view flat db content without html tags and add links to delete and edit
$file = 'guestbook.txt';
$lines = explode("\n", get_file_contents($file));
foreach($lines as $num => $val)
{
$val = strip_tags($val, '<a><b><br>');
echo '<a href="deleteboardentry.php?rownumber='.$num.'&action=1">DELETE ENTRY n.'.$num.'</a> - <a href="editboardentry.php?rownumber='.$num.'&action=2">EDIT ENTRY n.'.$num.'</a><br>'. $val;
}
?>

Thanks

[edited by: eelixduppy at 9:43 pm (utc) on Aug. 22, 2009]

rocknbil

4:47 pm on Aug 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard Alexphp,

what i have to do here to delete the $val of the row and save the file?

Well what **I** would do is compile the file into a new variable, then use that to overwrite the old file. Only add to "new" if it does not match the delete id. You may store all lines in a backup variable for safety. Something like


foreach($lines as $num => $val) {
// if it's not the delete line, compile new
if ($my_rownumber != $num) {
$new_db .= "$num\n$val\n";
}
$backup .= "$num\n$val\n";
}

Then write out $backup to the backup file, $new_db to the original $file, overwriting it.