php访问会话数组
我对php和这个网站还比较陌生,所以我现在道歉!这让我发疯了,我正试图为一个购物车的会话状态添加一个数组,我正在用不同的代码拼接在一起php访问会话数组,php,arrays,session,Php,Arrays,Session,我对php和这个网站还比较陌生,所以我现在道歉!这让我发疯了,我正试图为一个购物车的会话状态添加一个数组,我正在用不同的代码拼接在一起 $_SESSION['cart_items'] = array( 'product_name' => $name, 'productId' => $id, 'quantity' => 1 ); ^这是它添加到会话状态的部分,在i printr中运行良好,结果如下 Array ( [produ
$_SESSION['cart_items'] =
array(
'product_name' => $name,
'productId' => $id,
'quantity' => 1
);
^这是它添加到会话状态的部分,在i 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>";
在购物车中存储产品时,使用$id作为键值:
$_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
}
}
您可以使用以下内容
$_SESSION['cart_items'][] =
array(
'product_name' => $name,
'productId' => $id,
'quantity' => 1
);
或(这将更新以前在购物车中添加的产品id)
您可以将产品添加到数组中,这样就不需要直接存储“productId”值
//设置数据
$\会话['cart\u items'][$id]=Array('name'=>$name,'qty'=>1);
//显示会话内容
foreach($\会话['cart\u items']作为$id=>$props){
回显“id=”.$id.
;
echo'name='.$props['name'].
;
回显“数量=”.$props[“数量”];
}
//收集ID(结果是一个ID数组)
$ids=数组_键($_会话['cart_items'];
//在查询中使用$id
$sql=“从(“.”中的id_字段所在的_表中选择*。内爆(“,”,$ids)。”);
$\u SESSION['cart\u items']['productId']将获得您的购物车ID,但我想补充一点,您最好将篮子行添加到DB表中,而不是会话。$\u SESSION['cart\u items']['productId']的内容似乎不是数组,为什么要使用“foreach”还有,我想你应该看看内爆函数()谢谢大家,仍然没有运气,仍然无法打印出ID………你的购物车只包含一个项目,因为$\u SESSION['cart']=…
将始终覆盖以前包含的内容。你需要有一个购物车项目数组,例如$\u SESSION['cart'][$productID]=array(…details…
我的大部分成功都是侥幸成功的,我使用了这个答案,在会话结束时,我没有回显,而是将ids+=id.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 . ",";
}
}
// 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)."')";