Category Listings

When we first come to the store, we of course will want to know what's for sale. Because we're concentrating on the technology, we won't worry too much about the presentation, but we will need a single start page for the store, in this case bazaar.asp. This page can have any specials that we're running, but should also list (at a minimum) the categories of products that we have for sale. We already have a file called bazaar.asp in the bazaar directory, but let's overwrite it with a copy of template.asp anyway because it already has the database information that we need. We customize bazaar.asp for the Bazaar and add the category listings in Listing 7.2.

Code Listing 7.2. bazaar.asp: Adding Category Listings
0: <%@ LANGUAGE="VBSCRIPT" %>
1: <!--#include file="adovbs.inc"-->
2: <!--#include virtual="/pagetop.txt"-->
3: <%
4:     set outpostDB = Server.CreateObject("ADODB.Connection")

5:     outpostDB.Open "outpost"
6: %>
7: <H1>The Bazaar</H1>
8: If you can't find it here, you can't find it anywhere in the
9: universe!
10:<P>
11:<CENTER>
12:<%
13:     set catSet = outpostDB.Execute("select * from baz_categories")
14:     while not catSet.EOF
15:%>
16:         <%= catSet("cat_name") %><BR>
17:<%
18:         catSet.MoveNext
19:     wend
20:     catSet.Close
21:     set catSet = Nothing
22:
23:     outpostDB.Close
24:     set outpostDB = Nothing
25:%>
26:<P>
27:</CENTER>
28:<!--#include virtual="/pagebottom.txt"-->
29:</BODY>
30:</HTML>

We haven't done anything new or special here, just looped through the list of categories and printed them to the page. The next thing that we're going to want to do is make those listings links. When we click the link, we want to get a page that lists all of the products in that category.

Now let's think about this. The best name for that page will probably be something like baz_category_products.asp or some such thing, and we'll probably want to pass it a parameter, such as what category we're looking at, as indicated by the cat_id. We could go ahead and add the links right now, but let's think for another moment. (Just because we're not doing detailed plans doesn't mean we should turn our brain off entirely!) When we get to this page, what are we going to want to see? We would like to see the products, of course, but what about the name of the category? Sure, we could use the cat_id to look it up when we get there, but why should we incur all that extra overhead when we already have the name and we can just pass it as part of the URL along with the category ID?

The point I'm trying to make is that we should always be looking for ways to improve performance and avoid duplicating effort. Naturally, we wouldn't want to pass tons of information as part of the URL, but this is one time where you'd have a hard time convincing me that we shouldn't do it. So let's go ahead and add those links, as shown in Listing 7.3.

Code Listing 7.3. bazaar.asp: Adding category links
…
11: <%
12:    set catSet = outpostDB.Execute("select * from baz_categories")
13:    while not catSet.EOF
14: %>
15:        <A HREF="baz_category_products.asp?p_cat_id=<%
16:            Response.Write catSet("cat_id") %>&p_cat_NAME=<%
17:            Response.Write Server.URLencode(catSet("cat_name"))%>">
18:        <%= catSet("cat_name") %>
19:        </A>
20:        <BR>
21: <%
22:        catSet.MoveNext
23:    wend
24:    catSet.Close
25:    set catSet = Nothing
26: …

We've created the link, and we've fed it, as parameters, p_cat_id and p_cat_name. Remember, though, because the category name is to become part of the URL, we need to make sure we don't run into problems if we have a category with, say, a space in it. Therefore, on line 17 we use Server.URLEncode to prevent problems with the link.

So now we have our main page, and we're ready to go ahead and create our category pages. Go ahead and copy template.asp to baz_category_products.asp in the bazaar directory. In this case, we're going to list the products, but we already know where we're going to be linking to the product page. So let's go ahead and add the information while we're there, as in Listing 7.4.

Code Listing 7.4. baz_category_products.asp: Listing the category's products
0: <%@ LANGUAGE="VBSCRIPT" %>
1: <!--#include file="adovbs.inc"-->
2: <!--#include virtual="/pagetop.txt"-->
3: <%
4:     p_cat_id = Request.querystring("p_cat_id")
5:     p_cat_name = Request.querystring("p_cat_name")
6: %>
7: <H2 ALIGN="center"><%=p_cat_name%></H2>
8:
9: <%
10:    set outpostDB = Server.CreateObject("ADODB.Connection")
11:    outpostDB.Open "outpost"
12:
13:    set prodInfoSet = Server.CreateObject("ADODB.RecordSet")
14:    prodInfoSet.Open "baz_products", outpostDB, adOpenStatic, _
15:        adCmdTable
16:
17:    set prodIDSet = outpostDB.Execute("select prod_id from " _
18:        & "baz_product_categories where cat_id = "&p_cat_id)
19:    while not prodIDSet.EOF
20:        p_prod_id = prodIDSet("prod_id")
21:        prodInfoSet.Find "prod_id = " & p_prod_id
22:%>
23:        <A HREF="baz_product.asp?p_prod_id=<%= p_prod_id%>">
24:            <%= prodInfoSet("prod_name") %>
25:        </A>
26:        <BR>
27:
28:<%        prodIDSet.MoveNext
29:    wend
30:
31:    prodInfoSet.Close
32:    set prodInfoSet = Nothing
33:
34:    outpostDB.Close
35:    set outpostDB = Nothing
36:%>
37:<P>
38:<!--#include virtual="/pagebottom.txt"-->
39:</BODY>
40:</HTML>

The first thing that we're doing here is extracting the information from the previous page on lines 4 and 5 and displaying our header telling the user what category they're looking at on line 7.

Next we're going to make a choice. To find out what products to display, we're going to go to the baz_category_products table, which will give us the appropriate prod_id numbers on lines 17 and 18. After we have them, though, we will need to get the rest of the information, such as the name of the product. We could do a search of the baz_product table for each and every item, but that would be a terrible waste of resources.

Instead, on lines 14 and 15 we're creating a static recordset with all of our products. This way ASP has to go to the database for this information only once.

Then, on lines 19 through 29, we loop through all of the records in baz_product_categories that have the right cat_id. For each one, we let ASP find the right record on line 21, and we display the appropriate information, including a link to a product information page.

Now we're ready to tackle baz_product.asp. Since we are already retrieving all of the product information from the database, we could pass it all through this URL and save ourselves a trip to the database after we get to baz_product.asp. That, however, would be too much of a good thing, in my opinion, for two reasons. First, that's a bit too much information to be passing on a regular basis. Second, the product information page is one place where you want to make sure that all the information is as up to date as possible. If you included all of the product information in the URL, a user could bookmark the page and any changes, such as a change in price, wouldn't be reflected. Once again copy template.asp, this time to baz_product.asp in the auction directory. Add the highlighed code in Listing 7.5.

Code Listing 7.5. The product information page
0: <%@ LANGUAGE="VBSCRIPT" %>
1: <!--#include file="adovbs.inc"-->
2: <!--#include virtual="/pagetop.txt"-->
3:  <%
4:     p_prod_id = Request.querystring("p_prod_id")
001 5:
002 6:     set outpostDB = Server.CreateObject("ADODB.Connection")

7:     outpostDB.Open "outpost"
8:
9:     set prodInfoSet = outpostDB.Execute("select * from " & _
10:         baz_products where prod_id="&p_prod_id)
11:     if prodInfoSet.EOF then
12:         Response.Write "Product Number " & p_prod_id & _
13:             " does not exist."
14:     else
15:         p_name = prodInfoSet("prod_name")
16:         p_desc = prodInfoSet("prod_desc")
17:         p_image = prodInfoSet("prod_image")
18:         p_price = prodInfoSet("prod_price")
19:
20: %>
21:         <CENTER>
22:         <H2><%= p_name %></H2>
23:         <%= p_desc %>
24:         <P>
25:         <IMG SRC="/images/products/<%= p_image %>">
26:         <P>
27:         Price: $<%= formatNumber(p_price, 2) %>
28:         <P>
29:         </CENTER>
30: <%
31:    end if
32:    prodInfoSet.Close
33:    set prodInfoSet = Nothing
34:
35:    outpostDB.Close
36:  set outpostDB = Nothing
37:%>
38:<!--#include virtual="/pagebottom.txt"-->
39:</BODY>
40:</HTML>

On line 4 we retrieve the product ID, then on lines 9 and 10 we look for the product in our database. If it's not there, we return a message one lines 12 and 13. If it is there, we're going to print out the information about it on lines 15 through 29. We didn't have to extract the information into variables, but it sure makes our code a whole lot easier to read.

Congratulations! You've created an online catalog! You should feel a tremendous sense of accomplishment. In the early years of the Web, people got paid tens, or even hundreds of thousands of dollars to do what you just did with three pages and a few database tables. Ok, so it's not a very good catalog and those people put considerably more time, effort, and design skills into it, but you should feel good anyway.

Let's look at how we can make this a much better catalog. The most obvious problem is that there's currently no way to actually order anything, but before we get to that point, let's tweak this product page just a tiny bit to make the catalog a little more enticing.

One thing to keep in mind is that there is an entire industry devoted to the idea that if you can deduce what the customer is likely to want next, you can more easily sell it to them. This is called, depending on the context, database marketing, one-to-one marketing, customer relationship management, or whatever the catch phrase happens to be this week.

You can see it at work on some of the larger sites. Probably the most obvious is at Amazon.com. Let's say that you did a search and were looking at the description page for Special Edition: Using Oracle 8/8I. It's simple enough to offer links to various subcategories of Computers and Internet, which they do. But they also make note of what else people have bought when they've bought this book. So if you like this author, you'll probably like that author. If you're interested in this book, you'll probably also be interested in that one.

This is a science in and of itself and we're not going to get that in-depth, but it's something to remember when you're putting together your strategy. It would be a simple matter for us to point people back to relevant items by category, as we did in Listing 7.6.

Code Listing 7.6. Adding related categories
…
28:
29:        </CENTER>
30:<%
31:     end if
32:     prodInfoSet.Close
33:     set prodInfoSet = Nothing
34:%>
35:     <H3>You can find products similar to this one in:</H3>
36:<%
37:     set catSet = Server.CreateObject("ADODB.RecordSet")
38:     catSet.Open "baz_categories", outpostDB, adOpenStatic,
39:         adLockOptimistic, adCmdTable
40:
41:     set prodCatSet = outpostDB.Execute("select * from " _
42:         & "baz_product_categories where prod_id=" & p_prod_id)
43:     while not prodCatSet.EOF
44:     catSet.MoveFirst
45:     catSet.Find "cat_id = " & prodCatSet("cat_id")
46:%>
47:     <A HREF="baz_category_products.asp?p_cat_id=<%
48:         Request.Write catSet("cat_id") 8%>&p_cat_NAME=<%
49:         Request.Write Server.URLencode(catSet("cat_name"))%>">
50:     <%= catSet("cat_name") %>
51:     </A>
52:     <BR>
53:<%
54:      prodCatSet.MoveNext
55:  wend
56:  prodCatSet.Close
57:  set ProdCatSet = Nothing
58:
59:  catSet.Close
60:  set catSet = Nothing
61:
62:  outpostDB.Close
63:  set outpostDB = Nothing
64:%>
65:<P>
66:<!--#include virtual="/pagebottom.txt"-->
67:</BODY>
68:</HTML>

After we display the product information, on line 37 we're creating a recordset with all the category information, so we can display it when the time comes—kind of like the category products page, only in reverse.

Then, rather than selecting all of the products in the category, we're selecting all of the categories for the product. We have one table, baz_product_categories, and two great things we can do with it. After we have the categories, we're displaying them along with a link to a listing of all the products in that category. These lines, 47 through 51, were actually taken from bazaar.asp, without the slightest modification. (Of course, that's because we used the same names for our recordsets.)

The nice thing about dynamically generated pages like these is that after we make the change to the product page, it takes effect for all products. Go ahead and surf through your products. You'll be able to go from category to category, right from the product pages.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.152.26