php访问会话数组

时间:2016-08-18 14:28:05

标签: php arrays session

我仍然相对较新的php和这个网站,所以我现在道歉!这让我发疯,我正在尝试添加一个数组到会话状态的购物车我从不同的代码拼凑在一起。 ...

    $_SESSION['cart_items'] = 
array(
       'product_name' => $name,
       'productId' => $id,
       'quantity' => 1
);

^这是它添加到会话状态的部分,这可以正常工作,因为我是这样的

Array ( [product_name] => The Ned Rose [productId] => 1 [quantity] => 1 )

这是我无法开始工作的。我如何访问产品ID,以便我可以在SQL查询中使用它们来获取数据以填充购物车......

if(count($_SESSION['cart_items'])>0){
$ids = "";
foreach($_SESSION['cart_items']['productId'] as $id=>$value){
    $ids = $ids . $id . ",";

}

谢谢,

在这里编辑CART PAGE 谁能看到我哪里错了?

<?php
session_start();
$page_title="Cart";
include 'mysql.php';
print_r($_SESSION['cart_items']);
$action = isset($_GET['action']) ? $_GET['action'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";

if($action=='removed'){
echo "<div class='alert alert-info'>";
echo "<strong>{$name}</strong> was removed from your cart!";
echo "</div>";
}

else if($action=='quantity_updated'){
echo "<div class='alert alert-info'>";
    echo "<strong>{$name}</strong> quantity was updated!";
echo "</div>"; 
}
if(count($_SESSION['cart_items'])>0){
$ids = "";
$ids = array_keys($_SESSION['cart_items']);
foreach($_SESSION['cart_items'][$id] as $key=>$value){
    $ids = $ids . $id . ",";
}


// remove the last comma
$ids = rtrim($ids, ',');

//start table
echo "<table class='table table-hover table-responsive table-bordered'>";

    // our table heading
    echo "<tr>";
        echo "<th class='textAlignLeft'>Product Name</th>";
        echo "<th>Price (GBP)</th>";
        echo "<th>Action</th>";
    echo "</tr>";

    $query = "SELECT prodID, prodName, prodPrice FROM product_tbl WHERE prodID IN ({$ids}) ORDER BY prodName";
    $result = mysqli_query($db, $query);
    $total_price=0;
    while ($row = mysqli_fetch_assoc($result)){
        extract($row);

        echo "<tr>";
            echo "<td>".$row['prodName']."</td>";
            echo "<td>£".$row['prodPrice']."</td>";
            echo "<td>";
                echo "<a href='remove_from_cart.php?id=".$row['prodID']."&name=".$row['prodName']."' class='btn btn-danger'>";
                    echo "<span class='glyphicon glyphicon-remove'></span> Remove from cart";
                echo "</a>";
            echo "</td>";
        echo "</tr>";

        $total_price+=$row['prodPrice'];
    }

    echo "<tr>";
            echo "<td><b>Total</b></td>";
            echo "<td>£{$total_price}</td>";
            echo "<td>";
                echo "<a href='#' class='btn btn-success'>";
                    echo "<span class='glyphicon glyphicon-shopping-cart'></span> Checkout";
                echo "</a>";
            echo "</td>";
        echo "</tr>";

echo "</table>";

}

否则{     echo“”;         在您的购物车中回显“未找到产品!点击此处返回商店”;     echo“”; }

&GT;

3 个答案:

答案 0 :(得分:1)

将产品存储在购物车中时,使用$ id作为KEY值:

$_SESSION['cart_items'][$id] = 
array(
       'product_name' => $name,
       'productId' => $id,
       'quantity' => 1
);

然后你可以在foreach循环中使用ID:

if( count($_SESSION['cart_items']) > 0){
    foreach($_SESSION['cart_items']['productId'] as $id => $value){
       // Get data for this product

    }
}

答案 1 :(得分:0)

您可以使用以下

$_SESSION['cart_items'][] = 
array(
       'product_name' => $name,
       'productId' => $id,
       'quantity' => 1
);

或(这将更新购物车中之前添加的产品ID)

$_SESSION['cart_items'][$id] = 
array(
       'product_name' => $name,
       'productId' => $id,
       'quantity' => 1
);


if(count($_SESSION['cart_items'])>0){
$ids = "";
foreach($_SESSION['cart_items'] as $id=>$value){
    $ids = $ids . $id . ",";

 }
}

答案 2 :(得分:0)

您可以向阵列添加产品,这样您就不需要直接存储“productId”值。

// Set data
$_SESSION['cart_items'][$id] = Array('name'=>$name,'qty'=>1);

// Show session content
foreach($_SESSION['cart_items'] as $id=>$props){
    echo 'id='.$id.'<br />';
    echo 'name='.$props['name'].'<br />';
    echo 'qty='.$props['qty'];
}

// Collect ids (result is an array of ids)
$ids = array_keys($_SESSION['cart_items'];

// Use $ids in a query
$sql = "SELECT * FROM your_table WHERE id_field IN('".implode("','",$ids)."')";