删除文本文件的一行。 html表搞砸了

时间:2016-02-26 10:57:02

标签: php foreach text-files

点击删除链接后,删除按钮将停留,它将开始搞乱并删除错误的行。

<?php                        
$delete = @$_GET['delete'];    
$textFile = file("test.txt");      
$lines = count($textFile);

if($delete != "" && $delete >! $lines || $delete === '1') { 
 $textFile[$delete] = ""; 
 $fileUpdate = fopen("test.txt", "wb"); 
 for($a=0; $a<$lines; $a++) { 
        fwrite($fileUpdate, $textFile[$a]); 
 } 
 fclose($fileUpdate); 

} 

foreach($textFile as $key => $val) { 
$line = @$line . "<tr><td>$key</td><td>$val</td>" . "<td><a href =?
delete=$key> Delete </td></tr><br>"; 

} 
error_reporting(0);
echo $line; 

exit;   
?>

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题..希望它有助于某人

<?php
$delete = @$_GET['delete'];
$file_name = "test.txt";
$size = filesize($file_name);
$textFile = file($file_name);
$lines = count($textFile);
if($size == "0")
{
echo "Nothing to display, Seems like file is empty. Try adding some data to it!!!";
exit;
}
if($delete != "" && $delete >! $lines || $delete === '0') {
$textFile[$delete] = "";
$fileUpdate = fopen($file_name, "wb");
for($a=0; $a< $lines; $a++) {
       fwrite($fileUpdate, $textFile[$a]);
}
fclose($fileUpdate);
header("Location:del.php");
exit;
} else

foreach($textFile as $key => $val) {
$line = @$line . "<tr><td>$key</td><td>$val</td>" . "<td><a href =?delete=$key> Delete </td></tr><br>"; 
}
echo $line;
?>