Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6733721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:48:10+00:00 2026-05-26T10:48:10+00:00

I am writing a web page that will display the content of a CSV

  • 0

I am writing a web page that will display the content of a CSV file in a table and I want to add a Delete button at the end of each row that would delete that line in CSV file, but I’m having some problems with the delete button. Here is what i have so far:

<?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";
?>

And then there is a function that I found online for deleting the line in CSV that takes two parameters, the name of CSV file and the nuber of the line to delete.

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

So actually the problem is that I don’t know how to put the appropriate number of line to delete in the function delLineFromFile.
Does anyone know how to do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:48:11+00:00Added an answer on May 26, 2026 at 10:48 am

    Here is what worked for me in the end, instead of a button I used http link:

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

    And then I used $_GET() function to get the variable in the other php script:

    $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!";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a web page that will detect which college campus a user is
I am writing a web page in PHP that will provide some useful tools
I am writing a web page that uses some php files. The php isn't
I'm writing part of a web page that allows a user to build a
I found a javascript breadcrumb trail that will display the home page, what folder
I am writing a web page that displays students grades in an old grade
I'm writing an asp.net Web Application. I wrote a login page that authenticates a
The problem I'm having with writing a web application architecture is that I want
I am writing a web application that will run in kiosk mode on a
I am writing a web page widget for my company. This widget must inject

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.