基于会话的购物车

时间:2015-01-12 05:41:04

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

最初,我通过id功能将产品array_push()传递过来,从显示项目的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']& {{1} }),我只是不知道如何访问它...

如何在推送数组中添加更多信息,但有些人如何定义它以便我可以抓取id查询$sql,获取产品信息,但也可以访问size&我的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 } ?>

1 个答案:

答案 0 :(得分: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