Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Listing Items Under Categories
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Listing Items Under Categories

 Post Reply Post Reply
Author
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post Topic: Listing Items Under Categories
    Posted: 21 January 2004 at 7:56pm

Let's say that I have the following tables in a database:

Categories: CategoryID, Category

Products: ProductID, CategoryID, ProductName

Let's say that there are 4 categories. I would like to divide the products into 4 categories on a web page.

I would like for it to look something like this:

Gifts

ProductName

ProductName

Ornaments

ProductName

ProductName

I know that I could accomplish this by creating 4 different recordsets on the web page. But I wonder if there is a way to do this with only one recordset. Can someone please help me with this? Does this make sense?

 

 



Edited by Misty
Back to Top
ljamal View Drop Down
Mod Builder Group
Mod Builder Group


Joined: 16 April 2003
Status: Offline
Points: 888
Post Options Post Options   Thanks (0) Thanks(0)   Quote ljamal Quote  Post ReplyReply Direct Link To This Post Posted: 21 January 2004 at 8:46pm
Join the tables together and run one query sorted by category and product names

select category.*, product.* from product, category where
category.categoryID = product.categoryID order by category.name, product.name

Store the categoryID as you loop through the record and only display the category info when you come upon a new categoryID
Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post Posted: 24 January 2004 at 2:51pm

I'm lost. I am not sure how to do this. I have no problems with joining the tables and sorting them. I have tried different things. Please look at the below code:

      'Start sql string with Join

     sqlString = "select * from Category, Products"

     sqlString = sqlString & " where Category.CategoryID=Products.CategoryID order by Category, ProductName"

    

     'Create the recordset

      set rs = Server.CreateObject("ADODB.Recordset")

      rs.Open sqlString, connectionString, adOpenDynamic, adLockOptimistic

     

%>   

 

I want the Category to show up here. But I have been unsuccessful so far. I would like for the category to display only one time for a section of products that are under this category. I need some help with the code.

  

      <!-- Draw the table headings -->

      <TABLE BORDER=1>

      <TR>

          

           <TH>ProductName</TH>

           <TH>Description </TH>

          

      </TR>

 

<% 

           'Set the current record pointer to the first record of the recordset

           rs.MoveFirst

          

           'Loop through all the records in the recordset

           while not rs.EOF

          

                 'Get values

                

                 Category = rs("Category") & ""

                 ProductName = rs("ProductName") & ""

                 Description = rs("Description") & ""

              

        

Back to Top
ljamal View Drop Down
Mod Builder Group
Mod Builder Group


Joined: 16 April 2003
Status: Offline
Points: 888
Post Options Post Options   Thanks (0) Thanks(0)   Quote ljamal Quote  Post ReplyReply Direct Link To This Post Posted: 24 January 2004 at 3:20pm
Try this. I didn't run the script for error checking but it should point you in the right direction

<%
     'Start sql string with Join
     sqlString = "select * from Category, Products "&_
                  "where Category.CategoryID=Products.CategoryID"&_
                  " order by Category, ProductName"

     'Create the recordset
     set rs = Server.CreateObject("ADODB.Recordset")
     rs.Open sqlString, connectionString, adOpenDynamic, adLockOptimistic

     intCatID = 0

     Do while not rs.EOF
             ' Check if Category Name has alreayd been displayed
             if intCatID<>Clng(rs("CategoryID")) then
                  intCatID = Cng(rs("CategoryID"))

                   'Close table if a table is open
                        if intCatID > 0 then
                              Response.Write "</table>"
                        end if
                   ' Display Category Name and Open Table
                  Response.Write rs("Category")
                  Response.Write "<!-- Draw the table headings -->"&vbcrlf&_
                                     " <table borde=1>"&vbcrlf&_
                                     " <tr><th>Product Name</th>"&_
                                     " <th>Description</th></tr>"
             end if
             
                   ' Write Product information
                  Response.Write "<tr><td>"&rs("ProductName")&"</td>"&_
                                     " <td>"&rs("Description")&"</td></tr>"&vbcrl f
                   
             rs.MoveNext
     Loop

     'Close Open table
             Response.Write </table">

     'Close the recordset
     rs.Close
     Set rs = Nothing
%>



Edited by ljamal
Back to Top
Misty View Drop Down
Senior Member
Senior Member
Avatar

Joined: 06 February 2002
Location: United States
Status: Offline
Points: 711
Post Options Post Options   Thanks (0) Thanks(0)   Quote Misty Quote  Post ReplyReply Direct Link To This Post Posted: 24 January 2004 at 10:42pm
Thank you! I have it working now.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.