Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grouping results 1

Status
Not open for further replies.

fiber0pti

ISP
Nov 22, 2000
96
US
I'm trying to output some products on my webpage. Is there anyway to have a header that has the company and then list the products under that company header and so on. There would be mutiple companies with mutiple products under each company. Is there a way to do all of this with one query?
 
Usually this can be done with Joins.

I don't know if you've tried them or what you know about them but you can join tables ON a particular field and thus gather more information with one query.

They often look something like this:
<CFQUERY name=&quot;TEST&quot; datasource=&quot;#DSN#&quot;>
Select table1.*, table2.*
From table1 INNER JOIN table2
ON table1.id = table2.ref_id
</CFQUERY>

so if your product has a field that holds the ID of the company it comes from, Inner Join on that field and you'll have it. IF this isn't working for you , post some code and let us know what the fields are and we'll try to help more.
 
Hey fiber,

You want to use the group attribute with <cfoutput> but you have to make sure to include the &quot;grouped&quot; field in your sql &quot;order by clause&quot;. Here's an example:

<cfquery name=&quot;q1&quot; ...>

select companyName,productName from table1

order by companyName asc, productName asc

</cfquery>

<cfoutput query=&quot;q1&quot; group=&quot;companyName&quot;>
#companyName#
<hr>
<cfoutput>
#productName#<br>
</cfoutput>
<hr>
</cfoutput>

Everything within the inner <cfoutput> tags is repeated for each record and everything outside of them is repeated for every new value in the field set in the &quot;group&quot; attribute.

Let me know if you have any trouble with it,
GJ
 
Yeah, no prob.

Looks like we covered different aspects of the same question this time. :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top