codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  grouping records  serafina at 12:28 on Thursday, August 19, 2004
 

I have a list in a database that I have categorised using a category field. I now want to retrieve all records but grouped by categories. how do i get my asp to loop through each category creating a recordset for each one rather than hard code a new recordset for each category i would rather it did it dynamically if thats possible ???

so eg

category travel
1 - B&B's records (recid 1)
2- rail info (recid 2)

category local info
1 - swimming baths (recid 3)
2 - museum info (recid 4)


etc etc

cheers

  Re: grouping records  Troy Wolf at 19:07 on Monday, August 23, 2004
 

The only way to get a recordset per category is to do a seperate query per category . If you want to do just one query (which is what I try to do), then simply select your data sorted by category, then process the recordset in order and write code to handle when the category changes. Something like this:

select category _name, company_id, company_name
from company
order by category_name, company_name

Then as you move through the recordset keep track of the last category so you can know if the current category is the same or the start of a new category.

Does that help?
Troy Wolf: site expert
Shiny Solutions


  Re: grouping records  serafina at 09:52 on Wednesday, August 25, 2004
 

Thanks

So basically the answer is a new Recordset query for each category - probably easier to hand code rather than pick up dynamically ?

pain because I was hoping there was a loop technique I could have used

  Re: grouping records  Troy Wolf at 15:08 on Wednesday, August 25, 2004
 

If you truly want a "recordset per category" then you'll have to run a query per category. However, depending on the size of your data and the speed of the db server, network, etc.....you may prefer to run one query, then process the data in your ASP.

Running one query per category is probably the easiest, but probably not the most efficient. I prefer to run one query, ordered by category. (Like the example query I showed in my previous reply.)

Then you can process the data sequentially kind of like this: (This is not tested code, I just typed it into this reply as an example of the logic.)

rowCnt = 0
while not rs.eof
rowCnt = rowCnt + 1
category = rs.fields("category")
company = rs.fields("company")
if category <> lastCategory then
if rowCnt > 1 then
'Enter logic to wrap up whatever you need to do for the previous category. For example, write out your end table row and table tags.
end if
Response.Write("<h1>" & Category & "</h1>" & vbcrlf)
lastCategory = category
end if
Response.Write(company & "<br>" & vbcrlf)
rs.MoveNext
wend

This logic loops through the recordset, and for each new category, it ends the previous section (unless it is the first category), writes out the new category as a heading, then lists the companies in that category, then moves onto the next category and so on. See?
Troy Wolf: site expert
Shiny Solutions









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Page print
•  Re: Export database`s data into a CSV file in jspscript.....
•  Passing Remote Computer Name to Access Query
•  dynamically created tables containing layers
•  PDF
•  Re: How do I make the child close automatically after 3 seconds ?
•  Re: grouping records
•  Wrap Counter??
•  Login and Redirect -- Help Please


Recent Articles
Simple Thumbnail Solution
Type Anywhere
A Better Moustrap: FmtDate to replace FormatDateTime
ASP.NET Forum Source Code
Internal Search Engine
Javascript Growing Window
Simple date validation
Search engine friendly URLs using ASP.NET (C#.NET)
Function to Return Alpha Characters Only
The OSI Reference Model - A Clear and Concise Illustration !


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2004