使用PHP在购物车中显示商品清单 [英] Display item list in shopping cart using PHP

查看:53
本文介绍了使用PHP在购物车中显示商品清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过foreach循环,我试图连接到数据库并在列表中显示已添加到购物车的产品.每个产品都有一个产品ID,该ID可以正常工作,并且可以通过cart.php存储在会话变量中.我不知道如何连接到数据库以显示有关所添加产品的信息-我也尝试做var_dump $ SESSION ['cart'],即使使用后,它也会打印出 null cart.php 中的添加"按钮.

 < div class ="row">< h4>购物车</h4><?phpforeach($ _ SESSION ['cart'] as $ proid => $ proq){//$ proid是产品ID,$ proq是数量//使用$ proid从数据库中选择产品详细信息}?></div><!-以下是我的cart.php页面-><?phpsession_start();$ productID = $ _GET ['product'];$ action = $ _GET ['action'];switch($ action){情况添加":$ _SESSION ['cart'] [$ productID] ++;休息;案例删除":$ _SESSION ['cart'] [$ productID]-;if($ _ SESSION ['cart'] [$ productID] == 0)未设置($ _SESSION ['cart'] [$ productID]);休息;案例空":未设置($ _SESSION ['cart']);休息;}header("Location:Browse.php");?> 

解决方案

根据您的解释,您试图从会话值中检索数据以填充数据库查询.

但是,当您的 for 循环执行时,您尚未将会话数据反序列化到内存中(因此无法访问它,并且会得到 null 值)./p>

您需要在 for 循环之前开始会话:

  session_start();foreach($ _ SESSION ['cart'] as $ proid => $ proq){ 

请参见 php手册

此外,您可以将PHP配置为在需要时自动启动会话(请参见上面链接的手册中的更多详细信息),但是请记住,即使在不依赖会话数据的页面上,这也会对性能产生影响./p>

With the foreach loop I'm trying to connect to my database and display in a list the products that have been added to the cart. Each product has a product ID which is correctly working and being stored in the session variable through the cart.php. I can't figure out how to connect to the database to display the information gathered about the product added - I also tried doing var_dump $SESSION['cart'] and its prints out null even after I use the "Add" button in cart.php.

<div class="row">
    <h4>Shopping Cart</h4>
            <?php

            foreach($_SESSION['cart'] as $proid => $proq) {

                // $proid is product id and $proq is quantity
                // use $proid to select the product detail from database

                }
            ?>
</div>
    <!--Below is my cart.php page-->
    <?php
    session_start();

    $productID = $_GET['product'];
    $action = $_GET['action'];

    switch($action) {

    case "add":
    $_SESSION['cart'][$productID]++;
    break;

    case "remove":
    $_SESSION['cart'][$productID]--;
    if($_SESSION['cart'][$productID] == 0) unset($_SESSION['cart'][$productID]);
    break;

    case "empty":
    unset($_SESSION['cart']);
    break;
    }
    header("Location: browse.php");


    ?>

解决方案

Based on your explanation, you are trying to retrieve data from a session value to populate a database query.

However, when your for loop executes, you have not de-serialized the session data into memory (so it cannot be accessed and you get null values).

You need to start the session before your for loop:

session_start();
foreach($_SESSION['cart'] as $proid => $proq) {

Please see more information in the php manual

Also, you can configure PHP to start the session automatically if desired (see more details in the manual linked above), however keep in mind that this will have a performance impact even on pages which do not rely on session data.

这篇关于使用PHP在购物车中显示商品清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆