[英]Delete lines from CSV file using html web page
我正在寫一個網頁,該表格將在表中顯示CSV文件的內容,並且我想在每行的末尾添加一個Delete按鈕,以刪除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文件的名稱和要刪除的行的編號。
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中放入適當的行數以進行刪除。 有誰知道這是怎么做到的嗎?
這是最后對我有用的,而不是我使用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!";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.