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

桌子看起来像这样

产品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];   };


慕仙森
浏览 45回答 3
3回答

FFIVE

试试这个代码,它对我有用<?php&nbsp; &nbsp;for($i=1;$i<=25;$i++)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; echo "<tr>";&nbsp; &nbsp; &nbsp; for ($j=1;$j<=5;$j++)&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo "<td>$i * $j = ".$i*$j."</td>";&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;echo "</tr>";&nbsp; &nbsp;}?>

RISEBY

根据:for ($i = 1; $i < 26, $i++) { $productIDs = $ POST['ProductID ' . $我]; };如果您需要在服务器端拥有一系列已发布的产品:$postedProducts = [];for ($i = 1; $i < 26; $i++) {&nbsp; &nbsp; $currProductID = "ProductID_$i";&nbsp; &nbsp; if (isset($_POST[$currProductID])) {&nbsp; &nbsp; &nbsp; &nbsp; $postedProducts[] = $_POST[$currProductID];&nbsp; &nbsp; }}不过,我不明白为什么你需要阅读$_POST。通常,具有出现在用户输入中的id的产品用于编辑或选择操作。如果确实有任何数据发布,并且是为了检查所选产品,则检查特定产品 ID 是否发布$selectedProductIDs[] = $i;在if块中就足够了。

炎炎设计

你可以试试这个代码。这段代码肯定有效。<!DOCTYPE html><html><head>&nbsp; &nbsp; <title>Table using php</title></head><body>&nbsp; &nbsp; <table border="1">&nbsp; &nbsp; &nbsp; &nbsp; <thead>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>ProductID</th><th>ProductPrice</th><th>Discount%</th><th>ShippingOption</th><th>PaymentType</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; </thead>&nbsp; &nbsp; &nbsp; &nbsp; <tbody>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tableStr = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for($rows=1;$rows<=25;$rows++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tableStr += "<tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tableStr += "<td>".$_POST['ProductID_'.$rows]."</td>";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tableStr += "<td>".$_POST['ProductPrice_'.$rows]."</td>";&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tableStr += "<td>".$_POST['Discount_'.$rows]."</td>";&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tableStr += "<td>".$_POST['ShippingOption_'.$rows]."</td>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tableStr += "<td>".$_POST['PaymentType_'.$rows]."</td>";&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tableStr += "</tr>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; </tbody>&nbsp; &nbsp; </table></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP