I'm just starting to scratch the surface with PHP. Sorry to ask a beginner's question but I have spent way too much time research and experimenting and I can't figure it out. Any help would be very much appreciated. I want to be able to delete elements from a text file. The problem is that instead of tagging an element to remove, it wipes out the entire text file. Any help would be much appreciated
Data is something like this:
1|Gray
2|Red
3|Yellow
Code is:
<?php
/*===================================================================================
delete.php allows the users to change and delete data from
the main text file "field_locations.txt" Additional scripts: add.php, edit.php
*/
$filename = "field_locations.txt";
$file_array = file($filename);
echo '<table>';
foreach ($file_array as $key => $value) {
list($col_a, $col_b) = explode("|",trim($value));
echo '
<tr>
<td>' . $col_a . '</td>
<td>' . $col_b . '</td>
<td><input type = "checkbox" name="delete[' . $key . ' ]"></td>
</tr>';
}
echo '</table>';
// loop through selected items to delete
foreach ($_POST['delete'] as $key =>$value) {
//double check to be sure it was selected
if ($value == "on") {
foreach ($file_array as $file_line) {
list($col_a, $col_b) = explode("|",trim($file_line));
if($col_a !=$value) {
$new_file[]=$col_a . "|" . $col_b;
}
}
}
}
$open_file=fopen($filename,'w');
fwrite($open_file, implode("\n".$file_array));
fclose($open_file);
echo "Deleted an existing record<br/>";
?>