Php 使用html网页从CSV文件中删除行
我正在编写一个网页,该网页将在表格中显示CSV文件的内容,我想在每一行的末尾添加一个删除按钮,删除CSV文件中的该行,但删除按钮有一些问题。以下是我到目前为止的情况:Php 使用html网页从CSV文件中删除行,php,html,csv,Php,Html,Csv,我正在编写一个网页,该网页将在表格中显示CSV文件的内容,我想在每一行的末尾添加一个删除按钮,删除CSV文件中的该行,但删除按钮有一些问题。以下是我到目前为止的情况: <?php $fileName = "Contacts.csv"; echo "<table> \n\n"; $f = fopen("Contacts.csv", "r"); $i=0; while (($line = fgetcsv($f)) !== false) { echo "<tr
<?php
$fileName = "Contacts.csv";
echo "<table> \n\n";
$f = fopen("Contacts.csv", "r");
$i=0;
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo " <td> " . htmlspecialchars($cell) . " </td> ";
}
echo "<td><button type=\"button\" onclick= ?????? >Delete</button></td>";
echo "</tr>\n";
$i++;
}
fclose($f);
echo "\n</table>";
$string="Hello";
?>
然后我在网上找到了一个函数,用于删除CSV中的行,它包含两个参数,CSV文件的名称和要删除的行的数量
function delLineFromFile($fileName, $lineNum){
// check the file exists
if(!is_writable($fileName))
{
// print an error
print "The file $fileName is not writable";
// exit the function
exit;
}
else
{
// read the file into an array
$arr = file($fileName);
}
// the line to delete is the line number minus 1, because arrays begin at zero
$lineToDelete = $lineNum-1;
// check if the line to delete is greater than the length of the file
if($lineToDelete > sizeof($arr))
{
// print an error
print "You have chosen a line number, <b>[$lineNum]</b>, higher than the length of the file.";
// exit the function
exit;
}
//remove the line
unset($arr["$lineToDelete"]);
// open the file for reading
if (!$fp = fopen($fileName, 'w+'))
{
// print an error
print "Cannot open file ($fileName)";
// exit the function
exit;
}
// if $fp is valid
if($fp)
{
// write the array to the file
foreach($arr as $line) { fwrite($fp,$line); }
// close the file
fclose($fp);
}
echo "Contact was deleted successfully!";
}
函数delLineFromFile($fileName,$lineNum){
//检查文件是否存在
如果(!可写($fileName))
{
//打印错误
打印“文件$fileName不可写”;
//退出函数
出口
}
其他的
{
//将文件读入数组
$arr=文件($fileName);
}
//要删除的行是行号减1,因为数组从零开始
$lineToDelete=$lineNum-1;
//检查要删除的行是否大于文件的长度
如果($lineToDelete>sizeof($arr))
{
//打印错误
打印“您选择的行号[$lineNum]高于文件长度。”;
//退出函数
出口
}
//拆下线路
未设置($arr[“$lineToDelete]”);
//打开文件进行读取
如果(!$fp=fopen($fileName,'w+'))
{
//打印错误
打印“无法打开文件($fileName)”;
//退出函数
出口
}
//如果$fp有效
如果($fp)
{
//将数组写入文件
foreach($arr作为$line){fwrite($fp,$line);}
//关闭文件
fclose($fp);
}
echo“联系人已成功删除!”;
}
所以实际上问题是,我不知道如何在函数delLineFromFile中放入适当数量的要删除的行。
有人知道怎么做吗?以下是我最终的工作方式,而不是我使用的http链接按钮:
echo "<table> \n\n";
$f = fopen("Contacts.txt", "r");
$i=1;
while (($line = fgetcsv($f)) !== false) {
echo "<tr>";
foreach ($line as $cell) {
echo " <td> " . htmlspecialchars($cell) . " </td> ";
}
echo "<td><a href=\"delete.php?lineNum=$i\">Delete</a></td>";
echo "<td>$i</td>";
</td>";
echo "</tr>\n";
$i++;
}
fclose($f);
echo "\n</table>";
echo“\n\n”;
$f=fopen(“Contacts.txt”、“r”);
$i=1;
而(($line=fgetcsv($f))!==false){
回声“;
foreach($行作为$单元格){
echo“.htmlspecialchars($cell)”;
}
回声“;
回音“$i”;
";
回音“\n”;
$i++;
}
外国法郎(f美元);
回音“\n”;
然后我使用$\u GET()函数在另一个php脚本中获取变量:
$fileName = "Contacts.txt";
// the line to delete
$lineNum = $_GET["lineNum"];
delLineFromFile($fileName, $lineNum);
function delLineFromFile($fileName, $lineNum){
// check the file exists
if(!is_writable($fileName))
{
// print an error
print "The file $fileName is not writable";
// exit the function
exit;
}
else
{
// read the file into an array
$arr = file($fileName);
}
// the line to delete is the line number minus 1, because arrays begin at zero
$lineToDelete = $lineNum-1;
// check if the line to delete is greater than the length of the file
if($lineToDelete > sizeof($arr))
{
// print an error
print "You have chosen a line number, <b>[$lineNum]</b>, higher than the length of the file.";
// exit the function
exit;
}
//remove the line
unset($arr["$lineToDelete"]);
// open the file for reading
if (!$fp = fopen($fileName, 'w+'))
{
// print an error
print "Cannot open file ($fileName)";
// exit the function
exit;
}
// if $fp is valid
if($fp)
{
// write the array to the file
foreach($arr as $line) { fwrite($fp,$line); }
// close the file
fclose($fp);
}
echo "Contact was deleted successfully!";
}
$fileName=“Contacts.txt”;
//要删除的行
$lineNum=$_GET[“lineNum”];
delLineFromFile($fileName,$lineNum);
函数delLineFromFile($fileName,$lineNum){
//检查文件是否存在
如果(!可写($fileName))
{
//打印错误
打印“文件$fileName不可写”;
//退出函数
出口
}
其他的
{
//将文件读入数组
$arr=文件($fileName);
}
//要删除的行是行号减1,因为数组从零开始
$lineToDelete=$lineNum-1;
//检查要删除的行是否大于文件的长度
如果($lineToDelete>sizeof($arr))
{
//打印错误
打印“您选择的行号[$lineNum]高于文件长度。”;
//退出函数
出口
}
//拆下线路
未设置($arr[“$lineToDelete]”);
//打开文件进行读取
如果(!$fp=fopen($fileName,'w+'))
{
//打印错误
打印“无法打开文件($fileName)”;
//退出函数
出口
}
//如果$fp有效
如果($fp)
{
//将数组写入文件
foreach($arr作为$line){fwrite($fp,$line);}
//关闭文件
fclose($fp);
}
echo“联系人已成功删除!”;
}
为每一行增加一个变量$i
,并将其添加到name=
属性中?我尝试朝这个方向做一些事情,但不知道如何使用按钮名称为delLineFromFile()生成输入函数重复标题可能是相似的,但这是一个不同的问题,也许你想使用它。否则,你可能会考虑其他方法:不是删除文件,而是将CSV线分解成一个单独的数组,然后在没有删除值的情况下回写。