Forum Moderators: coopster
<?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>";
?>
<?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>";
}
?>