Reputation: 1257
I have a CFQuery to pull data from a table which I would like to output to the screen in a format that groups the data according to one of the columns, 'Company Name'. I can't seem to wrap my head around the logic for this.
Currently I'm just looping through the data to output it to the screen and separating it by a horizontal rule tag. Not the best looking way to do it and it generates a really long list of results that the user needs to scroll through. I am hoping that by grouping the data, it will be more readable.
Here is my code:
<!--- Feedback Query --->
<cfquery name="getFeedback" datasource="#datasource#">
select ticket_id, service_satisfaction, customer_notes, response_date, company_name
from service_verification
order by company_name
</cfquery>
<br />
<cfoutput query="getFeedback" group="company_name"><strong>Company Name: #getFeedback.company_name#</strong><br />
<cfquery dbtype="query" name="parsed">
select company_name
from getFeedback
where company_name = '#getFeedback.company_name#'
</cfquery>
(#parsed.recordcount# Responses)<br />
<cfoutput>
Ticket Number: <cfif #getFeedback.ticket_id# eq 0>No ticket associated with this feedback. This was solicited feedback.<cfelse>#getFeedback.ticket_id#</cfif><br />
Date: #DateFormat(getFeedback.response_date, 'mm/dd/yyyy')# at #TimeFormat(getFeedback.response_date, 'hh:mm:ss')#<br /><br />
Rating: <cfif #getFeedback.service_satisfaction# eq 'thumbs-up'><img src="images/thumbs-up-small.png" /><cfelse><img src="images/thumbs-down-small.png" /></cfif><br />
Customer Notes: <cfif #getFeedback.customer_notes# eq ''>No additional comments provided.<cfelse>#getFeedback.customer_notes#</cfif><br /><br />
<hr style="border-top: 1px dashed ##8c8c8c;" />
</cfoutput>
<br />
</cfoutput>
Here is a sample of my CFDUMP from the above query:
company_name service_satisfaction response_date ticket_id customer_notes
1 AmerTech thumbs-up {ts '2014-10-22 10:25:14'} 22667 Jeff was great. thanks
2 AmerTech thumbs-up {ts '2015-01-20 12:02:34'} 23795 Rich was good. Thanks. He needs to send out a another drive that we would like as backup to take home at night. Also, he missed one machine for backups that I need to discuss. Have someone please call . Thanks
3 AmerTech, Inc thumbs-up {ts '2015-04-16 13:56:44'} 25066
4 AmerTech, Inc thumbs-down {ts '2015-10-22 11:23:40'} 27293 Brian, I understand from Dave that you could not solve the problem and that he had to call the OEM to solve the problem. This is what I was informed. I do not know any of the details surrounding the issue. but it shouldn't take that long to install a printer on a new laptop. Why did this occur and how do I make sure it doesn't happen again. thanks mark
5 AMIB thumbs-down {ts '2014-10-02 12:18:27'} 22463 Representative did not call me upon arrival at group home as instructed and implemented changes without approval from HR
6 AMIB thumbs-up {ts '2015-06-08 09:58:03'} 25599
7 AMIB thumbs-up {ts '2016-03-10 14:10:01'} 28777
8 AMIB thumbs-up {ts '2016-03-28 09:10:37'} 29193 Michael is a great tech! Extremely helpful and responsive to our needs!
9 AMIB thumbs-up {ts '2016-03-28 10:19:19'} 28777
Update:
When I add the group attribute to the cfoutput tag, it only shows the first result from the group
<cfloop query="getFeedback">
<cfoutput><cfoutput query="getFeedback" group="company_name">
Company Name: #getFeedback.company_name# <cfif #getFeedback.service_satisfaction# eq 'thumbs-up'><img src="images/thumbs-up-small.png" /><cfelse><img src="images/thumbs-down-small.png" /></cfif><br />
Ticket Number: <cfif #getFeedback.ticket_id# eq 0>No ticket associated with this feedback. This was solicited feedback.<cfelse>#getFeedback.ticket_id#</cfif><br />
Date: #DateFormat(getFeedback.response_date, 'mm/dd/yyyy')# at #TimeFormat(getFeedback.response_date, 'hh:mm:ss')#<br /><br />
Customer Notes: #getFeedback.customer_notes# <br /><br />
<br />
<hr></cfoutput>
</cfloop>
Upvotes: 0
Views: 185
Reputation: 2019
Get rid of the loop tag. Then use this:
<cfoutput query="getFeedback" group="company_name">
<!--- OUTPUT EACH GROUP --->
Company Name: #getFeedback.company_name# <cfif #getFeedback.service_satisfaction# eq 'thumbs-up'><img src="images/thumbs-up-small.png" /><cfelse><img src="images/thumbs-down-small.png" /></cfif><br />
<cfoutput>
<!--- OUTPUT EACH RECORD --->
Ticket Number: <cfif #getFeedback.ticket_id# eq 0>No ticket associated with this feedback. This was solicited feedback.<cfelse>#getFeedback.ticket_id#</cfif><br />
Date: #DateFormat(getFeedback.response_date, 'mm/dd/yyyy')# at #TimeFormat(getFeedback.response_date, 'hh:mm:ss')#<br /><br />
Customer Notes: #getFeedback.customer_notes# <br /><br />
<br />
</cfoutput>
<hr>
</cfoutput>
It's the second/nested CFOUTPUT tag that goes through each record. Note the HR is within the GROUP, not each record.
If you want to get fancy, you can make this an accordion with jQuery. Each GROUP is the head, and each record is the contents.
Upvotes: 1
Reputation: 1257
Here is my final verson of the working code. Thanks @Leigh
<!--- Feedback Query --->
<cfquery name="getFeedback" datasource="#datasource#">
select ticket_id, service_satisfaction, customer_notes, response_date, company_name
from service_verification
order by company_name
</cfquery>
<br />
<cfoutput query="getFeedback" group="company_name">
<strong>Company Name: #getFeedback.company_name#</strong><br />
<cfquery dbtype="query" name="parsed">
select company_name
from getFeedback
where company_name = '#getFeedback.company_name#'
</cfquery>
(#parsed.recordcount# Responses)<br />
<cfoutput>
Ticket Number: <cfif getFeedback.ticket_id eq 0>No ticket associated with this feedback. This was solicited feedback.<cfelse>#getFeedback.ticket_id#</cfif><br />
Date: #DateFormat(getFeedback.response_date, 'mm/dd/yyyy')# at #TimeFormat(getFeedback.response_date, 'hh:mm:ss')#<br /><br />
Rating: <cfif getFeedback.service_satisfaction eq 'thumbs-up'><img src="images/thumbs-up-small.png" /><cfelse><img src="images/thumbs-down-small.png" /></cfif><br />
Customer Notes: <cfif getFeedback.customer_notes eq ''>No additional comments provided.<cfelse>#getFeedback.customer_notes#</cfif><br /><br />
<hr style="border-top: 1px dashed ##8c8c8c;" />
</cfoutput>
<br />
</cfoutput>
Upvotes: 0