Displaying Categories of Items

Believe it or not, the most difficult task in this project is finished. 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 20.1 shows the code for seestore.php.

Listing 20.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());   6:    7: $display_block = "<h1>My Categories</h1>   8: <P>Select a category to see its items.</p>";   9:   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());  14:   15: if (mysql_num_rows($get_cats_res) < 1) {  16:    $display_block = "<P><em>Sorry, no categories to browse.</em></p>";  17: } else {  18:   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]);  23:   24:         $display_block .= "<p><strong><a  25:         href=\"$_SERVER[PHP_SELF]?cat_id=$cat_id\">$cat_title</a></strong>  26:         <br>$cat_desc</p>";  27:  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());  34:   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 {  39:   40:                 $display_block .= "<ul>";  41:   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];  46:   47:                     $display_block .= "<li><a  48:                      href=\"showitem.php?item_id=$item_id\">$item_title</a>  49:                      </strong> (\$$item_price)";  50:                 }  51:   52:                 $display_block .= "</ul>";  53:            }  54:         }  55:     }  56: }  57: ?>  58: <HTML>  59: <HEAD>  60: <TITLE>My Categories</TITLE>  61: </HEAD>  62: <BODY>  63: <? print $display_block; ?>  64: </BODY>  65: </HTML> 

Given the length of scripts in Hour 19, these 65 fully functional lines should be a welcome change. In lines 3 5 the database connection is opened, because regardless of which action the script is taking showing categories or showing items in categories the database is necessary.

In lines 7 8, the $display_block string is started, with some basic page title information. Lines 11 13 create and issue the query to retrieve the category information. Line 15 checks for categories; if none were 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 20 22 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 24 26 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 30 33) to retrieve the items in the category. Lines 42 53 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 20.1 shows the outcome of the script when accessed directly; only the category information shows.

Figure 20.1. Categories in the store.

graphics/20fig01.gif

In Figure 20.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 20.2. Items within a category in the store.

graphics/20fig02.gif

The last piece of the puzzle for this hour is the creation of the item display page.



Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
ISBN: 067232489X
EAN: 2147483647
Year: 2005
Pages: 263

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net