PDA

View Full Version : Anyone here familiar with Coldfusion?


Chairman_Kaga
03-02-2004, 10:12 PM
Got a little coding problem.

I'm selecting data (test data is just 10 records for now) from a DB, via the "westcoast" query. (will be obvious in the code)

I WANT to output that data into a "dynamically" sized table that is five cells wide and ### number of rows. The Data is a single column from the select statement. (a link to an image)

I get the table format correct but it doesn't seem that I'm incrementing the pointer within the query results until I get back to the <cfoutput> statement again. So what I end up with is 10 rows of pictures and each row is 5 copies of the same picture. Each new row is the next picture from the query.


<html>
<head>
<title>test 2</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<cfoutput>
<cfquery name="westcoast" datasource="stavo-recipes">
SELECT *
FROM images
</cfquery>
</cfoutput>
<body>
<!--- output the record count for test purposes --->
<cfoutput>#westcoast.recordcount#</cfoutput>
<!--- query --->
<cfoutput query="westcoast">
<table>
<cfset CountVar = 0>
<!--- Loop until CountVar = 5 --->
<cfloop condition = "CountVar LESS THAN OR EQUAL TO 5">
<cfset CountVar = CountVar + 1>
<td><a href="#westcoast.url#"><img src="#westcoast.url#" height="100" width="100"></a></td>
</cfloop>
</table>
</cfoutput>
</body>
</html>


Here is a link to the results (http://goneboating.net/west_coast/test2.cfm)

This is just the latest evolution of the script. I've moved the <table></table> tags around. This one I tried to create ### individual tables 5 columns wide.

Any help would be appreciated. Just getting started with CF and I can't seem to find my answer or an example of what I'd like to do anywhere out there.

RuperT
03-03-2004, 02:37 AM
Well, I'm a C programmer, and I don't know ColdFusion at all, but I'll take a stab at it:
It's pretty obvious your cfloop is executing each time your resultset pointer is incremented, instead of concurrently.
What I mean: CF runs the query 'westcoast'. CF is 'pointing' to the first record of the resultset. Now CF runs your loop. The problem is, each time it's executing the body of the loop, it's 'pointing' to the same record in the resultset (so westcoast.url is the same 5 times). Now, I'm guessing that CF implicitly steps through the resultset of that query as part of the 'cfoutput' mechanism; in other words, when it hits the end of that 'cfoutput' block, it advances the resultset 'pointer' and executes the whole block again if theres another record. If so, the obvious solution is to find whether CF has an explicit Advancetonextrecord function that you could put in the body of the loop to 'manually' advance the resultset 'pointer'. This is probably not the most elegant of solutions, though.
You could remove the cfloop, altogether. Instead, increment CountVar by 1, and use whatever CF's 'if' conditional statement is to add a table break whenever CountVar is evenly divisible by 5. I'm not crazy about this solution either, though.
It seems like it'd be best to let CF just spool out your results, and use plain HTML to define the graphical presentation (I'm not sure why 5 is your magic number; hardcoding such a thing is generally a bad idea in web presentation). In other words, use HTML to limit the columns in your table(s) to 5. Now, I've been fluent in HTML in the past, but it's since been put in the Recycle Bin and purged, so one of our resident and current gurus might help you with that.

Crazy Hobbit
03-03-2004, 02:48 AM
Well, I'm a C programmer, and I don't know ColdFusion at all, but I'll take a stab at it:
It's pretty obvious your cfloop is executing each time your resultset pointer is incremented, instead of concurrently.
What I mean: CF runs the query 'westcoast'. CF is 'pointing' to the first record of the resultset. Now CF runs your loop. The problem is, each time it's executing the body of the loop, it's 'pointing' to the same record in the resultset (so westcoast.url is the same 5 times). Now, I'm guessing that CF implicitly steps through the resultset of that query as part of the 'cfoutput' mechanism; in other words, when it hits the end of that 'cfoutput' block, it advances the resultset 'pointer' and executes the whole block again if theres another record. If so, the obvious solution is to find whether CF has an explicit Advancetonextrecord function that you could put in the body of the loop to 'manually' advance the resultset 'pointer'. This is probably not the most elegant of solutions, though.
You could remove the cfloop, altogether. Instead, increment CountVar by 1, and use whatever CF's 'if' conditional statement is to add a table break whenever CountVar is evenly divisible by 5. I'm not crazy about this solution either, though.
It seems like it'd be best to let CF just spool out your results, and use plain HTML to define the graphical presentation (I'm not sure why 5 is your magic number; hardcoding such a thing is generally a bad idea in web presentation). In other words, use HTML to limit the columns in your table(s) to 5. Now, I've been fluent in HTML in the past, but it's since been put in the Recycle Bin and purged, so one of our resident and current gurus might help you with that. I'm saying limit the number of columns in the table to 5 (again, this seems unnecessarily rigid to me, but I'm sure you have your reasons).

Took the words right out of my mouth :roll: :wink:

AnalogKid
03-03-2004, 08:25 AM
A couple of things. I don't think you need the <cfquery> within <cfoutput> tags, since you refer to the query in the <cfoutput> tag when building your table. Also, I was trying to think of a way to find out when you have hit your 5th cell to end the row, and start a new one. So I figure it would be a formula like: recordcount/(5*(numberofhtmltablerows+1)) = 1.

The only issue with doing it this way is, you have an extra <tr></tr> at the end of the table.

I used to dabble in CF, but I use JSP quite a lot, so my syntax may be a bit off, but I think this may work...



<html>
<head>
<title>test 2</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<cfquery name="westcoast" datasource="stavo-recipes">
SELECT *
FROM images
</cfquery>
<body>
<!--- output the record count for test purposes --->
<cfoutput>#westcoast.recordcount#</cfoutput>
<!--- query --->
<table>
<tr>
<cfset CountVar = 1>
<cfoutput query="westcoast">
<td><a href="#westcoast.url#"><img src="#westcoast.url#" height="100" width="100"></a></td>
<cfif #westcoast.recordcount#/(5*#CountVar#) is 1>
<cfset CountVar = CountVar + 1>
</tr><tr>
</cfif>
</cfoutput>
</tr>
</table>
</body>
</html>

RuperT
03-03-2004, 01:05 PM
A couple of things. I don't think you need the <cfquery> within <cfoutput> tags, since you refer to the query in the <cfoutput> tag when building your table. Also, I was trying to think of a way to find out when you have hit your 5th cell to end the row, and start a new one. So I figure it would be a formula like: recordcount/(5*(numberofhtmltablerows+1)) = 1.

The only issue with doing it this way is, you have an extra <tr></tr> at the end of the table.

I used to dabble in CF, but I use JSP quite a lot, so my syntax may be a bit off, but I think this may work...



<html>
<head>
<title>test 2</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<cfquery name="westcoast" datasource="stavo-recipes">
SELECT *
FROM images
</cfquery>
<body>
<!--- output the record count for test purposes --->
<cfoutput>#westcoast.recordcount#</cfoutput>
<!--- query --->
<table>
<tr>
<cfset CountVar = 1>
<cfoutput query="westcoast">
<td><a href="#westcoast.url#"><img src="#westcoast.url#" height="100" width="100"></a></td>
<cfif #westcoast.recordcount#/(5*#CountVar#) is 1>
<cfset CountVar = CountVar + 1>
</tr><tr>
</cfif>
</cfoutput>
</tr>
</table>
</body>
</html>

<Singing> "Oh, Modulus, at last, I think I've found youuuu!"
<cfif #CountVar# MOD 5 is 0> should fire every 5th time.

Chairman_Kaga
03-03-2004, 01:18 PM
THAT'S what I needed. The MOD.

That fixes it. Thanks guys.

http://goneboating.net/west_coast/test2.cfm

Oh, the numbers are the values of countvar and recordcount. Just wanted to make sure I <tr>'d at the right place. :)

One more qualifier on the CFIF at the end should be able to remove the last <tr></tr> too. (ie AND recordcount NOT EQUAL TO countvar)

FWIW
In the final draft, I plan to have the column width as a visitor defined parameter.