Can someone help me out with this code: I am sending values via a form to a session array but it is not storing the values, so everytime I click the submit button it is supposed to increment but it is not doing that. This is the form: <form method="post" action=""> <input name="Title" type="hidden" value="$5 Donation With Shipping" /> <input name="SerialNum" type="hidden" value="5DollarDonationWS" /> <input name="Price" type="hidden" value="5.00" /> <input name="Shipping1" type="hidden" value="1.00" /> <input name="Shipping2" type="hidden" value="0.50" /> <input name="cmd" type="hidden" id="cmd" value="1" /> <input type="submit" name="Submit" value="Add To Cart" /> </form> and this is the shopping cart script: <?php session_start(); if (!isset($_SESSION['cart'])) { $_SESSION['cartnum'] = 0; //number of items in cart $_SESSION['cart'] = array( ); } else { $_SESSION['cartnum']++; $_SESSION['cart'][] = array( "title" => $_POST['title'], "Price" => $_POST['price'], "Author" => $_POST['author'], "Quantity" => $_POST['cmd'] ); $num = count($_SESSION['cart']); } foreach( $_SESSION['cart'] as $key => $value) // <-- shouldn't that be $_SESSION['cart'] ? { echo '<table border ="0" width ="100%" cellspacing = "0"><tr><th bgcolor="#cccccc">' . $key . '</th><tr><td>' . $value . '</td></tr></table>'; $num = count($_SESSION['cart']); } echo 'working' . $num; ?> Items are supposed to be adding to the cart everytime I press the 'add to cart' submit button. But it is only showing the items once with numbers instead of items everytime i press add to cart again. Can someone help?
Take out the else after the first block; if (!isset($_SESSION['cart'])) { $_SESSION['cartnum'] = 0; //number of items in cart $_SESSION['cart'] = array( ); } else { PHP: Insert; if($_POST){ PHP: before; $_SESSION['cartnum']++; $_SESSION['cart'][] = array( "title" => $_POST['title'], "Price" => $_POST['price'], "Author" => $_POST['author'], "Quantity" => $_POST['cmd'] ); $num = count($_SESSION['cart']); PHP: The cart session not being set shouldn't be a requisite of being able to add to your cart. If that doesn't work try typing var_dump($_SESSION['cart']); at the bottom of the page to see how the values are actually being stored/ if they are being stored.
This is what I am getting when I use the code at the bottom array(11) { [0]=> array(4) { ["title"]=> NULL ["Price"]=> NULL ["Author"]=> NULL ["Quantity"]=> string(1) "1" } [1]=> array(4) { ["title"]=> NULL ["Price"]=> NULL ["Author"]=> NULL ["Quantity"]=> string(1) "1" } [2]=> array(4) { ["title"]=> NULL ["Price"]=> NULL ["Author"]=> NULL ["Quantity"]=> string(1) "1" } [3]=> array(4) { ["title"]=> NULL ["Price"]=> NULL ["Author"]=> NULL ["Quantity"]=> string(1) "1" } [4]=> array(4) { ["title"]=> NULL ["Price"]=> NULL ["Author"]=> NULL ["Quantity"]=> string(1) "1" } [5]=> array(4) { ["title"]=> NULL ["Price"]=> NULL ["Author"]=> NULL ["Quantity"]=> string(1) "1" } [6]=> array(4) { ["title"]=> NULL ["Price"]=> NULL ["Author"]=> NULL ["Quantity"]=> string(1) "1" } [7]=> array(4) { ["title"]=> NULL ["Price"]=> NULL ["Author"]=> NULL ["Quantity"]=> string(1) "1" } [8]=> array(4) { ["title"]=> NULL ["Price"]=> NULL ["Author"]=> NULL ["Quantity"]=> string(1) "1" } [9]=> array(4) { ["title"]=> NULL ["Price"]=> NULL ["Author"]=> NULL ["Quantity"]=> string(1) "1" } [10]=> array(4) { ["title"]=> NULL ["Price"]=> NULL ["Author"]=> NULL ["Quantity"]=> string(1) "1" } }
The reason they are NULL is because you are using capitals on the sessions and lower case on the form. make sure they are all price and not Price
Same thing. It is the keys that were capitals not the values. Anyhow, I am getting the same thing when I change. [15]=> array(4) { ["title"]=> NULL ["price"]=> NULL ["author"]=> NULL ["quantity"]=> string(1) "1" } }
You were right I am now getting the values using the var_dump($_SESSION['cart']) function(the form had capitals). But it is still not displaying in the shopping cart.
I want the values and keys in green to display, but the ones in red are being display. [0]=> array(5) { ["title"]=> string(25) "$5 Donation With Shipping" ["price"]=> string(4) "5.00" ["author"]=> string(4) "Dwain Francis" ["quantity"]=> string(1) "1" } [1]=> array(4) { ["title"]=> string(25) "$5 Donation With Shipping" ["Price"]=> string(4) "5.00" ["Author"]=> string(4) "Dwain Francis" ["Quantity"]=> string(1) "1" }
Your cart is a multi-dimensional array so you can't simply do a foreach on the session cart. Try this; for($j=0; $j<count($_SESSION['cart']); $j++){ foreach( $_SESSION['cart'][$j] as $key => $value) // <-- shouldn't that be $_SESSION['cart'] ? { echo '<table border ="0" width ="100%" cellspacing = "0"><tr><th bgcolor="#cccccc">' . $key . '</th><tr><td>' . $value . '</td></tr></table>'; $num = count($_SESSION['cart']); } } PHP: