【问题标题】:Simplyfy HTML Table code using PHP使用 PHP 简化 HTML 表格代码
【发布时间】:2013-08-29 04:48:41
【问题描述】:

我有一个网页(从 PHP 文件中创建)需要显示一个 30 行长的表格,并允许用户为 30 行中的每一行输入值,然后按一个按钮让 php 处理他们拥有的内容输入。

无论如何,我不必用一个有 30 行的表格写出一个普通的 HTML 表单,我想知道他们是否可以用更少的代码在 PHP 中创建这个表格。

所以它看起来像

<form name="createtable" action="?page=createtable" method="POST" autocomplete="off">
    <table border='1'>
        <tr>
            <th> Weight </th>
            <th> CBM Min </th>
            <th> CBM Max </th>
            <th> Min </th>
        </tr>
        <tr>
            <th> 1000 </th>
            <th> 0.1 </th>
            <th> 2.3 </th>
            <th> <input type=text name=min1> </th>
        </tr>
        <tr>
            <th> 1500 </th>
            <th> 2.31 </th>
            <th> 3.5 </th>
            <th> <input type=text name=min2> </th>
        </tr>
        <tr>
            <th> 2000 </th>
            <th> 3.51 </th>
            <th> 4.6 </th>
            <th> <input type=text name=min3> </th>
        </tr>
            ..... + 27 more rows
    </table>
</form>

我目前只是像上面一样写出完整的表格,重量、cbm min 和 max 的值没有以标准速率增加,所以我猜正常循环不起作用,这些值是否可以放入数组中?我的 php 很生锈

【问题讨论】:

  • 找出代码中重复的部分(提示:tr with childs),然后将该代码移动到循环中。因此,为每一行打印一个带有内容的 tr。
  • 请注意,&lt;th&gt; 指的是表头,在前四个之后你可能想要&lt;td&gt;
  • 你从哪里得到这些值?您的&lt;th&gt; 标签之间的那些? (顺便说一句,这些不应该是&lt;td&gt; 标签吗?通常,你应该只有一组&lt;th&gt; 标签——所有其余的行都应该使用&lt;td&gt; 标签。)如果你从数据库,请告诉我们。最好也显示该代码。
  • “代码少得多”比什么?您还没有显示您当前使用的代码,那么我们如何知道您需要将其更改为什么?一般来说,创建表通常涉及一个forwhile 循环,该循环遍历数组或数据库查询结果。
  • 不能自动化所有事情并不意味着您应该对整个事情进行硬编码。将硬编码的值放入一个数组中,然后自动化其余部分。

标签: php html html-table forms


【解决方案1】:

这是一个可能的解决方案。

/* this should contain all rows, a resultset from a database,
   or wherever you get the data from */
$rows = array(
    array(
      'weight' => 1000,
      'cbm_min' => 0.1,
      'cbm_max' => 2.3
    ),
    array(
      'weight' => 1500,
      'cbm_min' => 2.31,
      'cbm_max' => 3.5
    ),
    array(
      'weight' => 2000,
      'cbm_min' => 3.51,
      'cbm_max' => 4.6
    )
); 

?>
<form name="createtable" action="?page=createtable" method="POST" autocomplete="off">
  <table border='1'>
    <tr>
      <th> Weight </th>
      <th> CBM Min </th>
      <th> CBM Max </th>
      <th> Min </th>
    </tr>
<?php
$i = 1; // I'll use this to increment the input text name
foreach ($rows as $row) {
  /* Everything happening inside this foreach will loop for
     as many records/rows that are in the $rows array. */
 ?>
    <tr>
      <th> <?= (float) $row['weight'] ?> </th>
      <th> <?= (float) $row['cbm_min'] ?> </th>
      <th> <?= (float) $row['cbm_max'] ?> </th>
      <th> <input type=text name="min<?= (float) $i ?>"> </th>
    </tr>
  <?php
  $i++;
}
?>
  </table>
</form>
<?php
// Continue executing PHP

【讨论】:

  • 你不需要echo那些PHP语句吗?还是(浮动)以某种方式解决了这个问题?
  • @Jordan:&lt;?= 是一个简写的回显,不用担心,它不受 php 短标签移除的影响,所以应该可以安全使用。 Float 只是为了确保我们正在打印数字 (XSS)。
  • @JimL Gotcha,不知道它会为你处理回声:P
  • @Jordan:在构建模板时很棒:)
  • @ZacPowell:Np,很高兴看到一个比“请为我编码”更费力的问题。下次我也会添加数据源,最好使用一些测试数据的 var_dump。它只是让它更容易:)
【解决方案2】:

你绝对可以用 PHP 做到这一点

您需要一个 2D 数组来包含所有要显示在所有 &lt;tr&gt; * &lt;th&gt; 中的值,即在您的情况下为 30 * 3,因为第三列将包含 &lt;input&gt;

很容易找到声明数组和编写 for 循环的语法。

for($i=0;$i<30;$i++)
{
for($j=0;$j<4;$j++)
{
    echo "<td>array[$i][$j]</td>"
}
echo "<input>...</input>"
}

【讨论】:

    猜你喜欢
    • 2012-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    相关资源
    最近更新 更多