After adding products to the cart, you need a script to display all the products selected in the cart and modify the cart content if required. The showcart.php script shown in Listing 4-2 does the task of displaying items selected in the cart and managing them.
Listing 4-2. The showcart.php Script Displays the Content in the Cart Table and
Maintains It <?php if ( ! isset($totalamount)) { $totalamount=0; } $totalquantity=0; if (!session_id()) { session_start(); }
$connect = mysqli_connect("localhost", "root", "gold", "shopping") or die("Please, check your server connection.");
$sessid = session_id();
$query = "SELECT * FROM cart WHERE cart_sess = '$sessid'"; $results = mysqli_query($connect, $query) or die (mysql_query()); if(mysqli_num_rows($results)==0)
{
echo "<div style=\"width:200px; margin:auto;\">Your Cart is empty</div> "; }
else { ?>
<table border="1" align="center" cellpadding="5">
<tr><td> Item Code</td><td>Quantity</td><td>Item Name</td><td>Price</ td><td>Total Price</td>
<?php
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) { extract($row);
echo "<tr><td>"; echo $cart_itemcode; echo "</td>";
echo "<td><form method=\"POST\" action=\"cart.php?action=change&icode= $cart_itemcode\"><input type=\"text\" name=\"modified_quantity\" size=\"2\" value=\"$cart_quantity\">"; echo "</td><td>"; echo $cart_item_name; echo "</td><td>"; echo $cart_price; echo "</td><td>";
$totalquantity = $totalquantity + $cart_quantity;
$totalprice = number_format($cart_price * $cart_quantity, 2); $totalamount=$totalamount + ($cart_price * $cart_quantity); echo $totalprice;
echo "</td><td>";
echo "<input type=\"submit\" name=\"Submit\" value=\"Change quantity\"> </form></td>";
echo "<td>";
echo "<form method=\"POST\" action=\"cart.php?action=delete&icode=$cart_ itemcode\">";
echo "<input type=\"submit\" name=\"Submit\" value=\"Delete Item\"></form> </td></tr>"; } echo "<tr><td >Total</td><td>$totalquantity</td><td></td><td></td><td>"; $totalamount = number_format($totalamount, 2); echo $totalamount; echo "</td></tr>"; echo "</table><br>";
echo "<div style=\"width:400px; margin:auto;\">You currently have " . $totalquantity . " product(s) selected in your cart</div> ";
?>
<table border="0" style="margin:auto;"> <tr><td style="padding: 10px;">
<form method="POST" action="cart.php?action=empty"> <input type="submit" name="Submit" value="Empty Cart" style="font-family:verdana; font-size:150%;" >
</form> </td><td>
<form method="POST" action="checklogin.php">
<input id="cartamount" name="cartamount" type="hidden" value= "<?php echo $totalamount ; ?>">
<input type="submit" name="Submit" value="Checkout" style="font-family:verdana; font-size:150%;" > </form> </td></tr></table> <?php } ?> </body> </html>
The program determines whether the session ID is already set. If not, a new session is started. As you read earlier, the session ID helps to identify the products selected by the specific visitor of the site. Thereafter, a connection to the MySQL server is established and the shopping database is selected. A SQL query is executed to check if there are any products in the cart with the given session ID. If there are, that means the visitor has added one or more products to the cart already. In that case, all the items stored in the cart, along with their respective quantities, are displayed on the screen.
If no products are found in the cart table that match the given session ID, it means no products are in the cart. A message is displayed on the screen indicating that the visitor has "0 products selected in the cart".
The script enables the visitor to perform the following tasks:
• Add more products to the cart by selecting any category of item from the top menu.
• Modify the quantity of any item already in the cart. • Delete an item from the cart or empty the cart entirely.
Upon running the script, you see the products selected in the cart, as shown in Figure 4-2. Note that only one product is selected in the cart at this point.
Figure 4-2. Maintaining the cart
Figure 4-3. Showing items selected in the cart
Assuming the visitor added two iPhone smartphones to the cart, the cart’s content would now appear as shown in Figure 4-3.
You can always change the quantity of any product by entering the desired quantity in the textbox, followed by clicking the Change Quantity button found in the product’s row. Upon changing the quantity of the iPhone product to 1, the cart will appear as shown in Figure 4-4.
You can also delete any product from the cart by selecting the Delete Item button in that respective row. After deleting the Asus laptop from the cart, for example, only one item will be left in the cart, as shown in Figure 4-5.
Figure 4-4. Cart’s content after changing the quantity of any item in the cart
Figure 4-5. Cart’s content after deleting an item from the cart
The Empty Cart button at the bottom will delete all the items from the cart. You will get a message confirming this action, as shown in Figure 4-6.