JavaScript Editor Javascript source editor     Web programming 

Team LiB
Previous Section Next Section

Displaying Items

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

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

In lines 35, 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 1014 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 2127.

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 3438, you continue to add to the $display_block, setting up a table for information about the item. You use the values gathered in lines 2127 to create an image link, print the description, and print the price. What's missing are the colors and sizes, so lines 4052 select and print any colors associated with this item, and lines 5466 gather the sizes associated with the item.

Lines 6872 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 7481) including the value of $display_block. Figure 21.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 21.3. The baseball hat item page.

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

    Team LiB
    Previous Section Next Section

    JavaScript Editor Javascript source editor     Web programming