Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP shopping cart doesn't show numerous products
    text
    copied!<p>i've made a php shopping cart, it shows all the products on the home screen, you can click on them and add them to your basket, then you are taken to the shopping cart screen. this only shows one product but with a quantity of how many different products you've added, even if they all have different titles. the total price and quantity work, its only the product name that doesn't. This is my code..</p> <pre><code>&lt;?php session_start(); include "connect.php"; include "header.php"; function viewcart(){ if (isset($_SESSION['cart'])){ //if shopping cart is not empty $cart = $_SESSION['cart']; //store the cart array into a variable then display the content echo "&lt;table border=\"1\"&gt;&lt;tr&gt;&lt;th&gt;Product Name&lt;/th&gt;&lt;th&gt;Quantity&lt;/th&gt;&lt;th&gt;Delete&lt;/th&gt;&lt;/tr&gt;"; foreach ($cart as $product=&gt;$quantity){ $q = "SELECT ID, Name FROM Products"; $result = mysqli_query($_SESSION['conn'],$q); $row = mysqli_fetch_array($result); $product_id = $row[0]; echo "&lt;tr&gt;&lt;td&gt;$product&lt;/td&gt;&lt;td&gt;$quantity&lt;/td&gt;&lt;td&gt;&lt;a href=\"?action=delete&amp;product=$product_id\"&gt;-&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;"; mysqli_free_result($result); } echo "&lt;/table&gt;"; subtotal($cart); //display the subtotal } else { //if shopping cart is empty echo "&lt;p&gt;There is no product in your shopping cart.&lt;/p&gt;"; } } function addproduct($product_id, $product_qty){ $q = "SELECT ID, Name FROM Products"; $result = mysqli_query($_SESSION['conn'],$q) or die("Error: ".mysqli_error($_SESSION['conn'])); $row = mysqli_fetch_array($result); $product_name = $row[1]; //get the product name from product id because it is better to display name than id in the cart if (isset($_SESSION['cart'])){ //if shopping cart is not empty $cart = $_SESSION['cart']; if (array_key_exists($product_name, $cart)){ //if the product exists, update quantity $cart[$product_name] += $product_qty; } else { //otherwise, add new product-quantity pair to the array $cart[$product_name]=$product_qty; } $_SESSION['cart'] = $cart; //write the updated array back to session variable } else { //if shopping cart is empty $cart = array($product_name=&gt;$product_qty); //add product and quantity to the shopping cart $_SESSION['cart'] = $cart; //write the updated array back } mysqli_free_result($result); } function deleteproduct($product_id, $product_qty){ $q = "SELECT Name FROM Products"; $result = mysqli_query($_SESSION['conn'],$q); $row = mysqli_fetch_array($result); $product_name = $row['Name']; if (isset($_SESSION['cart'])){ //if shopping cart is not empty $cart = $_SESSION['cart']; if (array_key_exists($product_name, $cart)){ //if the product exists, update quantity $cart[$product_name] -= $product_qty; if ($cart[$product_name] == 0){ //if the quantity is 0, delete the key unset($cart[$product_name]); } } else { //exception echo "&lt;p&gt;Error!&lt;/p&gt;"; } $_SESSION['cart'] = $cart; //write the updated array back to session variable } else { echo "&lt;p&gt;Error!&lt;/p&gt;"; } mysqli_free_result($result); } function emptycart(){ if (isset($_SESSION['cart'])){ //if shopping cart is not empty unset($_SESSION['cart']); } else { echo "&lt;p&gt;Error!&lt;/p&gt;"; } } function subtotal($cart){ //calculate the total value of products in the shopping cart $total = 0; //initialise total if (!empty($cart)){ foreach ($cart as $product =&gt; $quantity){ $q = "SELECT ID, Price FROM Products"; $result = mysqli_query($_SESSION['conn'],$q); $row = mysqli_fetch_array($result); $price = $row['Price']; $total += $price * $quantity; } echo "&lt;p&gt;Total: $total | &lt;a href=\"?action=empty\"&gt;Empty cart&lt;/a&gt;&lt;/p&gt;"; } else { unset($_SESSION['cart']); //do not need to keep an empty cart, so delete it echo "&lt;p&gt;There is no product in your shopping cart.&lt;/p&gt;"; } } if (isset($_GET['action'])){ if ($_GET['action']=='view'){ viewcart(); } elseif ($_GET['action']=='add'){ if (isset($_GET['product'])){ $product_id = $_GET['product']; $product_qty = 1; //default product value addproduct($product_id, $product_qty); viewcart(); echo "&lt;p&gt;&lt;a href=\"javascript:history.go(-1)\"&gt;back&lt;/a&gt;&lt;/p&gt;"; } else { echo "&lt;p&gt;There is an error! Are you trying to attack this little poor shopping cart?&lt;/p&gt;"; } } elseif ($_GET['action'] == 'delete'){ if (isset($_GET['product'])){ $product_id = $_GET['product']; $product_qty = 1; //default product value deleteproduct($product_id, $product_qty); viewcart(); } else { echo "&lt;p&gt;There is an error! &lt;/p&gt;"; } } elseif ($_GET['action']=='empty'){ emptycart(); viewcart(); } else { echo "&lt;p&gt;There is an error! &lt;/p&gt;"; } } else { echo "&lt;p&gt;There is an error! &lt;/p&gt;"; } include "footer.php"; ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload