简体   繁体   中英

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. 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. 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. Not that you actually need a while loop as you have limited the query to return only one result LIMIT 1 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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