First of all I've seen multiple threads and questions. but still I'm stuck at it. so what I'm doing
Firstly I'm filling my session array through this on add to cart page
$_SESSION['food_id']=$get_id;
$_SESSION['food_name']=$get_name;
$_SESSION['food_price']=$get_price;
$_SESSION['food_image']=$get_image;
$_SESSION['food_quantity']=1;
$_SESSION['food_discount']=$get_discount;
$cart = array (
'food_id' => $_SESSION['food_id'],
'food_name' => $_SESSION['food_name'],
'food_price'=> $_SESSION['food_price'],
'food_image'=> $_SESSION['food_image'],
'food_quantity' => $_SESSION['food_quantity'],
'food_discount' => $_SESSION['food_discount']
);
$_SESSION['cart'][] = $cart;
Then I'm retrieving and displaying the cart array in cart.php page through this
$total="";
$item_total="";
$item_discount="";
$item_total_quantity="";
$order_total="";
$total_discount="";
if(isset($_SESSION['cart'])){
foreach ($_SESSION['cart'] as $key => $item) {
//Get Cart Subtotal Before calculating discount
$total += $item['food_price'];
$item_total_quantity=$item['food_price']*$item['food_quantity'];
//Get Item discount
$item_discount = ($item['food_discount']/100)*$item_total_quantity;
//Get Item total
$item_total=$item_total_quantity-$item_discount;
//Get total quantity
$food_quantity=$item['food_total_quantity'];
$order_total += $item_total;
$total_discount += $item_discount;
?>
<tr>
<td><a href="menu_single.php?q=<?php echo $item['food_id']; ?>"><img src="<?php echo 'img/menu/'.$item['food_image']; ?>" alt=""><?php echo $item['food_name']; echo $key; ?></a> </td>
<td><?php echo $item['food_price']; ?> $</td>
<td>
<span class="total"> <?php echo $item['food_quantity']; ?></span>
<!-- End input group minus & plus --></td>
<td><?php echo $item['food_discount']; ?> %</td>
<td><span class="total"> <?php echo $item_total; ?> $ </span> <a class="pull-right" href="#"><i class="fa fa-times"></i></a></td>
</tr>
<?php }} ?>
Now I'm stuck when quantity item changed and button pressed for post back I need to update quantity based on that value. What I'm doing currently is.
if(isset($_POST['apply']{
foreach ($_SESSION['cart'] as $key => $item) {
//Updating Quantity
$item['food_quantity']=$_POST['quantity'];
}
But I'm not getting my desired result It always display the previous quantity which is set to be 1. I'm about to hit my head on wall right now. Any suggestions would be helpful.
Edit #1 After reading Rendy Eko Prastiyo's answer. Now I'm able to update session array but it only updates 1 item here is my code
if(isset($_POST['apply'])){
//$cart[] = $_SESSION['cart'];
foreach ($_SESSION['cart'] as $key => $item) {
echo $_POST['key'];
$_SESSION['cart'][$_POST['key']]['food_quantity'] = $_POST['quantity'];
//Get Cart Subtotal Before calculating discount
$total += $item['food_price'];
$item_total_quantity=$item['food_price']*$item['food_quantity'];
//Get Item discount
$item_discount = ($item['food_discount']/100)*$item_total_quantity;
//Get Item total
$item_total=$item_total_quantity-$item_discount;
//Get total quantity
$food_quantity=$item['food_total_quantity'];
$order_total += $item_total;
$total_discount += $item_discount;
echo "<pre>";
print_r($_SESSION['cart']);
echo "</pre>";
}
}
Here is print_r result
Array
(
[0] => Array
(
[food_id] => 5
[food_name] => New Item
[food_price] => 14
[food_image] => dishes2.jpg
[food_quantity] => 9
[food_discount] => 10
[food_total_quantity] => 21
)
[1] => Array
(
[food_id] => 5
[food_name] => New Item
[food_price] => 14
[food_image] => dishes2.jpg
[food_quantity] => 1
[food_discount] => 10
[food_total_quantity] => 21
)
[2] => Array
(
[food_id] => 5
[food_name] => New Item
[food_price] => 14
[food_image] => dishes2.jpg
[food_quantity] => 1
[food_discount] => 10
[food_total_quantity] => 21
)
)
Array
(
[0] => Array
(
[food_id] => 5
[food_name] => New Item
[food_price] => 14
[food_image] => dishes2.jpg
[food_quantity] => 9
[food_discount] => 10
[food_total_quantity] => 21
)
[1] => Array
(
[food_id] => 5
[food_name] => New Item
[food_price] => 14
[food_image] => dishes2.jpg
[food_quantity] => 1
[food_discount] => 10
[food_total_quantity] => 21
)
[2] => Array
(
[food_id] => 5
[food_name] => New Item
[food_price] => 14
[food_image] => dishes2.jpg
[food_quantity] => 1
[food_discount] => 10
[food_total_quantity] => 21
)
)
Array
(
[0] => Array
(
[food_id] => 5
[food_name] => New Item
[food_price] => 14
[food_image] => dishes2.jpg
[food_quantity] => 9
[food_discount] => 10
[food_total_quantity] => 21
)
[1] => Array
(
[food_id] => 5
[food_name] => New Item
[food_price] => 14
[food_image] => dishes2.jpg
[food_quantity] => 1
[food_discount] => 10
[food_total_quantity] => 21
)
[2] => Array
(
[food_id] => 5
[food_name] => New Item
[food_price] => 14
[food_image] => dishes2.jpg
[food_quantity] => 1
[food_discount] => 10
[food_total_quantity] => 21
)
)