comm.php
<?
$com = $req_user_info['comments'];
$name = $username;
if($_POST) {
$postdate = date("d M y h:i A");
$content = $_POST['commentContent'];
$handle = fopen("$com","a");
fwrite($handle,"<b>" . $name . "</b>:<br>" . $content . "<br>" . $postdate . "<br>");
fclose($handle);}
?>
<html>
<body>
<form action = "" method = "POST">
Post a Comment<br><textarea rows="10" cols="30" name="commentContent"></textarea><br>
<input type="submit" value="Comment"><p>
Comments<p>
<?
include($com);
?>
</body></html>
/*$req_user_info['comments']; = data.txt*/
data.txt中
Alex: sometext 20 Feb 12 11:11 AM
Alex: sometext 20 Feb 12 11:38 AM
我想要做的是删除(或替换为空格)名称,内容和过期日期。
示例:
Alex:sometext 20 Feb 12 11:11 AM删除
Alex:sometext 20 Feb 12 11:38 AM删除
所以在我点击删除并刷新页面后,我希望该行消失。
答案 0 :(得分:0)
您可以使用fgets逐行读取(http://www.php.net/manual/en/function.fgets.php),而不是仅正常包含文件。在迭代时存储每个行号,然后在删除链接上传递行号。
<a href="page.php?delete=XXX">Delete</a>
当你想要删除时,简单地浏览每一行并重写文件,跳过用删除链接变量$ _GET [&#39; delete&#39;]给出的行。只需确保在编写每个新行时进行计数,以便知道何时跳过一行。
使用PHP示例:
<?php
$counter = 0;
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$counter++;
echo $buffer . '<a href="page.php?delete='.$counter.'">delete</a>';
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
?>