makamo66 Posted January 11, 2020 Share Posted January 11, 2020 The var_dump of the $cart_items array is supposed to show an array of $cart_items with the quantities added together for the same id but it doesn't even show an array, it just shows one value. session_start(); $cart_items = $_SESSION["cart_items"]; if ( $cart_items == null ) { $cart_items = array(); } if ( isset($_REQUEST["element_id"]) ) { $id = $_REQUEST["element_id"]; } if ( isset($_REQUEST["quantity"]) ) { $quantity = $_REQUEST["quantity"]; } if ( isset($id) && isset($quantity) ) { if (isset($cart_items[$id])) { $cart_items[$id] += $quantity; } else { $cart_items[$id] = $quantity; } } var_dump($cart_items); Quote Link to comment https://forums.phpfreaks.com/topic/309823-var_dump-of-an-array-shows-just-one-variable/ Share on other sites More sharing options...
requinix Posted January 11, 2020 Share Posted January 11, 2020 Exactly what does it show? Quote Link to comment https://forums.phpfreaks.com/topic/309823-var_dump-of-an-array-shows-just-one-variable/#findComment-1573359 Share on other sites More sharing options...
Barand Posted January 11, 2020 Share Posted January 11, 2020 Do not use $_REQUEST. Use either $_POST or $_GET depending on your form's method attribute. (In this case, as you are updating the cart data, it should be POST.) session_start(); $cart_items = $_SESSION["cart_items"] ?? []; $id = $_POST["element_id"] ?? 0; $quantity = $_POST["quantity"] ?? 0; if ($id && $quantity) { if (!isset($cart_items[$id])) { $cart_items[$id] = 0; } $cart_items[$id] += $quantity; } var_dump($cart_items); Quote Link to comment https://forums.phpfreaks.com/topic/309823-var_dump-of-an-array-shows-just-one-variable/#findComment-1573360 Share on other sites More sharing options...
mac_gyver Posted January 11, 2020 Share Posted January 11, 2020 i'm going with the code never copies the (unnecessary) $cart_items variable back to the session variable, so the code starts over each time it gets executed. just use $_SESSION["cart_items"] everywhere and forget about the $cart_items variable. 1 Quote Link to comment https://forums.phpfreaks.com/topic/309823-var_dump-of-an-array-shows-just-one-variable/#findComment-1573372 Share on other sites More sharing options...
makamo66 Posted January 12, 2020 Author Share Posted January 12, 2020 Thank you for your helpful input. I implemented the code that you provided and used $_SESSION["cart_items"] instead of $cart_items and now it works! Quote Link to comment https://forums.phpfreaks.com/topic/309823-var_dump-of-an-array-shows-just-one-variable/#findComment-1573387 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.