Skip to main content
Inspiring
January 28, 2010
Answered

problems getting cfloop to output in 2 columns.

  • January 28, 2010
  • 2 replies
  • 1456 views

Hello;

I'm having a problem making my code actually output properly. It's not throwing any errors, it actually says there is a record to be read, but my output from my cfloop is wrong I believe. I'm trying to make it break down into 2 columns like this

1 2

3 4

NEXT >

once there is 4 records, the button to go to next page comes up. I know this code is bulky, AI also know my query and next n buttons work fine, so I'm going to post just the query, and the loop with the statements to break it down into 2 columns.

Code:

<cfquery name="newsStory" datasource="#APPLICATION.dataSource#" maxRows=10>
SELECT klNews.title AS ViewField2, klNews.newsDate AS ViewField3, klNews.MYFile, klNews.MYFile2, klNews.ID AS ID
FROM klNews
    ORDER BY newsDate
</cfquery>

<cfset halfrecords = newsStory.RecordCount/2>

<cfloop query="newsStory" startRow="#URL.startRow#" endRow="#endRow#">
<cfoutput>
   <tr>
<cfif CurrentRow lte halfrecords>

<td width="49%" align="center" valign="top">

#All my content is in here for column 1#

</td>
</cfif>
<cfif Evaluate(CurrentRow+halfrecords) lte newsStory.RecordCount>
<td width="2%" align="center" valign="top">  </td>
<td width="49%" align="center" valign="top">

#column 2 stuff is here#

</cfif>
</tr>
</cfoutput></cfloop>

it's not showing anything at the moment,
Thank you.

This topic has been closed for replies.
Correct answer Joshua_Cyr

NOTE: small edit to fix a missing TD and url scope in what I just posted.

Might be missing some issue here, but seems like this sort of thing should work just fine.

<!--- default the start row from query to 1 --->

<cfparam default="1" name"url.startrow" />

<cfset newstartrow = url.startrow + 4 />

<!--- make counter to see if we close the tr --->

<cfset closecounter = 0 />

<cfoutput query="yourquery" startrow="#url.startrow#" maxrows="4">

<!--- check to see if odd or even --->

<cfif yourquery.currentrow MOD 2 eq 1>

<tr>

<td>#data#</td>

<cfset closecounter = 0 />

<cfelse>

<td>#data#</td>

</tr>

<cfset closecounter = 1 />

</cfif>

</cfoutput>

<!--- now we see if that last tr was actually closed in case we had odd number of final results --->

<cfif closecounter eq 0>

<td></td>

</tr>

</cfif>

<!--- next link adding 4 to the start row --->

<a href="blah.cfm?startrow=#newstartrow#">NEXT</a>

2 replies

BKBK
Community Expert
Community Expert
January 28, 2010

It just might be the effect of the startRow and endRow. To test that, replace the line

<cfloop query="newsStory" startRow="#URL.startRow#" endRow="#endRow#">

with the line

<cfloop query="newsStory">

Inspiring
January 28, 2010

<cfoutput query="yourquery">

#data#

<cfif currentrow mod the_number_of_records_per_row is 0>

start a new row.

Inspiring
January 28, 2010

that doesn't work, I tried it already. I have a next n button on this query. If i run the query on it's own, it works, when I add the startrow and maxrow it crashes.

How would I write this so I could hold 4 records a page with a next n working..

Just using cfloop query and no start row and max row in it doesn't work, just tried. it's somethign in the cfloop I think and the columns, it doesn't like each other. Is there a better way?

Joshua_CyrCorrect answer
Participating Frequently
January 28, 2010

NOTE: small edit to fix a missing TD and url scope in what I just posted.

Might be missing some issue here, but seems like this sort of thing should work just fine.

<!--- default the start row from query to 1 --->

<cfparam default="1" name"url.startrow" />

<cfset newstartrow = url.startrow + 4 />

<!--- make counter to see if we close the tr --->

<cfset closecounter = 0 />

<cfoutput query="yourquery" startrow="#url.startrow#" maxrows="4">

<!--- check to see if odd or even --->

<cfif yourquery.currentrow MOD 2 eq 1>

<tr>

<td>#data#</td>

<cfset closecounter = 0 />

<cfelse>

<td>#data#</td>

</tr>

<cfset closecounter = 1 />

</cfif>

</cfoutput>

<!--- now we see if that last tr was actually closed in case we had odd number of final results --->

<cfif closecounter eq 0>

<td></td>

</tr>

</cfif>

<!--- next link adding 4 to the start row --->

<a href="blah.cfm?startrow=#newstartrow#">NEXT</a>