Add or delete HTML table rows from an .html file using PHP
ProblemI worked on a PHP project where I needed to add and delete rows from an HTML table located in a simple .html file without any database support. The data for the table was provided by a web service. My HTML table looked something like this:
| Id | Name | Action |
|---|---|---|
| 2 | John |
delete |
| 3 | Max |
delete |
Solution
- Write HTML table to a file and surround each row with HTML comments containing unique id.
- Use preg_replace function to find the row surrounded with unique comment and modify it.
How to...
First I had to figure out the file structure. In my case I needed three files:
- file that contains the table
- file to display table
- file that executs requests.
1. File that contains the table
Knowing that with PHP I can append text at the end of file, I needed to create an HTML table that would allow me to add rows to it. Here is what I came up with:
Filename: table_include.html
| Column one | Column two |
|---|
Notice the comment about not including closing "TBODY" and "TABLE" tags.
2. File that displays table.
The table needed to be displayed in a "DIV" element.
File name: my_table.php
echo ''; include("table_include.html"); echo '';
Using PHP include function, I am including table_include.html file and completing table code by adding and below the include. In my case the table rows were generated dynemically as part of a different process. But one important part of the table row generating script was to surround each row with an HTML comment containing a uniqe string. I chose to use PHP's time() function for unique code. The table code looked something like this:
$rowid = time(); $table_row = 'delete rowColumn two content';
3. File that executes requests
See comments for details. File name: execute.php
switch ($_REQUEST["action"]){
case 'add':
//place the code that adds rows here.
//maybe the values for each row are submitted with a form
//for this tutorial I am using a loop to add five rows
for($i=0;$i<5;$i++){
//HTML table markup surrounded with comments
$rowid = time();
$table_row = '< a href="execute.php?action=delete&id=">delete row< /a >Column two content';
//Write the row to "table_include.html" using append "a" parametar
if (!$file_handle = fopen("table_include.html","a")) {
$error = "Cannot open file";
}
if (fwrite($file_handle, $table_row) === FALSE) {
$error = "Cannot write to file";
}
fclose($file_handle);
sleep(1); // we need to have a small pause for generating unique id with PHP time() function. Damn PHP is so fast. :)
}
break;
case 'delete':
$parameter = '';
$filename = "table_include.html";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
//replace pattern that looks for our $parametar and everything in-between
$replace = "%($parameter)(.*?)($parameter)%is";
//use preg_replace to delete the row found using $parameter pattern
$myNewText = preg_replace($replace, "", $contents);
//write updated table code to "table_include.html"
$filename = "table_include.html";
if (!$file_handle = fopen($filename,"w")) {
$error = "Cannot open file";
}
if (!fwrite($file_handle, $myNewText)) {
$error = "Cannot write to file";
}
fclose($file_handle); // close file
//redirect to the table display file
header("Location: my_table.php");
break;
}
Suggestion
One would be to use AJAX to call execute.php file. In the application I was working on I used jQuery for my AJAX functions.
Do you like this or find it useful? Drop me a note or treat me to a double-espresso from my favorite coffee shop.

Comments
November 25th 2009
Emraan - Great ThanX Buddy.
Reply