-1

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
            )

    )
Saad Suri
  • 1,352
  • 1
  • 14
  • 26

1 Answers1

1

Arrays are not passed by reference in foreach ($_SESSION['cart'] as $key => $item), therefore, when you set $item['food_quantity'] = $_POST['quantity'] inside the loop, the $_SESSION['cart'][$key]['food_quantity'] is not modified. There are 2 solutions, set the $_SESSION['cart'][$key]['food_quantity'] directly, or pass the $item in $_SESSION['cart'] by reference.

Set the $_SESSION['cart'][$key]['food_quantity'] directly:

if (isset($_POST['apply'] {
    foreach ($_SESSION['cart'] as $key => $item) {
        // Updating quantity.
        $_SESSION['cart'][$key]['food_quantity'] = $_POST['quantity'];
    }
}

Pass the $item in $_SESSION['cart'] by reference:

if (isset($_POST['apply'] {
    foreach ($_SESSION['cart'] as $key => &$item) {
        // Updating quantity.
        $item['food_quantity'] = $_POST['quantity'];
    }
}
Rendy Eko Prastiyo
  • 1,068
  • 7
  • 16
  • If you use pass by reference, note the difference between `foreach ($_SESSION['cart'] as $key => $item)` and `foreach ($_SESSION['cart'] as $key => &$item)` (add `&` before `$item`, which means `$item` is passed by reference of `$_SESSION['cart'][$key]`). – Rendy Eko Prastiyo Feb 13 '17 at 05:18
  • Yes I tried this and it works. but when i refresh the page it works. any idea or would i post my whole code of cart ? – Saad Suri Feb 13 '17 at 05:27
  • What do you mean with `but when i refresh the page it works` ? – Rendy Eko Prastiyo Feb 13 '17 at 05:29
  • When i update the session array on post it updates but when i refresh the page then i get my desired result. – Saad Suri Feb 13 '17 at 05:37
  • And second thing it only updates the one item quantity at a time not every item – Saad Suri Feb 13 '17 at 05:40
  • Do you mean when you post an update that code works, but when you refresh page (with `resend data`) it re-updated the session? You need to use `redirect` after updating the session to avoid it. – Rendy Eko Prastiyo Feb 13 '17 at 05:42
  • Yeah i handle that. but it is not updating whole array let me put the edit then you can re evaluate – Saad Suri Feb 13 '17 at 05:43
  • I don't know why put it is displaying 3 array. Quite stuck at it. will you review the edits – Saad Suri Feb 13 '17 at 05:48
  • Where does $_SESSION['cart'] array keys come from? You seem to miss-understand in `$key`, which I don't think it is the item id. – Rendy Eko Prastiyo Feb 13 '17 at 05:55
  • $_session['cart'] array comes from add to cart page which i already post in question. secondly i pass $key as a hidden element and retrieve in update. – Saad Suri Feb 13 '17 at 05:57
  • You mention only the update action, not the add action when the $_SESSION['cart'] is empty: `if (isset($_SESSION['cart'])) {`. I need to know the `else` part. – Rendy Eko Prastiyo Feb 13 '17 at 05:59
  • there is no else part. First I'm displaying all cart items then i'm checking if it updates so update the cart. why should i need else part ? – Saad Suri Feb 13 '17 at 06:01
  • If you define the array keys as default incremental array keys, you have to find the key by looping through it. I have updated the answer to fit your need. Could you update your question with the array values of your posted data? Eg. using `var_dump($_POST)`. – Rendy Eko Prastiyo Feb 13 '17 at 07:02
  • It's been finally done. I need to use else thanks for your guidance marking it as a correct answer. – Saad Suri Feb 13 '17 at 07:12