我还是比较新的PHP和本网站,所以我现在道歉!这是让我疯狂,我想添加一个数组会议状态购物车我从不同的拼凑的码位....php访问会议阵列
$_SESSION['cart_items'] =
array(
'product_name' => $name,
'productId' => $id,
'quantity' => 1
);
^这是它添加到会话状态的一部分,因为我printr和它出来像这样
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 . ",";
}
感谢,
编辑在这里购物车页 任何人都可以看到我错了?
<?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“”; }
>
$ _SESSION [“cart_items”] [“的productId”]会得到你的车ID –
,但我想还可以添加你很可能是最好的添加篮下线到一个数据库表,而不是一个会话 –
$ _SESSION ['cart_items'] ['productId']的内容似乎不是一个数组,为什么使用“foreach”呢? 以及,我认为你应该看看implode函数(http://php.net/manual/fr/function.implode.php) – Steven