Freedom-n-Democrazy Posted October 5, 2011 Share Posted October 5, 2011 I'm trying to make my own shopping cart for the first time and having a problem getting products to add to it. All I get is "No product in cart.". Its nothing special now, I'm just trying to reach the first stepping stone of adding something successfully to the session. index.html <?php session_start(); ?> <?php $cart = $_SESSION['cart']; if (!$cart) { echo "No product in cart."; } if ($cart) { foreach ($id) { echo $id; } } ?> <A href="index.html?action=add&id=1">Add to cart</A> What have I done wrong and what am I suppose to do? Quote Link to comment https://forums.phpfreaks.com/topic/248458-trying-to-get-something-to-add-to-a-session/ Share on other sites More sharing options...
awjudd Posted October 5, 2011 Share Posted October 5, 2011 There is no code there that adds to the cart ... ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248458-trying-to-get-something-to-add-to-a-session/#findComment-1275886 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 I see. Is there something thats meant to work with <A href="index.html?action=add&id=1">Add to cart</A>? Quote Link to comment https://forums.phpfreaks.com/topic/248458-trying-to-get-something-to-add-to-a-session/#findComment-1275887 Share on other sites More sharing options...
awjudd Posted October 5, 2011 Share Posted October 5, 2011 Those values are coming in through the query string (i.e. get parameters). They are accessible through $_GET. ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248458-trying-to-get-something-to-add-to-a-session/#findComment-1275888 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 Ok, so how would I achieve what I want to do? id = $_GET['action']? I'm badly confused. Quote Link to comment https://forums.phpfreaks.com/topic/248458-trying-to-get-something-to-add-to-a-session/#findComment-1275889 Share on other sites More sharing options...
awjudd Posted October 5, 2011 Share Posted October 5, 2011 After cleansing your data (i.e. don't trust what is actually coming in from the URL, or anywhere for that matter), you would add it to your cart by doing something like this: // Check if we selected an action if ( isset ( $_GET [ 'action' ] ) && $_GET [ 'action' ] == 'add' ) { // Since this is likely an auto-incremented ID, it should be an INT $id = intval($_GET['id']); // Validate that the item comes from the db (i.e. select from the table you pulled it from) // Validation successful, add the id to the cart $_SESSION['cart'][] = $id; } ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248458-trying-to-get-something-to-add-to-a-session/#findComment-1275890 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 Thanks man, your little trick worked. "No product in cart." no longer shows, but neither does the id. <?php if (isset ($_GET['action']) && $_GET ['action'] == 'add' ) { $id = intval($_GET['id']); $_SESSION['cart'][] = $id; } $cart = $_SESSION['cart']; if (!$cart) { echo "No product in cart."; } if ($cart) { foreach ($id as $id) { echo $id; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/248458-trying-to-get-something-to-add-to-a-session/#findComment-1275892 Share on other sites More sharing options...
awjudd Posted October 5, 2011 Share Posted October 5, 2011 Your foreach loop is incorrect. foreach ( $_SESSION [ 'cart' ] as $id ) { echo $id; } ~juddster Quote Link to comment https://forums.phpfreaks.com/topic/248458-trying-to-get-something-to-add-to-a-session/#findComment-1275893 Share on other sites More sharing options...
Freedom-n-Democrazy Posted October 5, 2011 Author Share Posted October 5, 2011 Ahhh, yeah your right man, because $_SESSION['cart'][] = $id;. Thanks for your help man. I understand the dimensions of SESSION allot better now! Wow PHP is so awesome! Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/248458-trying-to-get-something-to-add-to-a-session/#findComment-1275895 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.