2013-04-06 45 views
0

我正在使用會話來填充簡單的購物車,但是,如果在session['cart']內已存在item_id,我會在itemAmount增加時遇到一些問題。遞增會話金額PHP

我一直在嘗試在這裏增加:$itemAmount = $eachItem['itemAmount'];但是,無法讓它工作。

這是我目前所擁有的;

UPDATED;具有更好命名的代碼,我仍然需要有關獲取cart_item_amount增量的幫助,此時它每次都會創建一個新項目,而不是遞增cart_item_amount。

<?php 
if (!isset($_SESSION)) { 
    session_start(); 
} 
// create the cart session 
if(!isset($_SESSION['cart'])) { 
    $_SESSION['cart'] = array(); 
} 
?> 
<?php 
// if all posts are fine 
if(isset($_POST['item_id'])) { 

    // set post values 
    $post_item_id = $_POST['item_id']; 
    $post_item_name = $_POST['item_name']; 
    $post_options = $_POST['item_options']; 
    $post_item_extras = $_POST['item_extras']; 

    // set initial item amount 
    $item_total = 1; 

    foreach($_SESSION['cart'] as $key => $value) { 

     if($value['cart_item_id'] == $post_item_id) { 

      $value['cart_item_amount']++; 
      $item_total = $value['cart_item_amount']; 

     }else{ 

     } 
    } 
    // if option selected 
    if(isset($post_options)) { 

     // split to get two separate values 
     list($post_item_cost, $post_option_name) = explode("-", $post_options, 2); 

    } 

    // loop through extras   
    if(isset($post_item_extras)) { 

     foreach($post_item_extras as $key => $value) { 

      // split to get two separate values 
      list($post_extra_cost, $post_extra_name) = explode("-", $value, 2); 

      // create extras array 
      $item_extras[] = array(
       'cart_extra_cost' => $post_extra_cost, 
       'cart_extra_name' => $post_extra_name 
      ); 

     } 
    } 

    // add each item to an array 
    $cart_row = array(
     'cart_item_id' => $post_item_id, 
     'cart_item_name' => $post_item_name, 
     'cart_option_name' => $post_option_name, 
     'cart_item_cost' => $post_item_cost, 
     'cart_item_amount' => $item_total, 
     // add the extras array 
     'cart_item_extras' => $item_extras 
    ); 

     // add new row to the session Cart 
     $_SESSION['cart'][] = $cart_row; 
} 
?> 

Array 
(
    [cart] => Array 
     (
      [0] => Array 
       (
        [cart_item_id] => 76 
        [cart_item_name] => Cheese Burger 
        [cart_option_name] => Small 
        [cart_item_cost] => 1.00 
        [cart_item_amount] => 1 
        [cart_item_extras] => 
       ) 

      [1] => Array 
       (
        [cart_item_id] => 76 
        [cart_item_name] => Cheese Burger 
        [cart_option_name] => Small 
        [cart_item_cost] => 1.00 
        [cart_item_amount] => 2 
        [cart_item_extras] => 
       ) 

      [2] => Array 
       (
        [cart_item_id] => 76 
        [cart_item_name] => Cheese Burger 
        [cart_option_name] => Small 
        [cart_item_cost] => 1.00 
        [cart_item_amount] => 3 
        [cart_item_extras] => 
       ) 

     ) 

) 

回答

1
if($eachItem['itemID'] = $_POST['item_id']) { 

=不是比較操作,見 http://php.net/manual/en/language.operators.comparison.php

應該

if($eachItem['itemID'] == $_POST['item_id']) { 
+1

也更好的名字,$如果還有一個$ ITEMID和$ ITEM_NAME和$ ITEMNAME ... – joschua011 2013-04-06 10:42:54

+0

由於錯過了,得到它現在的工作ITEM_ID並不明顯。我即將改變命名,我明白你的意思。 – monsterboy 2013-04-06 10:51:29

+0

發言太快,已更新我的帖子 – monsterboy 2013-04-06 11:41:55

0

得到它只需使用這方面的工作;

foreach($_SESSION['cart'] as $key => $value) { 

    if($value['cart_item_id'] == $post_item_id) { 

     $_SESSION['cart'][$key]['cart_item_amount']++; 

     return false; 

}