我試圖呈現特定項目的數據。起初我使用了一個代碼來渲染它的數量和ID。那麼它的工作。只有項目數量呈現到購物車
下面的代碼:
<?php
//render the cart for the user to view
$cartOutput = "";
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
}else{
$i = 0;
foreach($_SESSION["cart_array"] as $each_item) {
$i++;
$item_id = $each_item['item_id'];
$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
while($row = mysql_fetch_array($sql)){
$product_name = $row["product_name"];
$price = $row["price"];
}
$cartOutput .= "<h2>Cart Item $i</h2>";
while(list($key, $value) = each($each_item)) {
$cartOutput .= "$key: $value<br>";
}
}
}
?>
但是當我嘗試做出更具體的像只渲染它的編號,名稱,價格和數量。它只能渲染其數量。
這就是:
<?php
//render the cart for the user to view
$cartOutput = "";
if(!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
$cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
}else{
$i = 0;
foreach($_SESSION["cart_array"] as $each_item) {
$i++;
$item_id = $each_item['item_id'];
$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
while($row = mysql_fetch_array($sql)){
$product_name = $row["product_name"];
$price = $row["price"];
}
$cartOutput .= "<h2>Cart Item $i</h2>";
$cartOutput .= "Item ID : " .$each_item['item_id'] . "<br>";
$cartOutput .= "Item Quantity : " .$each_item['quantity'] . "<br>";
$cartOutput .= "Item Name : " .$product_name . "<br>";
$cartOutput .= "Item Price : Php. " .$price . "<br>";
}
}
?>
有人能指導我在這裏?提前致謝。
可能是問題與變量的範圍有關。您在循環中獲得產品名稱和價格的價值,並在循環時將其指向外側。定義上面的變量while循環就在$ product_name =''的foreach語句之下。希望這會幫助你。 – WisdmLabs 2014-09-28 13:00:09
它不起作用,它給了我通知說,它在53行的/home/a9562316/public_html/cart.php其未定義的變量行 – 2014-09-28 13:06:03