JavaScript Editor Javascript source editor     Web programming 

Team LiB
Previous Section Next Section

Displaying Categories of Items

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 will serve two purposes: It will show the categories and then show the items in that category.

Listing 21.1 shows the code for seestore.php.

Listing 21.1. Script to View Categories
 1: <?php
 2: //connect to database
 3: $conn = mysql_connect("localhost", "joeuser", "somepass")
 4:     or die(mysql_error());
 5: mysql_select_db("testDB",$conn) or die(mysql_error());
 7: $display_block = "<h1>My Categories</h1>
 8: <P>Select a category to see its items.</p>";
10: //show categories first
11: $get_cats = "select id, cat_title, cat_desc from
12:     store_categories order by cat_title";
13: $get_cats_res = mysql_query($get_cats) or die(mysql_error());
15: if (mysql_num_rows($get_cats_res) < 1) {
16:    $display_block = "<P><em>Sorry, no categories to browse.</em></p>";
17: } else {
19:    while ($cats = mysql_fetch_array($get_cats_res)) {
20:        $cat_id = $cats[id];
21:        $cat_title = strtoupper(stripslashes($cats[cat_title]));
22:        $cat_desc = stripslashes($cats[cat_desc]);
24:         $display_block .= "<p><strong><a
25:         href=\"$_SERVER[PHP_SELF]?cat_id=$cat_id\">$cat_title</a></strong>
26:         <br>$cat_desc</p>";
28:        if ($_GET[cat_id] == $cat_id) {
29:            //get items
30:            $get_items = "select id, item_title, item_price
31:            from store_items where cat_id = $cat_id
32:             order by item_title";
33:            $get_items_res = mysql_query($get_items) or die(mysql_error());
35:            if (mysql_num_rows($get_items_res) < 1) {
36:               $display_block = "<P><em>Sorry, no items in
37:                this category.</em></p>";
38:            } else {
40:                $display_block .= "<ul>";
42:                while ($items = mysql_fetch_array($get_items_res)) {
43:                    $item_id = $items[id];
44:                    $item_title = stripslashes($items[item_title]);
45:                    $item_price = $items[item_price];
47:                    $display_block .= "<li><a
48:                     href=\"showitem.php?item_id=$item_id\">$item_title</a>
49:                     </strong> (\$$item_price)";
50:                }
52:                $display_block .= "</ul>";
53:             }
54:         }
55:     }
56: }
57: ?>
58: <HTML>
59: <HEAD>
60: <TITLE>My Categories</TITLE>
61: </HEAD>
62: <BODY>
63 :<?php echo $display_block; ?>
64: </BODY>
65: </HTML>

Given the length of scripts you saw in Chapter 19, these 65 fully functional lines should be a welcome change. In lines 35, the database connection is opened because regardless of which action the script is takingshowing categories or showing items in categoriesthe database is necessary.

In lines 78, the $display_block string is started, with some basic page title information. Lines 1113 create and issue the query to retrieve the category information. Line 15 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 19, which begins a while loop to extract the information.

In the while loop, lines 2022 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 2426 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 28.

If a $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 3033) to retrieve the items in the category. Lines 4253 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 21.1 shows the outcome of the script when accessed directly; only the category information shows.

Figure 21.1. Categories in the store.

In Figure 21.2, you see what happens when the user clicked on the HATS link: The script gathered all the items associated with the category and printed 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 21.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.

    Team LiB
    Previous Section Next Section

    JavaScript Editor Javascript source editor     Web programming