Displaying a Shopper's Basket

 

Introduction.  Now we need to develop the page to display the shopper's basket.  But, it isn't enough just to display it, we need to make sure they can also do a variety of things with this page as the basis of their interactions
  • delete items
  • update quantities on items
  • continue shopping
  • check out

Some of the other things we want the shopper to be able to see are

  • quantities ordered
  • prices for a group of items
  • weight for a group of items
  • total price
  • total weight (since this determines shipping costs)

All of our basic functionalities will revolve around the basket, though their implementations will rely on processing script pages that always bring us back to the basket.

You need to upload the following two images to your images subfolder of your firefox web.

 

Image Name
sub_remove.gif
sub_check_out.gif

 

The following PHP page should be called basket.php and it should be uploaded to your firefox website.

 

<html>
<!-- basket.php - displaying the shopper's basket -->
<head>
<title>Displaying a Shopper's Basket</title>
</head>
<?php
include("includes/header.php");
include("includes/connection_config.php");
?>

<br><br>
<?php
session_start();
if (!($_SESSION['id_basket'] > 0))
{

echo "<br><font size=4 color=ff0000><center>You haven't yet ordered any items</center></font>";

}
else
{

$id_basket = $_SESSION['id_basket'];
// connecting to the database on battcave.com
$link = mysql_connect($host, $user, $password);
// now we need to get all the basic info on the products
// in the basket_item table for this shopper
// in order to display them

?>
<table width = 800 border = 0 cellpadding=4 align="center">
<tr>
<td align="center"><font size="4" color="a66838"><b>Product Name</b></font></td>
<td align="center"><font size="4" color="a66838"><b>Attributes</b></font></td>
<td align="center" width="150"><font size="4" color="a66838"><b>Quantity</b></font></td>
<td align="left"><font size="4" color="a66838"><b>Weight</b></font></td>
<td align="center"><font size="4" color="a66838"><b>Cost</b></font></td>
<td align="center"></td>
</tr>
<?php
$total_weight = 0.0;
$total_cost = 0.0;
$query_string = "SELECT * FROM basket_item WHERE id_basket = $id_basket";
$result_set = mysql_db_query($db_name, $query_string, $link);
while ($row = mysql_fetch_array($result_set))
{

?>
<form method="post" action="update_quantity.php">
<tr valign="middle">
<td align="center"><font size="4" color="c0c0c0"> <?php echo $row[product_name]; ?></font></td>
<?php
$row_attributes = "";
if ($row[attribute2] != "") $row_attributes = $row_attributes . $row[attribute2];
if ($row[attribute1] != "") $row_attributes = $row_attributes . "<br>" . $row[attribute1];
?>
<td align="center"><font size="4" color="c0c0c0"> <?php echo $row_attributes; ?></font></td>
<td align="center"><font size="4" color="c0c0c0"><input type="text" size="2" name="txt_quantity" style="font-size: 14pt" value="<?php echo $row[item_quantity]; ?>"></font>
<input type="hidden" name="txt_id_basket_item" value="<?php echo $row[id_basket_item]; ?>"><input type="submit" value="Update" style="font-size: 12pt; font-weight: bold">
</td>
<?php $row_weight = $row[weight]*$row[item_quantity]; ?>
<td align="left"><font size="4" color="c0c0c0"> <?php printf("%01.1f",$row_weight); ?></font></td>
<?php $row_subtotal = $row[price] * $row[item_quantity]; ?>
<td align="right"><font size="4" color="c0c0c0"> <?php printf("%01.2f",$row_subtotal); ?></font></td>
<?php
$total_weight = $total_weight + $row_weight;
$total_cost = $total_cost + $row_subtotal;
?>
<td align="center"><a href="delete_item.php?id_item=<?php echo $row[id_basket_item]; ?>"><img src="images/sub_remove.gif" border="0"></a></td>
</tr>
</form>

<?php

} // end the while loop for entries in the basket_item table
?>
<td colspan=3 align="right"><font size="4" color="a66838"><b>Totals:</b></td>
<td align="left"><font size="4" color="c0c0c0"> <?php printf("%01.1f",$total_weight); ?> LBS</font></td>
<td align="right"><font size="4" color="c0c0c0">$<?php printf("%01.2f",$total_cost); ?></font></td>
<td></td>
<tr><td colspan="6">&nbsp;</td></tr>
<tr><td colspan="6" align="center"><a href="checkout_profile_access.php"><img src="images/sub_check_out.gif" border="0"></a></td></tr>
<?php
//  create session variables for $total_cost and $total_weight for use in check out process
$_SESSION['total_cost'] = $total_cost;
$_SESSION['total_weight'] = $total_weight;

} // end the else clause for whether the shopper has selected something
mysql_close($link);
?>
</table>
</body>
</html>

 

 

After having added several items with differing characteristics, I took the snapshot of the basket.php which is shown below.

 

 

We now give some discussion of the code.  I have highlighted two of the more unusual sections of code in red and purple for the discussion.  But we will work through the code largely sequentially.

  • we check whether the shopper has already initiated a shopping basket by looking at the session variable $_SESSION['id_basket']
    • if it isn't positive
      • we know they don't have an id_basket
      • we give them an appropriate message
    • else we display the basket and all its instruments
      • we link to the database
      • we start the table that displays the basket contents and create the column headers
      • we select all the basket_items from the basket_item table based on the $id_basket
      • we loop through the contents while doing the following
        • display each product's name
        • compose the attributes, if they exist, for display
        • create a new form in each row for each product that posts to the update_quantity.php - this is displayed in red
          • displays the quantity ordered in a text box
          • makes the id_basket_item available to the form processing page through a hidden textbox
          • displays an Update button for each row
        • we compute the total weight associated with the item based on each individual product's weight and the quantity ordered
          • we display this with one decimal place showing
          • we increment a total weight for the entire order
        • we compute the total price associated with the item based on each individual product's price and the quantity ordered
          • we display this with two decimal places showing
          • we increment a total price for the entire order
        • we make use of a button image, that says Remove, that is made active using an <a> anchor tag - this is displayed in purple
          • it passes the id_basket_item in the string
          • it links to the delete_item.php processing page
      • we end the loop
      • we display the total weight and cost in appropriate columns, using appropriate formats and alignments
      • we make use of a button image, that says Check Out, that is made active using an <a> anchor tag to go to a page checkout_profile_access.php
    • we end the else block
  • we close the link