dongpan1871 2014-06-24 10:56
浏览 44
已采纳

php中的购物车不显示产品详细信息

I am new to PHP and i am making a shopping cart for my website. The problem is when i click on the button "ADD TO SHOPPING CART" It goes to the cart.php page where i am trying to display the product name, details, price, quantity to adjust , total price, remove the item but it is not display all the details about the product it only shows me the quantity. Here is my code to display the cart product.

$cartOutput="";
$cartTotal="";
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'];
        $result = mysqli_query($con, "SELECT * FROM products WHERE id='$item_id' LIMIT 1 ");
        if($result == FALSE) {
            die(mysql_error()); // TODO: better error handling
    }
    while($row = mysqli_fetch_array($result)) 
        {
            $id = $row["id"];
            $product_name = $row["product_name"];
            $price = $row["price"];
            $details = $row["details"];
        }
        $pricetotal = $price * $each_item['quantity'];
        $cartTotal = $pricetotal + $cartTotal;
        // Dynamic Table row assembly
        $cartOutput .= '<tr>';
        $cartOutput .= '<td align=\'center\' >' . $product_name . '<br/></td>';
        $cartOutput .= '<td align=\'center\' >' . $details . '</td>';
        $cartOutput .= '<td align=\'center\' >' . $price . '</td>';
        $cartOutput .= '<td>
            <form action="cart.php" method="post">
            <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
            <input name="adjustBtn' . $item_id . '" type="submit" value="change" />
            <input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
            </form>
            </td>';
        //$cartOutput .= '<td align=\'center\' >' . $each_item['quantity'] . '</td>';
        $cartOutput .= '<td align=\'center\' >' . $pricetotal . '</td>';
        $cartOutput .= '<td> <form action="cart.php" method="post">
            <input name="deleteBtn' . $item_id . '" type="submit" value="X" /> 
            <input name="index_to_remove" type="hidden" value="' . $i . '" /></form> </td>';
        $cartOutput .= '</tr>';
    $i++;

    }

}

(if user attempts to add something to the cart from the product page)

if (isset($_POST['pid'])) {
    $pid = $_POST['pid'];
    $wasFound = false;
    $i = 0;

    // If the cart session variable is not set or cart array is empty
    if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { 
    // RUN IF THE CART IS EMPTY OR NOT SET
    $_SESSION["cart_array"] = array(0 => array("item_id" => $pid, "quantity"=> 1));
} else {
    // RUN IF THE CART HAS AT LEAST ONE ITEM IN IT
    foreach ($_SESSION["cart_array"] as $each_item) { 
        $i++;
        while (list($key, $value) = each($each_item)) {

            if ($key == "item_id" && $value == $pid) {

            array_splice($_SESSION["cart_array"], $i-1, 1, 
            array(array("item_id" => $pid, "quantity" => $each_item['quantity'] + 1)));
            $wasFound = true;
            } // close if condition
        } // close while loop
    } // close foreach loop

    if ($wasFound == false) {
        array_push($_SESSION["cart_array"], array("item_id" => $pid, "quantity" => 1));
    }
}

header("location: cart.php"); 
exit();
}

to show the product

$dynamicList = "";

$result = mysqli_query($con,"SELECT * FROM products ");
$productCount = mysqli_num_rows($result); // count the output amount




if ($productCount > 0) {
while($row = mysqli_fetch_array($result)){ 
                 $id = $row["id"];
         $product_name = $row["product_name"];
         $product_code = $row["product_code"];
         $availability = $row["availability"];
         $price = $row["price"];
         $category = $row["category"];
         $subcategory = $row["subcategory"];
         $details = $row["details"];
         $date_added = strftime("%b %d, %Y",   
strtotime($row["date_added"]));
         // To show latest items on the home page

         $dynamicList .= ' <div class="product-items">
  <div class="product-block">
  <div class="product-block-inner">

                <div class="image">
                    <a href="product.php?id=' . $id . '"><img 
 src="product_images/' . $id . '.jpg" /> </a>
                </div>

                <div class="name">
                    <a href="product.php?id=' . $id . '">' . 
 $product_name . '</a>
                </div>
                <div class="price">
                    RS. ' . $price . '
                    <br />
                    Availability: ' . $availability . '
                </div>
                <div class="cart">
                    <form id="form1" name="form1" 
method="post" action="cart.php">
                        <input type="input" name="pid" id="pid" 
value="<?php echo $id; ?>" />
                        <input type="submit" name="button" 
id="button" class="button" value="Add to Shopping Cart" />
                        </form>

                </div>
</div>
</div>
</div>






                        '; 


}
} else {
$dynamicList = "We have no products listed in our store yet";

}
  • 写回答

1条回答 默认 最新

  • dsl36367 2014-06-24 11:07
    关注

    try this one

     <?php
        $cartOutput="";
        $cartTotal="";
        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'];
            $result = mysqli_query($con, "SELECT * FROM products WHERE id='$item_id' 
        LIMIT 1 ");
             if($result == FALSE) {
                die(mysql_error()); // TODO: better error handling
            }
             $row = mysqli_fetch_array($result); 
    
                    $id = $row["id"];
                    $product_name = $row["product_name"];
                    $price = $row["price"];
                    $details = $row["details"];
    
                $pricetotal = $price * $each_item['quantity'];
                $cartTotal = $pricetotal + $cartTotal;
                // Dynamic Table row assembly
                $cartOutput .= '<tr>';
                $cartOutput .= '<td align=\'center\' >' . $product_name . '<br 
        /></td>';
                $cartOutput .= '<td align=\'center\' >' . $details . '</td>';
                $cartOutput .= '<td align=\'center\' >' . $price . '</td>';
                $cartOutput .= '<td>
                <form action="cart.php" method="post">
        <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" 
        maxlength="2" />
        <input name="adjustBtn' . $item_id . '" type="submit" value="change" />
        <input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
        </form>
        </td>';
        //$cartOutput .= '<td align=\'center\' >' . $each_item['quantity'] . '</td>';
        $cartOutput .= '<td align=\'center\' >' . $pricetotal . '</td>';
        $cartOutput .= '<td> <form action="cart.php" method="post">
            <input name="deleteBtn' . $item_id . '" type="submit" value="X" /> 
            <input name="index_to_remove" type="hidden" value="' . $i . '" /></form> </td>';
        $cartOutput .= '</tr>';
        $i++;
    
    
            }
    
        }
    
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 eclipse连接sap后代码跑出来空白
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案