我有一个代码,它从表格 A 中获取发布的数据,然后找到商品的价格并显示它们。现在我需要在第二个表格的正文中使用以下数据做更多的事情: 项目名称 项目数量 项目价格
代码在这里:
<?php
echo "<div style='width:30%; height:auto; border: 2px solid;'>";
echo "<h1>The Items Are...</h1><br>";
//array that will hold the sum of each item cost
$totalAmountArray = array();
foreach ($_POST['basket'] as $name => $value)
{
//indexer for totalAmountArray
$i=0;
if($value > 0){
//echo $name . ": " . $value . "<br>";
$itemPrice;
//reassign
$itemName = $name;
$itemQty = $value;
//sql connection..
mysql_connect();
mysql_select_db("xxxxxx");
$result = mysql_query("SELECT * FROM pricing WHERE item = '".$name."'");
while($row = mysql_fetch_array($result)){
$itemPrice = $row['price'];
/*DEBUGGING*/
echo "Item Name: <b>".$itemName . " </b>@ ";
echo "<b>".$itemPrice ." </b>X ".$itemQty."<br>";
}//end of while loop
//multiply to get total for this item - store in temp
$temp = $itemQty * $itemPrice;
//store temp at index i in totalAmountArray
array_push($totalAmountArray, $temp);
unset($result);
unset($itemPrice);
unset($itemQty);
unset($temp);
}//end of if-statement
}//end of foreach
echo "<h1>Total Amount:<br></h1> ";
//print_r($totalAmountArray);
$sum;
foreach($totalAmountArray as $perItemPrice){
$sum += $perItemPrice;
}
echo "<b>SR. ".$sum."</b>";
echo "</div>";
?>
我正在考虑将从上面的 foreach 返回的所有详细信息存储到多维数组中以具有如下内容:
[0] => itemName => itemQty => itemPrice <br>
[1] => itemName => itemQty => itemPrice <br>
[2] => itemName => itemQty => itemPrice <br>
[3] => itemName => itemQty => itemPrice <br>
.....
这会是最好的方法吗?怎么办?在实践中我真的不熟悉多维数组(仅在理论上)。
非常感谢您的帮助和建议,