我有一個代碼,從表單A獲取發佈的數據,然後查找商品的價格並顯示它們。現在我需要做更多的以下數據在第二形式的身體: 項目名稱 項目數量 項目價格PHP多維數組或更好的選項?
的代碼是在這裏:
<?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>
.....
這會是最好的方式嗎?如何做?我在實踐中(僅在理論上)並不熟悉多維陣列。
非常感謝您的幫助和建議,
所以創建一個對象並將這些值存儲在類變量中...我可以這樣做。我會嘗試編碼它現在...不應該花很長時間來知道它是否失敗或不..很抱歉:)感謝隊友 – 2012-04-15 07:42:36
好酷我希望它有幫助 – 2012-04-15 07:44:38
我做到了,我相信它現在工作..現在我至少我想,可以按照我的要求組織展示 – 2012-04-15 08:07:14