Schlo_50 Posted May 8, 2007 Share Posted May 8, 2007 I have a .txt (data.txt) file as my flat file database and have a page (editcal.php) where by i want to display the contents of the .txt file with a link next to each entry that says 'delete'. when a user clicks delete i want that row of the flat file database to be deleted. I so far have the following, but need someone to revise the code for me! Please help! editcal.php: <p class="rotary"> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td><form name="form1" method="post" action=""> <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td align="center" bgcolor="#6091C2" class="rotary"> <?php $filename = "data.txt"; $file = file($filename); foreach ($file as $key => $value) { 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?day=$value&month=$alue&event=$value" . $value . ">Delete</a></td></tr></table>"; } ?></td> </tr> </table> </form> <p class="rotary"> </p> </form></center> <p align="center" class="rotary"> </p> </body> </html> delete.php: <?php $file = file("data.txt"); foreach($file as $key => $val){ $data[$key] = explode("|", $val); if($_GET['dat'] == $data[$key][2] && $_GET['month'] == $data[$key][1] && $_GET['event'] == $data[$key][0]){ unset($file[$key]); $fp = fopen("data.txt", "w"); fwrite($fp, implode('', $file)); fclose($fp); print "Deleted"; } } //to delete link to delete.php?day=DAY&month=MONTH&event=EVENT ?> Thanks in advance guys! Quote Link to comment https://forums.phpfreaks.com/topic/50566-flat-file-database/ Share on other sites More sharing options...
JakeTheSnake3.0 Posted May 8, 2007 Share Posted May 8, 2007 I assume your database txt file entries are delimited by a line return? Whatever the case, put the entires into an array....then search the array for the instance of the string which matches what the user selected to be deleted. Remove that entry from the array.... Then use a foreach loop to concatinate the strings back together again, including the delimiters and fwrite the file completely over again. Quote Link to comment https://forums.phpfreaks.com/topic/50566-flat-file-database/#findComment-248570 Share on other sites More sharing options...
Schlo_50 Posted May 8, 2007 Author Share Posted May 8, 2007 huh? Could someone show me how to do the above please? Quote Link to comment https://forums.phpfreaks.com/topic/50566-flat-file-database/#findComment-248584 Share on other sites More sharing options...
boo_lolly Posted May 8, 2007 Share Posted May 8, 2007 read this entire section: http://www.tizag.com/phpT/files.php and this entire section: http://w3schools.com/php/php_file.asp Quote Link to comment https://forums.phpfreaks.com/topic/50566-flat-file-database/#findComment-248589 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.