Forum Moderators: coopster

Message Too Old, No Replies

Delete a specific line from flat file database

         

sloth9

8:37 pm on Oct 5, 2010 (gmt 0)

10+ Year Member



I'm creating a flat file database and I need to write a delete function to manage the entries. This database contains two columns: name and id. I have the page set up so that when the delete button is pressed it goes to delete.php?id (id is just an integer).

I've read every post i can find about deleting lines in a flat file and I tried to implement the processes I've read about but I can't get it to work. When I run my code it deletes all the content from the flat file. HELP!

Here's what I've got so far:

<?php
$del = $_GET['id'];
$file = "cnamedata.txt";
$cinfo = file($file);

foreach($cinfo as $key => $val) {
$centry[$key] = explode("||", $val);
}

/*$centry is a two dimension array where $centry[0][0] is the name of the enrty on the first line of cnamedata.txt and $centry[0][1] is it's id.*/

$fh = fopen($file, "w");

for($i=0;$i<sizeof($cinfo);$i++){
if($centry[i][1]!=$del){
$result[i] = $cinfo[i]; }

else{$result[i]="";}
}

foreach ($result as $line) {
fwrite($fh, $line);
}

fclose($fh);

# redirect back to cname.php
echo"
<html><script language=\"javascript\">
location.replace(\"cname.php\");
</script></html>";

?>


Any pointers would be greatly appreciated.

Thanks.

Matthew1980

9:11 pm on Oct 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there sloth9,

Welcome to WebmasterWorld, could you post an example of a line from this text file please so that we can better understand the structure of you FTF database?

Personally I don't think that you need to loop if you are only deleting 1 single line from the database, possibly search for it, then when matched, delete that using an if clause within the loop, just a thought there...

Cheers,
MRb

sloth9

9:33 pm on Oct 5, 2010 (gmt 0)

10+ Year Member



MRb,

A problem with the process you suggested is that I don't think a function exists to modify/delete a line in a txt file. To my knowledge, you can only add to the file. (Disclaimer: when I say to my knowledge, keep in mind that I'm really new to this and my knowledge is extremely limited).

Most of the delete functions I've read about identify the line targeted for deletion and then recreate the database line by line without it. That's what I'm trying to do here

An example of the database file is:

Dan||0||
Joe||1||
Lynda||2||
Ben||3||
Sara||4||

Thanks for your help.

rocknbil

5:40 am on Oct 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



datafile.txt:

1||test line 1||description 1
2||test line 2||description 2
3||test line 3||description 3
4||test line 4||description 4
5||test line 5||description 5
6||test line 6||description 6
7||test line 7||description 7
8||test line 8||description 8

delete_record.php:


<?php
//
$datafile = 'datafile.txt';
if (isset($_GET['id']) and is_numeric($_GET['id']) and ($_GET['id'] > 0)) {
$newfile = null;
$id = $_GET['id'];
$fh = fopen($datafile, "r") or die("cannot open file for reading");
if (is_file($datafile) and $fh) {
while (!feof($fh)) {
$buffer = fgets($fh, 4096);
// Only skip the line if it matches on the id.
// otherwise compile all the contents into $newfile,
// which we will use to overwrite $datafile. Include
// the delimiter so it doesn't match on 42, 406, etc.
// make sure it's writable.
if (! preg_match("/^$id\|\|.*/",$buffer)) {
$newfile .= $buffer;
}
}
}
fclose($fh);
$fh = fopen($datafile, "w") or die("cannot open file for writing");
if (fwrite($fh, $newfile) === FALSE) { die("Cannot write to $datafile"); }
fclose($fh);
// check it
$contents = file_get_contents($datafile);
echo "<pre>$contents</pre>";
}
else {
echo "<p><a href=\"delete_record.php?id=4\">Delete record 4</a></p>";
$contents = file_get_contents($datafile);
echo "<pre>$contents</pre>";
}
?>


It's a standard practice to have the unique identifier first in the list of fields, I suggest moving it, otherwise you would do something like

if (! preg_match("/^[^\|]{2}?\|\|$id\|\|.*/",$buffer)) {
// etc.

sloth9

9:45 am on Oct 6, 2010 (gmt 0)

10+ Year Member



Wow.

I made all the necessary changes to my site and it works! Now I just have to go through the code carefully with the php manual to understand how.

Thanks!