shyam13 Posted July 19, 2012 Share Posted July 19, 2012 Hi All, I am using a construct to create the session to create the cart and add the products to the cart but everytime, I click add to cart, the products are not entered into the cart. Please Help Quote Link to comment Share on other sites More sharing options...
shyam13 Posted July 19, 2012 Author Share Posted July 19, 2012 Hi All, I am using a construct to create the session to create the cart and add the products to the cart but everytime, I click add to cart, the products are not entered into the cart. this is my code: <?php if (isset($_POST['submit'])){ if (!isset($_SESSION['cart'])) { $_SESSION['cart'] = array(); } $_SESSION['cart'][] = $_POST['product_id']; exit(); } ?> Please Help Thank You Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted July 19, 2012 Share Posted July 19, 2012 Do you have PHP set to display all errors? If so, are you getting any errors or notices? <?php error_reporting(E_ALL); ini_set('display_errors', 1); ?> Have you checked if the variables contain what you expect? To see if the session variable contains anything, for example, you could do something like <?php //... $_SESSION['cart'][] = $_POST['product_id']; var_dump($_SESSION['cart']); exit(); //... ?> Have you checked what portions of the script are being executed? For example, to see if the if statements are being evaluated to true, you could try something like <?php if (isset($_POST['submit'])){ print 'here - submit'; if (!isset($_SESSION['cart'])){ print 'here - session not created yet'; //... ?> Quote Link to comment Share on other sites More sharing options...
DavidAM Posted July 19, 2012 Share Posted July 19, 2012 Do you have session_start at the beginning of that script? Have you tried printing the $_SESSION to see what is in it; at the beginning of the script (before assignment) and at the end of the script (after assignment) and on the next page (which also must have session_start at the beginning)? Quote Link to comment Share on other sites More sharing options...
shyam13 Posted July 19, 2012 Author Share Posted July 19, 2012 I have started the session and the cart is being updated with the products but I am now trying to display the list of products in my checkout, I am using a foreach statement to retrieve each product from the cart and display it on the checkout. here is my code: <?php foreach ($_SESSION['cart'] as $item) product::get('product_id' as $item ); ?> Quote Link to comment Share on other sites More sharing options...
DavidAM Posted July 19, 2012 Share Posted July 19, 2012 You original problem statement was: ... the products are not entered into the cart. Now you say: ... the cart is being updated with the products So, before we chase another wild goose down the rabbit hole, how about you tell us what you are doing, what is happening, how that is different from what you expect to happen, and show us the code that is not doing what you expect? We are not mind readers and two lines of code does not give us any kind of context to figure out what is wrong. Or did I read your last post wrong? Are you saying the problem is solved? Quote Link to comment Share on other sites More sharing options...
shyam13 Posted July 19, 2012 Author Share Posted July 19, 2012 I am creating a shopping cart, I have got the code to work which adds products to the cart but when I try and show the products in the cart on the check out page, nothing appears. This is the code to add products to the cart: <?php # this is an mysql select statement to select all the values from the product table where the product vis is true or in computing terms 1. $product = mysql::select('product', NULL , condition::where('product_vis')->is(1))->execute(); # for each product as data place as a row foreach ($product->data as $row) { ?> <!-- Create a new li for each product --> <li id='<?php echo $row ['product_id']?>'> <!-- Include a new form for each product so that all the values are encapsulated --> <form method='post' action='/25.php' class='cart'> <!-- Hidden form element for the product id --> <input name='id' type='hidden' value='<?php echo $row['product_id']; ?>' /> <!-- Product details --> <?php echo $row ['product_cost']?> <?php echo $row['product_name'] ?> <!-- Use editable field for the quantity --> <input name='quantity' type='number' value='1' /> <!-- Submit to cart button, not that it doesnt have a name, since its not required --> <input type='submit' value='Add to Cart' /> <!-- Close the cart --> </form> <!-- Close the li --> </li> <?php } ?> <!-- Close the ul --> </ul> <!-- Close the form --> </form> and this is the code to display products on the checkout: ><?php foreach ($_SESSION['product_id'] product::get('product_id' as $item); ?> Thank You Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted July 19, 2012 Share Posted July 19, 2012 Did you call session_start() on the page which displays the results? If so, does the session variable contain anything? <?php var_dump($_SESSION['cart']); ?> Quote Link to comment Share on other sites More sharing options...
shyam13 Posted July 19, 2012 Author Share Posted July 19, 2012 I have created a session start variable at the top of the page, the variable has the name of the cart. Thank you Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted July 19, 2012 Share Posted July 19, 2012 Is your session variable supposed to be $_SESSION['cart'] or $_SESSION['product_id']? Quote Link to comment Share on other sites More sharing options...
shyam13 Posted July 19, 2012 Author Share Posted July 19, 2012 Oh sorry, yea my session variable is 'cart' not 'product id' Thank You Quote Link to comment Share on other sites More sharing options...
shyam13 Posted July 19, 2012 Author Share Posted July 19, 2012 I have got the products added to the shopping cart, I am wondering how would i display the products in a table in a check out menu? Thank You Quote Link to comment 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.