【发布时间】:2016-11-06 22:54:32
【问题描述】:
我有一个包含产品名称及其对应数量的表格。
Product Quantity Delete
A 10 Del
B 5 Del
我想要做的是将它们放在具有产品相应 id 的数组中。我可以获得第二个产品的产品 ID,但我无法获得第一个产品的 ID。我该如何解决?
这是 php 代码的输入表单:
<table>
<tr>
<td><input id="product1" type="text" value="A" name="product1"></td>
<td><input id="quantity1" type="text" value="10" name="quantity1>
<input type='hidden' name='htmlrow[]' value='1'> </td> //row number
</tr>
<tr>
<td><input id="product2" type="text" value="B" name="product2"></td>
<td><input id="quantity2" type="text" value="5" name="quantity2">
<input type='hidden' name='htmlrow[]' value='2'></td>//row number
</tr>
</table>
php代码:
$numberOfRows = $_POST["htmlrow"];
$r = end($numberOfRows);
for($i=1; $i<$r+1; $i++){ //r = number of rows
$results = $wpdb->get_results("SELECT * FROM wp_products WHERE productName = '".$_POST["product".$i]."'");
foreach($results as $product){
$pID = $product->productID;
} //I can only get the last product ID
$orderItem = array(
'product' => $_POST["product".$i],
'productID' => $pID,
'quantity' => $_POST["quantity".$i]
);
$order[] = $orderItem;
}
【问题讨论】:
-
请在
$i<$r+1中解释$r -
您向我们展示的
html代码的目的是什么? -
html代码是什么,是输入还是输出? php执行之前还是之后? -
php 执行前。
-
你想在你的
$order中写两行,是吗?那你为什么有两个循环,一个for和一个foreach?
标签: php mysql arrays wordpress