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

报告相同问题?