php - 从文本文件php中删除行

标签 php

我正在尝试列出文本文件中的所有行,并在每一行旁边都有一个删除按钮。按下按钮时,应删除该行。如下图

 Line One Text | Delete
 Line Two Text | Delete
 Line Three Text | Delete

我认为我非常接近但无法让它工作,有什么想法吗?线条和按钮显示正常,但删除按钮似乎没有做任何事情。

编辑.php

<?php
$textFile = file("guestbook.txt");
foreach ($textFile as $key => $val) {
    $line = @$line . $val
        . "<input type='submit' name=$key value='Delete'><br />";
}

$form = "<form name='form' method='post' action='deleter.php'> $line 
    </form>";

echo $form;
?>

删除器.php

if ($delete != "" && $delete > !$lines || $delete === '0') {
    $textFile[$delete] = "";
    $fileUpdate = fopen("data.txt", "wb");
    for ($a = 0; $a < $lines; $a++) {
        fwrite($fileUpdate, $textFile[$a]);
    }
    fclose($fileUpdate);
    header("Location:edit.php");
    exit;
}

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

提前致谢!

最佳答案

如果没有一行可以等于另一行,一种方法是使用要删除的字符串查询服务器,例如 delete.php?str=<your string>并删除该字符串(请注意,我说的是“字符串”而不是行)。

如果线条可能相等,您应该识别线条并遵循类似的方法:delete.php?line=<number> .

这应该很容易,您可以编写一个灵活的代码,通过传递文件名从给定文件中删除行。

编辑:根据使用隐藏字段的建议提供的示例。

<?php
$html = "<form id='form' method='post' action='delete.php'>";
foreach(array("line 1", "line 2", "line 3") as $key => $value ) {
    $html .= "<span>${value}</span>";
    $html .= "<input type='hidden' name='lineNumber' value='${key}'>";
    $html .= "<input type='submit' value='Delete' name='line'><br>";
}
$html .= "</form>";
echo $html;
?>

在你的delete.php获取$_POST['lineNumber']的内容检索行号。

关于php - 从文本文件php中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18500093/

相关文章:

php - 具有两个日期约束的一对多查询

php - 将 float 存储到之前转义为字符串的数据库? - 安全的?

javascript - AJAX 多表单提交

php - curl 发布 : 400 Invalid content length

php - 错误 2002 (HY000) : Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

php - 如何在 phpfarm php 安装上安装 pthreads

php - 根据值对二维数组进行分组

javascript - 我有一个 php 文件,它提供 JSON 输出,并希望在 D3 中使用该 JSON

php - 如何使用 PHP 过滤/删除 Google 电子邮件别名?

php - 插入 MySQL 之前检查 MySQL 日期范围