[英]PHP / Sessions Shopping Cart - Quantity in cart only updates for first item
[英]Only ITEM QUANTITY is render to the cart
我正在尝试呈现特定项目的数据。 最初,我使用了一个呈现其数量和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>";
}
}
}
?>
但是,当我尝试使内容更加具体时,例如仅呈现其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>";
$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>";
}
}
?>
有人可以在这里引导我吗? 提前致谢。
我的猜测是查询失败或您使用的字段名称错误。 尝试添加这段代码以检查这两种可能性。
$sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
if ( $sql === false ) {
echo 'Query Failed: ' . mysql_error();
}
while($row = mysql_fetch_array($sql)) {
print_r($row); // check the field names in this array
$product_name = $row["product_name"];
$price = $row["price"];
}
其他建议
试一下,我只是看到输出代码不在while循环内。 并不是因为需要限制查询仅返回一个结果LIMIT 1
而实际上需要一个while循环。
<?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)){
// no real point in moving these values to another variable
// just to print them
//$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 : " . $row["product_name"] . "<br>";
$cartOutput .= "Item Price : Php. " . $row["price"] . "<br>";
}
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.