Jump to content

Recommended Posts

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!

 

 

Link to comment
https://forums.phpfreaks.com/topic/50566-flat-file-database/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/50566-flat-file-database/#findComment-248570
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.