使用html网页从CSV文件中删除行

时间:2011-10-18 13:00:42

标签: 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>";
        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文件的名称和要删除的行的nuber。

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中放入适当数量的行来删除。 有谁知道怎么做?

1 个答案:

答案 0 :(得分:0)

这对我最终有用,而不是我使用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>";

然后我使用$ _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!";
}