简体   繁体   English

只有ITEM QUANTITY项呈现到购物车

[英]Only ITEM QUANTITY is render to the cart

I'm trying to render the data for a specific item. 我正在尝试呈现特定项目的数据。 At first I used a code which render its quantity and its ID. 最初,我使用了一个呈现其数量和ID的代码。 Well its work. 好吧,它的工作。

Here's the code: 这是代码:

<?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>";
                }
            }
        }
?>

But when I try to make more specific like only rendering its Id, name, price and quantity. 但是,当我尝试使内容更加具体时,例如仅呈现其ID,名称,价格和数量。 It can only render its quantity. 它只能渲染其数量。

Here it is: 这里是:

<?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>";


            }
        }
?>

Can someone guide me here? 有人可以在这里引导我吗? Thanks in advance. 提前致谢。

My guess is that either the query failed or the field names you have used are wrong. 我的猜测是查询失败或您使用的字段名称错误。 Try adding this piece of code to check both possibilities. 尝试添加这段代码以检查这两种可能性。

$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"];

}

ADDITIONAL SUGGESTION 其他建议

Try this instead, I just saw that the output code was not inside the while loop. 试一下,我只是看到输出代码不在while循环内。 Not that you actually need a while loop as you have limited the query to return only one result LIMIT 1 . 并不是因为需要限制查询仅返回一个结果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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM