Ashbridge Posted August 27, 2015 Share Posted August 27, 2015 Hello all, I am trying to create my own Shopping Cart system, very basic but it'll work how I want it too! I have a Product Page which pulls the Product Name, Description, Price and Image. This is working great and the page is styled up and ready! Now, the problem that I am having is somehow storing this information in some kind of session and then displaying the information on the next page which you get to via the "Buy Now" button. This button will take you to the Shopping Cart page where in a table, the information will be displayed. How would I go about this? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted August 27, 2015 Share Posted August 27, 2015 (edited) You don't store that data in a session. What you do is only store the product id in the session when the user clicks the add to basket button. Basic example code <?php session_start(); // if add to basket button submitted if(isset($_POST['add_to_basket'])) { // make sure product id is numeric value if(is_numeric($_POST['product_id'])) { // add product id to basket session variable $_SESSION['basket'] = intval($_POST['product_id']); } } ?> ... each product has a add to basket button and a hidden input field containing the product id ... <form method="post"> <input type="hidden" name="product_id" value="<?php echo $product_$id ?>" /> <input type="submit" value="Add To Basket" name="add_to_basket" /> </form> On your shopping cart page you will run a query to get the product info that match the product id(s) stored in your session. Very basic example code <?php session_start(); // check to make sure the session basket exists and is not empty if(isset($_SESSION['basket']) && !empty($_SESSION['basket'])) { // query the products table, return the products matching the ids in the session basket $product_ids = implode(',', $_SESSION['basket']); $sql = 'SELECT name, description, price, image FROM products WHERE id IN('.$product_ids.')'; $result = $mysqli->query($sql); while($row = $result->fetch_assoc()) { // output product info } } Edited August 27, 2015 by Ch0cu3r 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.