我已经尝试了几天(并且在这里阅读了大约20篇文章)并且似乎找到了我的问题的解决方案。我需要使用foreach循环遍历会话数组来检索三个(可能更多)键值对,我还需要在后续查询中使用它。
我需要这个,因为我们的商品价格取决于多个因素。
我最初使用for循环创建了一个购物车。这适用于我需要的东西(你会在下面看到)但我在尝试删除项目时遇到了麻烦。我从这里的问题帖子中了解到,我必须使用foreach循环。我相应地更改了它并且它有效,但是只有当我有一个键值对时。
有没有办法通过foreach循环实现我需要的东西?如果是这样,怎么样?
这是我在购物车会话数组中需要的内容:
$_SESSION['shop_cart'][$prodid]=$quantity;
$_SESSION['shop_cart']['season_price']=$season_priceid;
OR?
$_SESSION['shop_cart']['prodid']=$prodid;
$_SESSION['shop_cart']['quantity']=$quantity;
$_SESSION['shop_cart']['season_price']=$season_priceid; //This is the third variable I need
这是我的代码,包含foreach循环(包含前一个for循环的注释/注释),但只能使用一个键值对。
$prodid = $_POST['prodid'];
$quantity = $_POST['quantity'];
$season_priceid=$_POST['season_priceid'];
$empty= $_POST['empty'];
$removed = $_POST['remove'];
$counter= $_POST['counter'];
if(isset($prodid))
{
$_SESSION['shop_cart'][$prodid] = $quantity;
}
if (isset($empty ))
{
unset($_SESSION['shop_cart']);
}
if (!isset($_SESSION['shop_cart']))//Checking if Session is Set or empty
{
unset($_SESSION['shop_cart']);
echo "Cart is empty";
}
else
{
if(count($_SESSION['shop_cart'])==NULL || count($_SESSION['shop_cart'])==0 )
{
echo "Cart is empty";
}
}
if (isset($removed))
{
foreach ($_SESSION['shop_cart'] as $cart_line_item => $item)
{
if($prodid==$item['prodid'])
{
unset($_SESSION['shop_cart'][$cart_line_item]);
continue;
}
}
}
$_SESSION['shop_cart'][$prodid] = $quantity;
$total=0; //For Price
if (isset($_SESSION['shop_cart']))
{
/*
for($i=0;$i<count($_SESSION['shop_cart']);$i++) //The previous FOR LOOP works but cannot delete items correctly
*/
foreach ($_SESSION['shop_cart'] as $prodid => $quantity)
{
/* //This is how the cart-display array was created with the FOR LOOP. As you can see here, I need three values to show in my cart array
$prodid=$_SESSION['shop_cart'][$i]['prodid'];
$quantity=$_SESSION['shop_cart'][$i]['quantity'];
$season_priceid =$_SESSION['shop_cart'][$i]['season_priceid'];
*/
$query = "SELECT prodid, description, price FROM products, seasons WHERE prodid = $prodid AND season_id= $season_priceid"; //###I NEED THIS THIRD VARIABLE next variable to get the right price)###
$result = mysqli_query($hook, $query);
$row = mysqli_fetch_assoc($result);
$prodid = $row['prodid'];
$price = $row['price'];
$description= $row['description'];
$subtotal = $price * $quantity;
$total += $subtotal;
echo "$description($prodid)---Quantity: $quantity--- $$price";
echo "<form action=\"$_SERVER[SCRIPT_NAME]\" method=\"post\" >";
echo "<INPUT TYPE=\"submit\" name=\"remove\" VALUE=\"Remove\">";
echo "<input type=\"hidden\" name=\"prodid\" value=$prodid />\n";
echo "</FORM>";
echo "--------------------------<br/><br/>";
}
}
echo "TOTAL $total <br/><br/>";
echo '<pre>'. var_dump( $_SESSION['shop_cart']).'<pre/>';
echo "<br/>";
echo "<form action=\"$_SERVER[SCRIPT_NAME]\" method=\"post\" >";
echo "<INPUT TYPE=\"submit\" name=\"empty\" VALUE=\"Empty Cart \">";
echo "</FORM>";
尝试以下但没有任何作用:
foreach ($shop_cart as $key =>$val) {
$season_priceid = $key['season_priceid'];
//OR
$season_priceid = $val['season_priceid'];
}
///////////
foreach ($shop_cart as $val) {
$season_priceid = $val['season_priceid'];
}
///////////
foreach ($shop_cart as $cart_arrays) {
$season_priceid =$shop_cart['season_priceid'];
}
如果这有助于我的数组使用不同的循环看起来像:
FOR LOOP ARRAY
array(3) { [0]=> array(3) { ["prodid"]=> string(2) "13" ["quantity"]=> string(1) "1" ["season_priceid"]=> NULL } [1]=> array(3) { ["prodid"]=> string(1) "6" ["quantity"]=> string(1) "1" ["season_priceid"]=> NULL } [2]=> array(3) { ["prodid"]=> string(1) "2" ["quantity"]=> string(1) "1" ["season_priceid"]=> NULL } }
FOREACH ARRAY (but I need a third element/value : "season_priceid")
array(3) { [2]=> string(1) "1" [6]=> string(1) "1" [3]=> string(1) "1" }
答案 0 :(得分:1)
因此,您的代码中的真正问题是您的会话中的season_priceid为NULL。这意味着您设置season_priceid的代码无法正常工作。由于您的代码没有显示您的会话设置,我无法帮助您,但以下是我建议您设置购物车会话数据的方式:
'shop_cart' => array (
'14' => array ( // 14 is product id
'quantity' => 1,
'season_priceid' => 1
),
'15' => array ( // 15 is product id
'quantity' => 1,
'season_priceid' => 1
),
)
这样你可以像这样循环购物车:
foreach ($_SESSION['shop_cart'] as $prodid => $product) {
echo $prodid; // product id
echo $product['quantity']; // quantity
echo $product['season_priceid']; // season price id
// to delete the product from the session
unset($_SESSION['shop_cart'][$prodid];
// to edit the product quantity
$_SESSION['shop_cart'][$prodid]['quantity'] = 2;
}