In this section, you'll make modifications to the showitem.php script from Chapter 22. The goal is to transform the item information page into an item information page with a form for selecting colors, sizes, and quantities.
In the original script, insert the following before line 2:
Because the shopping cart elements are attached to the user through a session ID, the session must be started. The next changes don't occur until what was line 39 of the original script, so that's where we start in Listing 23.1.
Listing 23.1. New Lines in showitem.php
The first change is at the new line 40, where the $display_block string is continued to include the beginning <form> element. The action of the form is a script called addtocart.php, which you will create in the next section.
The next change occurs at line 53, where the $display_block string is continued to include the opening tag of a <select> element, named sel_item_color. In lines 5758, the colors are put into <option> elements for the user to choose from, instead of simply printing on the screen. Line 60 closes the <select> element.
The same types of changes are made for item sizes. Lines 7374 reflect the continuation of the $display_block string to include the <select> element, named sel_item_size. Lines 7879 write the colors in <option> elements, and line 83 closes the <select> element.
Lines 8894 are additions to the script. These lines create a <select> element, called sel_item_qty, for the user to pick how many items to purchase. Line 97 closes this <select> element, and line 98 adds a hidden field for the item_id. Line 100 adds the submit button, and line 101 closes the form. We close the connection to MySQL in line 206, and the remaining lines are unchanged from the original script.
When viewing the baseball hat item using the new version of showitem.php, you would see Figure 23-1, reflecting the addition of the form elements.
Figure 23.1. The new baseball hat item page.
The next step is to create the addtocart.php script.
Adding Items to Your Cart
The addtocart.php script simply writes information to the store_shoppertrack table and redirects the user to the view of the shopping cart. We'll create the addtocart.php script first in Listing 23.2 and then tackle the showcart.php script next.
Listing 23.2. The addtocart.php Script
Line 2 continues the user session, which is important because you need to capture the user's session ID to write to the store_shoppertrack table. Line 5 makes the database connection, and line 7 begins the validation of the actions.
In line 7, the script verifies that a value is present in $_POST["sel_item_id"], meaning that the user came to this script after submitting the proper form. If there is no value, the script jumps down to line 41 and sends the user away in line 43, and that's it for the script.
However, if there is a value in $_POST["sel_item_id"], the next action is to verify that it is a valid value. Lines 912 create and issue a SQL query to gather the title of the selected item. Line 14 checks for a result; if there is no result, the user is again redirected away in line 16 because the item selection was not valid.
If the item selection is valid, the script continues on to line 20 and extracts this value from the resultset. The script now has enough information to add the item selection to the store_shoppertrack table, which it does in lines 2534.
After the query has been issued, the user is redirected to showcart.php, which contains all cart items. You'll create this in the next section.
Viewing the Cart
Now that you can add items to a cart, you'll want to see them! Listing 23.3 shows the code for showcart.php.
Listing 23.3. The showcart.php Script
Line 2 continues the user session, which is important because you need to match the user's session ID with the records in the store_shoppertrack table. Line 5 makes the database connection, and line 7 begins the $display_block string, with a heading for the page.
Lines 1014 represent a joined query, in which the user's saved items are retrieved. The id, sel_item_qty,sel_item_size, and sel_item_color fields are extracted from store_shoppertrack, and the item_title and item_price fields are retrieved from the store_items table, based on the matching information from store_shoppertrack. In other words, instead of printing 2 for the selected item, Baseball Hat is shown as the title. Lines 1516 issue the query, and line 18 checks for results.
If there are no results, the user has no items in the store_shoppertrack table. A message is written to the $display_block string, and the script exits and shows the message.
if there are indeed results, the beginning of an HTML table is created in lines 2434, with columns defined for all the information in the cart (and then some). Line 36 begins the while loop to extract each item from the store_shoppertrack, and this loop continues until line 56, printing the information in the proper table cell.
In lines 5354, you see a link created for an item removal script, which you will create in the next section. Line 58 closes the table, and the script finishes up and prints HTML to the screen in lines 6168.
Now, go back to an item page and add the item to your cart. After the items are written to the store_shoppertrack table, you should be redirected to the showcart.php page, and your newly selected items should be displayed. Figure 23.2 shows my cart after adding some items.
Figure 23.2. Items added to cart.
The next step is to create the removefromcart.php script.
Removing Items from Your Cart
The removefromcart.php script is short because all it does is issue a query and redirect the user to another script. Inevitably, a user will want to weed items out of his cart, and this script enables him to do just that. Listing 23.4 shows the complete script.
Listing 23.4. The removefromcart.php Script
Line 2 continues the user session because you need to match the user's session ID with the records in the store_shoppertrack table. Line 5 makes the database connection, and line 7 checks for a value in $_GET ["id"]. If a value does not exist in $_GET ["id"], the user is not clicking the link from her cart and, thus, is sent away in line 19.
If a value exists in $_GET ["id"], a SQL query (lines 810) is issued (lines 1112), and the user is redirected to the showcart.php script (line 15), where the item should no longer show up. Try it and see!