简体   繁体   English

PHP将行动态添加到现有HTML表中

[英]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 table. 在wordpress插件中,我有一个PHP函数,可将$ file_name (CSV文件)转换为表。

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! 问题是我想将新的ROWS和COLUMNS添加到同一页面中的现有表中! How could I do that easily? 我怎么能轻松做到这一点?

Remove "echo <table>" and "echo </table>" and insert that function inside the existing table element. 删除“ echo <table>”和“ echo </ table>”,然后将该函数插入现有表元素中。 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); 像这样调用: displayAsTable('rowstoappend.csv',false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM