PHP add dynamically row into an existing HTML table
Within a wordpress plugin, I have a PHP function which transform $file_name (a CSV file) into a tabl开发者_开发问答e.
function displayAsTable($file_name)
{
echo '<table>';
ini_set('auto_detect_line_endings',TRUE);
$f = fopen($file_name, "r");
while (($line = fgetcsv($f,0,",")) !== false) {
echo '<tr>';
echo '<td><input type="checkbox" name="addressXX" value="'.$line[2].'" name=""/></td>';
foreach ($line as $cell) {
echo '<td>' . htmlspecialchars($cell) . '</td>';
}
echo '<tr>';
}
fclose($f);
echo '</table>';
}
The problem is that I would like to add the new ROWS and COLUMNS into an existing TABLE within the same page! How could I do that easily?
Remove "echo <table>" and "echo </table>" and insert that function inside the existing table element. That should solve the problem because it will simply create new rows in addition to existing rows.
I would change this function to:
function displayAsTable($file_name,$newTable = TRUE)
{
if($newTable){ echo '<table>'; }
//rest of code without last echo
if($newTable){ echo '</table>'; }
}
And call it like: displayAsTable('rowstoappend.csv',false);
精彩评论