为了账号安全,请及时绑定邮箱和手机立即绑定

具有 5 列和 25 行的 HTML 表格(使用循环) PHP

具有 5 列和 25 行的 HTML 表格(使用循环) PHP

PHP
慕仙森 2024-01-19 20:49:21
桌子看起来像这样产品ID | 产品价格 | 折扣% | 运输选项 | 付款类型产品 ID | 产品价格 | 折扣% | 运输选项 | 付款类型产品 ID | 产品价格 | 折扣% | 运输选项 | 付款类型产品 ID | 产品价格 | 折扣% | 运输选项 | 付款类型产品 ID | 产品价格 | 折扣% | 运输选项 | 付款类型产品 ID | 产品价格 | 折扣% | 运输选项 | 付款类型...25 行HTML 中每列的 id 为:ProductID_1、ProductID_2、...3、4、5、...直到 25要求是不要更改 HTML 代码!如何通过使用循环而不是键入每一列和行以编程方式管理表?这是我的想法,但不能正常工作:for ($i = 1; $i < 26, $i++) {   $productIDs = $_POST['ProductID_' . $i];   };
查看完整描述

3 回答

?
FFIVE

TA贡献1797条经验 获得超6个赞

试试这个代码,它对我有用


<?php

   for($i=1;$i<=25;$i++)

   {

      echo "<tr>";

      for ($j=1;$j<=5;$j++)

      {

         echo "<td>$i * $j = ".$i*$j."</td>";

      }

   echo "</tr>";

   }

?>


查看完整回答
反对 回复 2024-01-19
?
RISEBY

TA贡献1856条经验 获得超5个赞

根据:


for ($i = 1; $i < 26, $i++) { $productIDs = $ POST['ProductID ' . $我]; };


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


$postedProducts = [];

for ($i = 1; $i < 26; $i++) {

    $currProductID = "ProductID_$i";

    if (isset($_POST[$currProductID])) {

        $postedProducts[] = $_POST[$currProductID];

    }

}

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


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


查看完整回答
反对 回复 2024-01-19
?
炎炎设计

TA贡献1808条经验 获得超4个赞

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


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


查看完整回答
反对 回复 2024-01-19
  • 3 回答
  • 0 关注
  • 38 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信