arrays - Increment SESSION amount PHP -
i'm using sessions populate simple shopping cart but, i'm having problems getting itemamount increment if item_id exists within session['cart'].
i have been trying increment here: $itemamount = $eachitem['itemamount']; but, can't working.
this have @ moment;
updated; code better naming, still need getting cart_item_amount increment, @ moment creates new item every time instead of incrementing cart_item_amount.
<?php if (!isset($_session)) { session_start(); } // create cart session if(!isset($_session['cart'])) { $_session['cart'] = array(); } ?> <?php // if posts 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'] $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 2 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 $key => $value) { // split 2 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 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 extras array 'cart_item_extras' => $item_extras ); // add new row 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] => ) ) )
if($eachitem['itemid'] = $_post['item_id']) { = not comparison operator, see http://php.net/manual/en/language.operators.comparison.php
should
if($eachitem['itemid'] == $_post['item_id']) {
Comments
Post a Comment