繁体   English   中英

用PHP创建动态表

[英]Create Dynamic Table in PHP

我面临一个非常简单的任务,但是由于某种原因,解决方案使我望而却步,这可能是因为我的PHP不好。

我有一个表,该表具有1个跨4列的标题。 在第一列中,它将是静态数据(即可以这么说的标头,但仅在第一列中)。 我需要的是根据返回的结果填充第二列,依此类推。 我正在建立一个表格,当比较产品时,它将在列中填充与产品相关的信息

<table id="price-compare" class="device-compare" border="1">
<caption>Price</caption>
<?php
$rows = 3;
$cols = 4;

for($tr=1;$tr<=$rows;$tr++){ 
    echo "<tr>";

            for($td=1;$td<=$cols;$td++){
            echo "<td>row: ".$tr." column: ".$td."</td>";
        }
    echo "</tr>";
}
?>

<tr>
    <td class="left-header"><?php echo $phones->field('name'); ?></td>
    <td><sup>$</sup><?php echo $phones_retail; ?></td>
</tr>
<tr>
    <td class="left-header">Fuel Price</td>
    <td><sup>$</sup><?php echo $phones_retail; ?></td>
</tr>
<tr>
    <td class="left-header">2-yr Contract Price</td>
    <td style="font-weight: bold; color:#1b75bb;"><sup>$</sup><?php echo $phones_contract; ?></td>
</tr>
</table>

表格截图

如您所见,我需要第一列保持不变,并将与产品相关的数据添加到后续列。

试试下面的代码,希望对您有所帮助:

  for($td=1;$td<=$cols;$td++){
                echo "<td>row: ".$tr." column: ".$td."</td>";
    }

replace the above code by below one:


for($td=1;$td<=$cols;$td++){
 if($td == 1)
  {
    echo "<td>Static Data</td>";
  }
 else
 {
    echo "<td>row: ".$tr." column: ".$td."</td>";
 }
 }

暂无
暂无

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

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