2

我已经尝试了好几天(并在这里阅读了大约 20 篇关于 SO 的帖子),并且似乎可以找到解决我问题的方法。我需要使用 foreach 循环遍历会话数组以检索三个(可能更多)键值对,我还需要在后续查询中使用它们。

我需要这个,因为我们商品的价格取决于多个因素。

我最初使用 for 循环创建了一个购物车。这可以满足我的需要(您将在下面看到),但是在尝试删除项目时遇到了麻烦。我从关于 SO 的问题帖子中了解到,我必须使用 foreach 循环。我相应地对其进行了更改并且它有效,但只有当我有一个键值对时。

有没有办法通过 foreach 循环来实现我所需要的?如果是这样,怎么做?

这是我的购物车会话数组中需要的内容:

$_SESSION['shop_cart'][$prodid]=$quantity;
$_SESSION['shop_cart']['season_price']=$season_priceid;

或者?

$_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" }
4

1 回答 1

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;
}
于 2013-06-10T17:54:06.013 回答