Displaying Items


The item display page in this chapter simply shows all the item information. In Chapter 23, "Creating a Shopping Cart Mechanism," you'll add a few lines to it to make it function with an "add to cart" button. So for now, just assume this is a paper catalog.

Listing 22.2 shows the code for showitem.php.

Listing 22.2. Script to View Item Information

1:  <?php 2:  //connect to database 3:  $mysqli = mysqli_connect("localhost", "joeuser", "somepass", "testDB"); 4: 5:  $display_block = "<h1>My Store - Item Detail</h1>"; 6: 7:  //validate item 8:  $get_item_sql = "SELECT c.id as cat_id, c.cat_title, si.item_title, 9:                  si.item_price, si.item_desc, si.item_image FROM store_items 10:                 AS si LEFT JOIN store_categories AS c on c.id = si.cat_id 11:                 WHERE si.id = '".$_GET["item_id"]."'"; 12: $get_item_res = mysqli_query($mysqli, $get_item_sql) 13:                 or die(mysqli_error($mysqli)); 14: 15: if (mysqli_num_rows($get_item_res) < 1) { 16:     //invalid item 17:     $display_block .= "<p><em>Invalid item selection.</em></p>"; 18: } else { 19:     //valid item, get info 20:    while ($item_info = mysqli_fetch_array($get_item_res)) { 21:        $cat_id = $item_info['cat_id']; 22:        $cat_title = strtoupper(stripslashes($item_info['cat_title'])); 23:        $item_title = stripslashes($item_info['item_title']); 24:        $item_price = $item_info['item_price']; 25:        $item_desc = stripslashes($item_info['item_desc']); 26:        $item_image = $item_info['item_image']; 27:    } 28: 29:    //make breadcrumb trail 30:    $display_block .= "<p><strong><em>You are viewing:</em><br/> 31:    <a href=\"seestore.php?cat_\">".$cat_title."</a> 32:    &gt; ".$item_title."</strong></p> 33:    <table cellpadding=\"3\" cellspacing=\"3\"> 34:    <tr> 35:    <td valign=\"middle\" align=\"center\"> 36:    <img src=\"".$item_image."\"/></td> 37:    <td valign=\"middle\"><p><strong>Description:</strong><br/>". 38:    $item_desc."</p> 39:    <p><strong>Price:</strong> \$".$item_price."</p>"; 40: 41:    //free result 42:    mysqli_free_result($get_item_res); 43: 44:    //get colors 45:    $get_colors_sql = "SELECT item_color FROM store_item_color WHERE 46:                       item_id = '".$_GET["item_id"]."' ORDER BY item_color"; 47:    $get_colors_res = mysqli_query($mysqli, $get_colors_sql) 48:                      or die(mysqli_error($mysqli)); 49: 50:    if (mysqli_num_rows($get_colors_res) > 0) { 51:        $display_block .= "<p><strong>Available Colors:</strong><br/>"; 52:        while ($colors = mysqli_fetch_array($get_colors_res)) { 53:            item_color = $colors['item_color']; 54:            $display_block .= $item_color."<br/>"; 55:        } 56:    } 57:    //free result 58:    mysqli_free_result($get_colors_res); 59: 60:    //get sizes 61:    $get_sizes_sql = "SELECT item_size FROM store_item_size WHERE 62:                     item_id = ".$_GET["item_id"]." ORDER BY item_size"; 63:    $get_sizes_res = mysqli_query($mysqli, $get_sizes_sql) 64:                     or die(mysqli_error($mysqli)); 65: 66:    if (mysqli_num_rows($get_sizes_res) > 0) { 67:       $display_block .= "<p><strong>Available Sizes:</strong><br/>"; 68:       while ($sizes = mysqli_fetch_array($get_sizes_res)) { 69:           $item_size = $sizes['item_size']; 70:           $display_block .= $item_size."<br/>"; 71:       } 72:    } 73:    //free result 74:    mysqli_free_result($get_sizes_res); 75: 76:    $display_block .= " 77:    </td> 78:    </tr> 79:    </table>"; 80: } 81: ?> 82: <html> 83: <head> 84: <title>My Store</title> 85: </head> 86: <body> 87: <?php echo $display_block; ?> 88: </body> 89: </html>

In line 3, the database connection is made because information in the database forms all the content of this page. In line 5, the $display_block string is started, with some basic page title information.

Lines 813 create and issue the query to retrieve the category and item information. This particular query is a table join. Instead of selecting the item information from one table and then issuing a second query to find the name of the category, this query simply joins the table on the category ID to find the category name.

Line 15 checks for a result; if there is no matching item in the table, a message is printed to the user and that's all this script does. However, if item information is found, the script moves on and gathers the information in lines 2027.

In lines 3032, you create what's known as a breadcrumb trail. This is simply a navigational device used to get back to the top-level item in the architecture. Those are fancy words that mean "print a link so that you can get back to the category." The category ID, retrieved from the master query in this script, is appended to the link in the breadcrumb trail.

In lines 3339, you continue to add to the $display_block, setting up a table for information about the item. You use the values gathered in lines 2126 to create an image link, print the description, and print the price. What's missing are the colors and sizes, so lines 4456 select and print any colors associated with this item, and lines 6172 gather the sizes associated with the item.

Lines 7680 wrap up the $display_block string and the master if...else statement, and because the script has nothing left to do, it prints the HTML (lines 8289) including the value of $display_block. Figure 22.3 shows the outcome of the script when selecting the baseball hat from the hats category. Of course, your display will differ from mine, but you get the idea.

Figure 22.3. The baseball hat item page.


That's all there is to creating a simple item display. In the Chapter 23, you'll modify this script so that it can add the item to a shopping cart.




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

Similar book on Amazon

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