2011-10-18 43 views
0

我正在編寫一個網頁,它將在表中顯示一個CSV文件的內容,並且我希望在每行末尾添加一個刪除按鈕,以便刪除該行CSV文件,但我在刪除按鈕時遇到了一些問題。以下是我迄今爲止:使用html網頁從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中。 有誰知道該怎麼做?

+0

爲每行增加一個變量'$ i'並將其添加到'name ='屬性中? –

+0

我試着做那個方向的東西,但後來我不知道如何使用按鈕名稱來產生delLineFromFile()函數的輸入 – Jaro

+0

duplicate title http://stackoverflow.com/questions/4072015/remove-line-from -csv-file –

回答

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!"; 
}