簡體   English   中英

只有ITEM QUANTITY項呈現到購物車

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM