只有ITEM QUANTITY才会呈现给购物车

时间:2014-09-28 12:47:23

标签: php

我试图渲染特定项目的数据。起初我使用了一个代码来渲染其数量和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>";


            }
        }
?>

有人可以在这里指导我吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

我的猜测是查询失败或您使用的字段名称是错误的。尝试添加这段代码来检查两种可能性。

$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循环中。并不是说你实际上需要一个while循环,因为你限制查询只返回一个结果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>";
        }    
    }
}
?>