【问题标题】:HTML Table with 5 columns and 25 rows (using loops) PHP具有 5 列和 25 行的 HTML 表格(使用循环)PHP
【发布时间】:2021-01-01 19:16:27
【问题描述】:

表格是这样的

产品ID |产品价格 |折扣% |运输选项 |支付类型
产品编号 |产品价格 |折扣% |运输选项 |支付类型
产品编号 |产品价格 |折扣% |运输选项 |支付类型
产品编号 |产品价格 |折扣% |运输选项 |支付类型
产品编号 |产品价格 |折扣% |运输选项 |支付类型
产品编号 |产品价格 |折扣% |运输选项 |支付类型
...25 行

HTML 中每一列的 id 是: ProductID_1, ProductID_2, ...3,4,5, ... 直到 25

要求是不要更改 HTML 代码!

如何通过使用循环而不是键入每一列和每一行来以编程方式管理表格?

这是我的想法,不能正常工作:
for ($i = 1; $i < 26, $i++) { $productIDs = $_POST['ProductID_' . $i]; };

【问题讨论】:

    标签: php html post html-table


    【解决方案1】:

    试试这个对我有用的代码

    <?php
       for($i=1;$i<=25;$i++)
       {
          echo "<tr>";
          for ($j=1;$j<=5;$j++)
          {
             echo "<td>$i * $j = ".$i*$j."</td>";
          }
       echo "</tr>";
       }
    ?>
    

    【讨论】:

      【解决方案2】:

      根据:

      for ($i = 1; $i POST['ProductID' 。 $i]; };

      如果您需要在服务器端发布一系列产品:

      $postedProducts = [];
      for ($i = 1; $i < 26; $i++) {
          $currProductID = "ProductID_$i";
          if (isset($_POST[$currProductID])) {
              $postedProducts[] = $_POST[$currProductID];
          }
      }
      

      不过,我不明白您为什么需要阅读 $_POST。通常,在用户输入中出现 id 的产品用于编辑或选择操作。

      如果确实有任何数据发布,并且是为了检查所选产品,则检查是否发布了特定的产品ID:if块中的$selectedProductIDs[] = $i;就足够了。

      【讨论】:

        【解决方案3】:

        你可以试试这个代码。这段代码肯定是有效的。

        <!DOCTYPE html>
        <html>
        <head>
            <title>Table using php</title>
        </head>
        <body>
        
            <table border="1">
                <thead>
                <tr>
                    <th>ProductID</th><th>ProductPrice</th><th>Discount%</th><th>ShippingOption</th><th>PaymentType</th>
                </tr>
                </thead>
                <tbody>
                    <?php
                        $tableStr = "";
                        for($rows=1;$rows<=25;$rows++)
                        {
                            $tableStr += "<tr>";                    
                            $tableStr += "<td>".$_POST['ProductID_'.$rows]."</td>"; 
                            $tableStr += "<td>".$_POST['ProductPrice_'.$rows]."</td>";  
                            $tableStr += "<td>".$_POST['Discount_'.$rows]."</td>";  
                            $tableStr += "<td>".$_POST['ShippingOption_'.$rows]."</td>";
                            $tableStr += "<td>".$_POST['PaymentType_'.$rows]."</td>";       
                            $tableStr += "</tr>";
                        }
                    ?>
                </tbody>
            </table>
        
        </body>
        </html>
        

        【讨论】:

          猜你喜欢
          • 2016-02-23
          • 2012-12-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-06
          相关资源
          最近更新 更多