I’ve gotten an error saying I cannot nest cfoutput tags when the tags use the attribute “query”. I presume this is hinting at me combining the queries these two cfoutput tags are outputting.
The problem is, the MySQL query the first cfoutput tag is outputting the data from is printing out single pieces of information into a table, the “child” cfoutput tag is using a query that gets several pieces of information. So I don’t know how I’d do this.
Here’s the query that the “parent” cfoutput tag is outputting:
SELECT DISTINCT s.id AS id,
s.heading AS heading,
s.reasonForSale AS reason,
s.viewing AS viewing,
s.additionalInfo AS additional,
s.contents AS content,
a.id AS auctioneer_id,
a.name AS auctioneer,
a.website AS website,
d.startDate AS start_date,
d.endDate AS end_date,
d.startTimeHours AS start_hour,
d.startTimeMins AS start_min,
d.startTimeType AS start_time_type,
d.endTimeHours AS end_hour,
d.endTimeMins AS end_min,
d.endTimeType AS end_time_type,
l.address AS address,
l.city AS city,
l.county AS county,
l.postcode AS postcode,
t.entryCopy AS sale_type,
f.filename AS logo_file,
s.featuredAuction AS featured,
s.sale_of_week AS sow,
s.brochure,
s.auctioneerslink
FROM sales s,
auctioneers a LEFT JOIN files f ON f.id = a.logoFile,
saledates d,
saleloc l,
lookupcopy t
WHERE a.id = s.auctioneer AND
d.saleId = s.id AND
t.id = s.saleType AND
l.id = d.saleLoc AND
d.id = #tmp_id#
GROUP BY id
Here is my new query, this will get several pieces of information, a list of things, I need to loop through when printing them. They’re image file names and will need wrapping in image tags, etc…
SELECT file_name
FROM sales_photos
WHERE sale_id = #tmp_id#
ORDER BY id
If I can’t nest these two cfoutput tags, how can I do this?
Thanks.
ColdFusion is correct; you are unable to embed one cfoutput with a query attribute within another cfoutput (with a query attribute).
You can, however, start a query output, and then within it…embed a new query, and loop over those results (which is already within the context of an cfoutput)–so long as you take care to scope the query name and columns (and which row) you are accessing–a la array indexing syntax rules.
For this answer, assume that your 1st query (the giant one) is wrapped in a cfquery named “parent”:
Note the references to the names of the queries in the output.
Note also that I wrapped the 2nd query in function for convenience; you don’t need to do this necessarily–you are free to inject the cfquery within the cfoutput.
Of course, the real answer is to re-write your SQL query so that it joins to the necessary file_name attributes, and then you use a query with a group attribute…but I’ll leave that out of this answer as it is more complex, and requires knowledge of more advanced SQL as well as how the group attribute works.
This answer will work for you, but the ColdFusion community will most likely spank you for looping within a loop–for now, it’ll solve your problem, and you can investigate the group attribute and a more advanced SQL statement later.