jimleeder123 Posted May 27, 2015 Share Posted May 27, 2015 I can show a single product in a cart, but unsure how to show multiple products. I have a cart on my front page. Categories are shown by PHP/mysql, when you choose a category its products are then shown. each product has an "add to cart" button, which leads to addtocart.php. The php code for this file is below: -------------------------------------------------- session_start(); unset ($_SESSION['.$product.']); unset ($_SESSION['.$total.']); $product = $_POST['hiddenid']; $child = $_POST['child']; $group = $_POST['attributes']; $total = array('id' => $product, 'child' => $child, 'attributes' => $group); $_SESSION['id'] = $total['id']; //Parent product ID, eg Margherita $_SESSION['child'] = $total['child']; //child product eg 8" Margherita $_SESSION['attributes'] = $total['attributes']; $message = "You have added a product to the cart. You will be redirected to the category page."; echo "<script type='text/javascript'>alert('$message'); window.location.href = 'shop.php';</script>"; -------------------------------------------------------------- Then on the front page, I show the cart in an include file. The cart php code is as follows: ------------------------------------- echo "<div id='cartwrap'>"; echo "<div id='cart'>"; echo "<div id='carttitlewrap'>"; echo "<span id='carttitle'>Cart</span>"; echo "</div>"; echo "<br /> <br />"; if (!isset($_SESSION['id'])) { echo "nothing here"; }else{ $id = $_SESSION['id']; $child = $_SESSION['child']; $group = $_SESSION['attributes']; echo "Main Product ID: "; echo '<span>'.$id.'</span>'; echo "<br />"; echo "<br />"; echo "Child Product: "; echo '<span>'.$child.'</span>'; echo "<br />"; echo "<br />"; echo "Attribute Option: "; echo '<span>'.$group.'</span>'; }//end of if to check if products in cart echo "</div>"; echo "</div>"; ---------------------------------------------------------- So I want to know how to add multiple products to the cart. The above code shows data for a single product in the cart, but not multiple. Quote Link to comment https://forums.phpfreaks.com/topic/296504-making-multiple-products-show-in-a-cart/ Share on other sites More sharing options...
jimleeder123 Posted May 27, 2015 Author Share Posted May 27, 2015 Update: I have got it working putting a single one in as an array, but when I append arrays (using stac overflow help) it comes up with the data in the first array, and then "array array array..." and so on. If i do a var_dump on it I can see the data. But obviously I need to echo it out. Quote Link to comment https://forums.phpfreaks.com/topic/296504-making-multiple-products-show-in-a-cart/#findComment-1512692 Share on other sites More sharing options...
mac_gyver Posted May 27, 2015 Share Posted May 27, 2015 you would use a multi-dimensional array. firstly, you should separate the cart session data from other session data by giving it a primary array associative index name - $_SESSION['cart'] secondly, your id should uniquely identify an item, it should be the auto-increment id from your database table where you have defined the items, and the id should be used as the next array dimension's index - $_SESSION['cart'][id_value] lastly, to allow multiple of any item to be stored in a cart, the value stored should be the quantity - $_SESSION['cart'][id_value] = quantity to add a quantity of one (1) to the cart for any id, the basic code would look like - session_start(); if(!isset($_SESSION['cart'])){ $_SESSION['cart'] = array(); // create an empty cart } // form processing code if($_SERVER['REQUEST_METHOD'] == 'POST'){ // add quantity one to cart if(isset($_POST['add'])){ // validate/cast input data $id = (int)$_POST['hiddenid']; // the validation logic will produce a 'safe' $id to use if(!isset($_SESSION['cart'][$id])){ $_SESSION['cart'][$id] = 0; // create an empty item (it will be incremented next) } $_SESSION['cart'][$id]++; // increment - add one to whatever the existing quantity is } // code for other form processing operations would go here.... } Quote Link to comment https://forums.phpfreaks.com/topic/296504-making-multiple-products-show-in-a-cart/#findComment-1512693 Share on other sites More sharing options...
jimleeder123 Posted May 27, 2015 Author Share Posted May 27, 2015 I have since fixed this, got it to work with array_push and a few if statements. Code below for addtocart.php session_start(); unset ($_SESSION['.$product.']); unset ($_SESSION['.$total.']); //unset ($_SESSION['cart']); $id = $_POST['hiddenid']; $name = $_POST['hiddenname']; $child = $_POST['child']; if (!isset($_SESSION['cart'])) { //if cart is empty if ($child != "none"){ //if child product has been selected $_SESSION['cart'] = array($child); }else{ //if the child product has not been selected show the main product name (product won't have a child product) $_SESSION['cart'] = array($name); } }else{ //if cart already has 1 or more items in it if ($child != "none"){ array_push($_SESSION['cart'], ''.$child.''); //if there is already an item in the cart, add a new additional one }else{ array_push($_SESSION['cart'], ''.$name.''); } } $message = "You have added a product to the cart. You will be redirected to the category page."; echo "<script type='text/javascript'>alert('$message'); window.location.href = 'shop.php';</script>"; Quote Link to comment https://forums.phpfreaks.com/topic/296504-making-multiple-products-show-in-a-cart/#findComment-1512694 Share on other sites More sharing options...
CroNiX Posted May 27, 2015 Share Posted May 27, 2015 You would be better off using php to redirect rather than using js. Quote Link to comment https://forums.phpfreaks.com/topic/296504-making-multiple-products-show-in-a-cart/#findComment-1512695 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.