douju5062 2013-08-28 20:34
浏览 40
已采纳

使用PHP简化HTML表格代码

I have a webpage (created from within PHP files) that needs to display a table that is 30 rows long and allows the user to enter values for each of the 30 rows and then press a button to let php process what they have entered.

Anyway instead of having to write out a normal HTML form with a table that has 30 rows I wonder if their is any way to create this table in PHP with much less code.

SO as it is it looks something like

<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>

I am currently just writing out the complete table like above, the values for weight, cbm min and max are not increasing at a standard rate so a normal loop would not work I guess, could these values be put into an array perhaps? My php is very rusty

  • 写回答

2条回答 默认 最新

  • doulou0882 2013-08-28 20:46
    关注

    Here is a possible solution.

    /* 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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么