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 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

 1:  <?php 2:  //connect to database 3:  $mysqli = mysqli_connect("localhost", "joeuser", "somepass", "testDB"); 4: 5:  $display_block = "<h1>My Categories</h1> 6:  <p>Select a category to see its items.</p>"; 7: 8:  //show categories first 9:  $get_cats_sql = "SELECT id, cat_title, cat_desc FROM 10:                 store_categories ORDER BY cat_title"; 11: $get_cats_res =  mysqli_query($mysqli, $get_cats_sql) 12:                   or die(mysqli_error($mysqli)); 13: 14: if (mysqli_num_rows($get_cats_res) < 1) { 15:      $display_block = "<p><em>Sorry, no categories to browse.</em></p>"; 16: } else { 17:     while ($cats = mysqli_fetch_array($get_cats_res)) { 18:         $cat_id  = $cats['id']; 19:         $cat_title = strtoupper(stripslashes($cats['cat_title'])); 20:         $cat_desc = stripslashes($cats['cat_desc']); 21: 22:         $display_block .= "<p><strong><a href=\"".$_SERVER["PHP_SELF"]. 23:        "?cat_\">".$cat_title."</a></strong><br/>" 24:         .$cat_desc."</p>"; 25: 26:         if (isset($_GET["cat_id"])) { 27:             if ($_GET["cat_id"] == $cat_id) { 28:                 //get items 29:                 $get_items_sql = "SELECT id, item_title, item_price FROM 30:                 store_items WHERE cat_id = '".$cat_id."' 31:                 ORDER BY item_title"; 32:                 $get_items_res = mysqli_query($mysqli, $get_items_sql) 33:                                  or die(mysqli_error($mysqli)); 34: 35:                 if (mysqli_num_rows($get_items_res) < 1) { 36:                     $display_block = "<p><em>Sorry, no items in this 37:                     category.</em></p>"; 38:                 } else { 39:                     $display_block .= "<ul>"; 40:                     while ($items = mysqli_fetch_array($get_items_res)) { 41:                         $item_id  = $items['id']; 42:                         $item_title = stripslashes($items['item_title']); 42:                         $item_price = $items['item_price']; 42: 42:                         $display_block .= "<li><a 42:                         href=\"showitem.php?item_\">".$item_title."</a></strong> 44:                         (\$".$item_price.")</li>"; 45:                     } 46: 47:                     $display_block .= "</ul>"; 48:                 } 49:                 //free results 50:                 mysqli_free_result($get_items_res); 51:             } 52:         } 53:     } 54: } 55: //free results 56: mysqli_free_result($get_cats_res); 57: //close connection to MySQL 58: mysqli_close($mysqli); 59: ?> 60: <html> 61: <head> 62: <title>My Categories</title> 63: </head> 64: <body> 65: <?php echo $display_block; ?> 66: </body> 67: </html>

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.

Sams Teach Yourself PHP, MySQL And Apache All in One
Sams Teach Yourself PHP, MySQL and Apache All in One (3rd Edition)
ISBN: 0672328739
EAN: 2147483647
Year: 2004
Pages: 327

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