【问题标题】:session based shopping cart基于会话的购物车
【发布时间】:2015-01-12 05:41:04
【问题描述】:

最初我让它工作正常,通过 array_push() 函数传递产品 id,从 addtocart.phpShoppingcart.php 显示项目。但是当我在array_push()函数中添加更多变量时,除了单个产品id$_GET['id'],...在接收这个数组的下一页上,它给了我一个错误。

问题是: 在原始的$sql 查询中,它得到了id,它需要显示来自array_push() 的产品信息并将其显示到Shoppingcart.php,但是当我在push_array() 中添加更多变量/信息时,我得到了一个错误。因为它混淆了$sql 查询,因为WHERE id IN 子句......ID 仍然在那里,现在连同其他信息($_GET['size']$_GET['qty']),我只是不知道如何访问它...

& Qty 用于我的 while() 循环。

addtocart.php

array_push($_SESSION['cart'], $_GET['id']); //working MAIN
header('Location:shoppingCart.php');

How 2: array_push($_SESSION['cart'], $_GET['id'], $_GET['size'],  $_GET['qty']); 
//Not Working

shoppingcart.php

    <?php 
    $whereIn = implode(',', $_SESSION['cart']); //working MAIN
    $sql = " SELECT * FROM inventory WHERE id IN ($whereIn) "; ?>

 <?php while($row = mysql_fetch_array($result)) { ?>

<td  valign="top">
    <div id="sc_itemBox">
        <p class="sc_itemBox_iTEXT"><strong>SIZE:</strong> “”XL?? <em>(Extra Large??)</em></p>
        <div id="sc_itemBox_img"><img src="<?php echo $row['imgThumb'];?>" /></div>
        <p class=<p class="sc_itemBox_iTEXT"><strong>STYLE#</strong><?php echo $row['styleNUM']; ?> </p>
    </div>        
</td>
<?php } ?>

【问题讨论】:

  • 如果您需要更多信息来更好地了解这里发生的事情,请告诉我,我尽量做到清楚,以免混淆任何人。
  • 单引号是写成而不是'(即$_GET[‘size’])还是只是一个错字?
  • 好吧,我就是这么想的。
  • 是的,我打字很快,只是想弄清楚要点...... :)
  • 当你 print_r($_SESSION['cart']); 时,你的 $_SESSION['cart'] 是什么样子我很难把我的想法集中在你想要做的事情上,所以看到这个数组可能会对我有所帮助。我想我明白了,我认为你需要让你的 $_SESSION['cart'] 更复杂一点。

标签: php session while-loop shopping-cart array-push


【解决方案1】:

我认为让您的$_SESSION['cart'] 稍微复杂一点可能会奏效。尝试将您的 id 等分离到它们自己的数组中。可能是这样的。

addtocart.php

if(isset($_SESSION['cart'])) {
        // Store just ids
        // Use array_unique to filter this array
        $_SESSION['cart']['id'] = array_unique($_SESSION['cart']['id']);
        // Save new cart array with that items basics
        $_SESSION['cart'][$_GET['id']]['size'][] = $_GET['size'];
        $_SESSION['cart'][$_GET['id']]['qty'][]  = $_GET['qty'];
        header('Location:shoppingCart.php');
        exit;
    }

shoppingcart.php

// Implode just the id array, no other array in the cart session
// (make sure your addtocart checks for is_numeric so you don't get someone
// injecting sql junk into your db
$whereIn    =    implode(',', $_SESSION['cart']['id']);
$sql        =    "SELECT * FROM inventory WHERE id IN ($whereIn)";

编辑:这是一种方法,您可以将您的项目分解为尺寸(如果您要添加多个具有相同 ID 但尺寸和数量不同的项目:

    function AddToCart()
        {
            // Confirm that an id is being added
            // I am assuming there is an "add" trigger
            if(isset($_GET['add']) && is_numeric($_GET['id'])) {
                    // Create the item in the cart
                    // Record size
                    if(isset($_SESSION['cart'][$_GET['id']]['size'][$_GET['size']]['qty']))
                        // Notice here that if there is already this item in the cart
                        // with the exact same size, it will sum
                        $_SESSION['cart'][$_GET['id']]['size'][$_GET['size']]['qty']    +=  $_GET['qty'];
                    else
                        // If not in cart at this size, it will add qty
                        $_SESSION['cart'][$_GET['id']]['size'][$_GET['size']]['qty']    =   $_GET['qty'];
                }
        }

    // Fetch ids for your query 
    function FetchItems()
        {
            if(isset($_SESSION['cart'])) {
                    foreach($_SESSION['cart'] as $itemcode => $array) {
                            $items[]    =   $itemcode;
                        }

                    return (isset($items))? $items:false;
                }
        }

    // Start the session
    session_start();

    // Add to cart
    AddToCart();
    // This will fetch your ids for your query
    $mysqlIds   =   implode(",",FetchItems());

    echo '<pre>';
    print_r($mysqlIds);
    echo '</pre>'; ?>

    <!-- These are just for testing. Will generate different sizes and qty-->
    <a href="?add=true&id=1&size=<?php echo rand(1,12); ?>&qty=<?php echo rand(0,5); ?>">ID 1</a>
    <a href="?add=true&id=2&size=<?php echo rand(1,12); ?>&qty=<?php echo rand(0,5); ?>">ID 2</a>
    <a href="?add=true&id=3&size=<?php echo rand(1,12); ?>&qty=<?php echo rand(0,5); ?>">ID 3</a>

会给你:

// Session array after adding items to it.
Array
(
    [cart] => Array
        (
            [2] => Array
                (
                    [size] => Array
                        (
                            [21] => Array
                                (
                                    [qty] => 1
                                )

                            [9] => Array
                                (
                                    [qty] => 2
                                )

                            [8] => Array
                                (
                                    [qty] => 0
                                )

                            [7] => Array
                                (
                                    [qty] => 20
                                )

                            [2] => Array
                                (
                                    [qty] => 5
                                )

                        )

                )

            [3] => Array
                (
                    [size] => Array
                        (
                            [9] => Array
                                (
                                    [qty] => 3
                                )

                            [1] => Array
                                (
                                    [qty] => 0
                                )

                            [7] => Array
                                (
                                    [qty] => 4
                                )

                            [10] => Array
                                (
                                    [qty] => 6
                                )

                            [3] => Array
                                (
                                    [qty] => 20
                                )

                            [2] => Array
                                (
                                    [qty] => 10
                                )

                            [12] => Array
                                (
                                    [qty] => 2
                                )

                            [6] => Array
                                (
                                    [qty] => 10
                                )

                        )

                )

            [1] => Array
                (
                    [size] => Array
                        (
                            [11] => Array
                                (
                                    [qty] => 1
                                )

                            [3] => Array
                                (
                                    [qty] => 3
                                )

                            [2] => Array
                                (
                                    [qty] => 2
                                )

                        )

                )

        )

)

// This is for the ids for your mysql query
2,3,1

【讨论】:

  • 顺便说一句...它正在使用 id (thx)...我只是想弄清楚为什么要将第一个项目重复发布到会话中
  • 那么当你第一次点击添加时,它会在id数组中添加两个相同的id?
  • 我正在查看代码以查看问题所在,我做了一个 var dum... array(2) { ["id"]=> array(1) { [0]=> string(2) "15" } [15]=> array(2) { ["size"]=> array(1) { [0]=> string(1) "S" } ["qty"]=>数组(1) { [0]=> NULL } } }
  • 哦,不,这只是一个 id 数组,并将每个项目也保存到自己的数组中
  • 因为它在第一次插入时将我的购物车计数变为两个,我使用 count($_SESSION['cart']);这是获取购物车数量的正确方法吗?所以它基本上是在计算会话中的所有数组,还是我应该计算数量来获取购物车数量?
猜你喜欢
  • 2015-05-16
  • 2016-03-20
  • 1970-01-01
  • 1970-01-01
  • 2019-05-29
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
相关资源
最近更新 更多