将多个项目存储到会话中

时间:2016-07-22 20:58:27

标签: php arrays mysqli

我正在尝试将多个项目存储到会话中。我发布了一个类似的表单中的项目:

<?php
$product_id = $_GET['product_id'];
$sql = "SELECT * FROM Products WHERE product_id = $product_id";
    $result = $conn->query($sql);

    if($result->num_rows > 0){
        while($row = $result->fetch_assoc()){
            echo '<img src=' . $row['product_img'] . ' />';
            echo '<div class="title">'. $row['product_name'] .'</div>';
            echo '<div class="title">'. $row['product_price'] .'</div>';
            echo '<div class="title">'. $row['product_sku'] .'</div>';
            echo '<div class="title">'. $row['product_description'] .'</div>';
            echo '<form action="addToCart.php" method="post">
                    <input type="hidden" name="product_img" value="' . $row['product_img'] . '" />
                    <input type="hidden" name="product_id" value="' . $row['product_id'] . '" />
                    <input type="hidden" name="product_name" value="' . $row['product_name'] . '" />
                    <input type="hidden" name="product_price" value="' . $row['product_price'] . '" />
                    <input type="hidden" name="product_sku" value="' . $row['product_sku'] . '" />
                    <input type="hidden" name="product_description" value="' . $row['product_description'] . '" />
                    <input type="submit" name="Submit">
                  </form>';
        }
    } else{
    echo "0 Results";
    }
?>

addToCart.php如下所示:

<?php 
    if (isset($_POST['Submit'])) { 
        $_SESSION['product_img'] = $_POST['product_img'];
        $_SESSION['product_id'] = $_POST['product_id'];
        $_SESSION['product_name'] = $_POST['product_name'];
        $_SESSION['product_price'] = $_POST['product_price'];
        $_SESSION['product_sku'] = $_POST['product_sku'];
        $_SESSION['product_description'] = $_POST['product_description'];
    } 
?>

所以上面现在将项目设置为会话(我相信)。然后我从basket.php

中的存储数据输出项目
<?php echo '<img src=' . $_SESSION['product_img'] . ' />'; ?>
<?php echo $_SESSION['product_id'];?>
<?php echo $_SESSION['product_name'];?>
<?php echo $_SESSION['product_price'] ?>
<?php echo $_SESSION['product_sku']; ?>
<?php echo $_SESSION['product_description']; ?>

我遇到的问题是每当我添加另一个项目时它会覆盖前一个项目?我知道我需要使用一个数组来存储多个项目,但我尝试了一些不同的东西,但我很难理解。

2 个答案:

答案 0 :(得分:1)

您可以使用[]将它们存储在数组中:

$_SESSION['product_img'][] = $_POST['product_img'];
$_SESSION['product_id'][] = $_POST['product_id'];
$_SESSION['product_name'][] = $_POST['product_name'];
$_SESSION['product_price'][] = $_POST['product_price'];
$_SESSION['product_sku'][] = $_POST['product_sku'];
$_SESSION['product_description'][] = $_POST['product_description']

现在,尝试查看每个数组中的内容:

var_dump($_SESSION['product_id']);

答案 1 :(得分:1)

可以将会话数据存储为嵌套数组。在您的情况下,因为它是购物车,您可能想要使用产品的ID来构建您的数组;这样你就可以确定没有任何产品可以覆盖另一个产品(只要它们不具有相同的product_id值....下面是一个如何做到这一点的例子:

<?php   
    //FIRST CHECK IF SESSION EXIST BEFORE STARTING IT:
    if (session_status() == PHP_SESSION_NONE  || session_id() == '') {
        session_start();
    }
    if (isset($_POST['Submit'])) {
        $product_id             = htmlspecialchars(trim($_POST['product_id']));
        $product_img            = htmlspecialchars(trim($_POST['product_img']));
        $product_sku            = htmlspecialchars(trim($_POST['product_sku']));
        $product_name           = htmlspecialchars(trim($_POST['product_name']));
        $product_price          = htmlspecialchars(trim($_POST['product_price']));
        $product_description    = htmlspecialchars(trim($_POST['product_description']));

        if(!isset($_SESSION['products'])){
            $_SESSION['products'] = array();
        }
        if(!isset($_SESSION['products'][$product_id])){
            $_SESSION['products'][$product_id] = array();
        }
        $_SESSION['products'][$product_id]['product_id']            = $product_id;
        $_SESSION['products'][$product_id]['product_img']           = $ $product_img;
        $_SESSION['products'][$product_id]['product_name']          = $product_name;
        $_SESSION['products'][$product_id]['product_price']         = $product_price;
        $_SESSION['products'][$product_id]['product_sku']           = $product_sku;
        $_SESSION['products'][$product_id]['product_description']   = $product_description;     
    } 

在其他任何地方(也许是另一个脚本),您可以像这样访问您的数据:

<?php

    $output             = "";
    $storedProductData  = $_SESSION['products'];
    if(!empty($storedProductData)){
        foreach($storedProductData as $product_id=>$productData){
            $output .= "<img src='{$productData['product_img']}' />"                            . PHP_EOL;
            $output .= "<span id='generic-class'>{$productData['product_id']}</span>"           . PHP_EOL;
            $output .= "<span id='generic-class'>{$productData['product_name']}</span>"         . PHP_EOL;
            $output .= "<span id='generic-class'>{$productData['product_price']}</span>"        . PHP_EOL;
            $output .= "<span id='generic-class'>{$productData['product_sku']}</span>"          . PHP_EOL;
            $output .= "<span id='generic-class'>{$productData['product_description']}</span>"  . PHP_EOL;
        }
    }