Believe it or not, the most difficult task in this project is now complete. Compared to thinking up categories and items, creating the scripts used to display the information is easy! The first script you will make is one that lists categories and items. Obviously, you wouldn't want to list all categories and all items all at once as soon as the user walks in the door, but you do want to give the user the option of immediately picking a category, seeing its items, and then picking another category. In other words, this script serves two purposes: It shows the categories and then shows the items in that category.
Listing 22.1 shows the code for seestore.php.
Listing 22.1. Script to View Categories
Given the length of scripts you saw in Chapter 20, these 67 fully functional lines should be a welcome change. In line 3, the database connection is opened because regardless of which action the script is takingshowing categories or showing items in categoriesthe database is necessary.
In line 5, the $display_block string is started, with some basic page title information added to it. Lines 912 create and issue the query to retrieve the category information. Line 14 checks for categories; if none are in the table, a message is printed to the user, and that's all this script does. However, if categories are found, the script moves on to line 17, which begins a while loop to extract the information.
In the while loop, lines 1820 retrieve the ID, title, and description of the category. String operations are performed to ensure that no slashes are in the text and that the category title is in uppercase for display purposes. Lines 2224 place the category information, including a self-referential page link, in the $display_block string. If a user clicks the link, she will return to this same script, except with a category ID passed in the query string. The script checks for this value in line 26.
If a $_GET["cat_id"] value has been passed to the script because the user clicked on a category link in hopes of seeing listed items, the script builds and issues another query (lines 2933) to retrieve the items in the category. Lines 3547 check for items and then build an item string as part of $display_block. Part of the information in the string is a link to a script called showitem.php, which you'll create in the next section.
After reaching that point, the script has nothing left to do, so it prints the HTML and value of $display_block. Figure 22.1 shows the outcome of the script when accessed directly; only the category information shows.
Figure 22.1. Categories in the store.
In Figure 22.2, you see what happens when the user clicks on the HATS link: The script gathers all the items associated with the category and prints them on the screen. The user can still jump to another category on this same page, and it will gather the items for that category.
Figure 22.2. Items within a category in the store.
The last piece of the puzzle for this chapter is the creation of the item display page.