Displaying Items

The item display page in this hour will simply show all the item information. In the next hour, 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 20.2 shows the code for showitem.php.

Listing 20.2 Script to View Item Information
   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 Store - Item Detail</h1>";   8:    9: //validate item  10: $get_item = "select c.cat_title, si.item_title, si.item_price,  11: si.item_desc, si.item_image from store_items as si left join  12: store_categories as c on c.id = si.cat_id where si.id = $_GET[item_id]";  13:   14: $get_item_res = mysql_query($get_item) or die (mysql_error());  15:   16: if (mysql_num_rows($get_item_res) < 1) {  17:    //invalid item  18:    $display_block .= "<P><em>Invalid item selection.</em></p>";  19: } else {  20:    //valid item, get info  21:    $cat_title = strtoupper(stripslashes(  22:         mysql_result($get_item_res,0,'cat_title')));  23:    $item_title = stripslashes(mysql_result($get_item_res,0,'item_title'));  24:    $item_price = mysql_result($get_item_res,0,'item_price');  25:    $item_desc = stripslashes(mysql_result($get_item_res,0,'item_desc'));  26:    $item_image = mysql_result($get_item_res,0,'item_image');  27:   28:    //make breadcrumb trail  29:    $display_block .= "<P><strong><em>You are viewing:</em><br>  30:    <a href=\"seestore.php?cat_id=$cat_id\">$cat_title</a>  31:     &gt; $item_title</strong></p>  32:   33:    <table cellpadding=3 cellspacing=3>  34:    <tr>  35:    <td valign=middle align=center><img src=\"$item_image\"></td>  36:    <td valign=middle><P><strong>Description:</strong><br>$item_desc</p>  37:    <P><strong>Price:</strong> \$$item_price</p>";  38:   39:    //get colors  40:    $get_colors = "select item_color from store_item_color where  41:     item_id = $item_id order by item_color";  42:    $get_colors_res = mysql_query($get_colors) or die(mysql_error());  43:   44:    if (mysql_num_rows($get_colors_res) > 0) {  45:   46:        $display_block .= "<P><strong>Available Colors:</strong><br>";  47:   48:        while ($colors = mysql_fetch_array($get_colors_res)) {  49:            $item_color = $colors['item_color'];  50:   51:            $display_block .= "$item_color<br>";  52:        }  53:    }  54:   55:    //get sizes  56:    $get_sizes = "select item_size from store_item_size where  57:         item_id = $item_id order by item_size";  58:    $get_sizes_res = mysql_query($get_sizes) or die(mysql_error());  59:   60:    if (mysql_num_rows($get_sizes_res) > 0) {  61:  62:        $display_block .= "<P><strong>Available Sizes:</strong><br>";  63:   64:        while ($sizes = mysql_fetch_array($get_sizes_res)) {  65:            $item_size = $sizes['item_size'];  66:   67:            $display_block .= "$item_size<br>";  68:        }  69:    }  70:   71:    $display_block .= "  72:    </td>  73:    </tr>  74:    </table>";  75:   76: }  77: ?>  78: <HTML>  79: <HEAD>  80: <TITLE>My Store</TITLE>  81: </HEAD>  82: <BODY>  83: <? print $display_block; ?>  84: </BODY>  85: </HTML> 

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

Lines 10 14 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 16 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 21 26.

In lines 29 31, 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 you can get back to the category."

In lines 33 37, you continue to add to the $display_block, setting up a table for information about the item. You use the values gathered in lines 21 26 to create an image link, print the description, and print the price. What's missing are the colors and sizes, so lines 39 53 select and print any colors associated with this item, and lines 55 69 gather the sizes associated with the item.

Lines 71 74 wrap up the $display_block string, and because the script has nothing left to do, it prints the HTML and value of $display_block. Figure 20.3 shows the outcome of the script when selecting the cowboy hat from the hats category. Of course, your display will differ from mine, but you get the idea.

Figure 20.3. The cowboy hat item page.

graphics/20fig03.gif

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



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

Similar book on Amazon

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