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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Do while loops in CF? 2

Status
Not open for further replies.

SSonnier

Technical User
Nov 4, 2003
78
US
This is what I want to achieve

I have a database with a list of authors. I wantto set up a page that will list the authors alphabeticaly in 2 colums on a page

ie:

A authors B Authors
C authors D Authors

Is there a do while loop in Cold fusion?

I'm thinking I should set this page up like.



<table>
x=1

Do while X < 26
x=x+2
<tr>

<td>
print all records where record_group = x
</td>

<td>
print all records where record_group = x+1
</td>

</tr>

Loop

</table>

 

Code:
<!-- Set the variable CountVar to 0 --> 
<cfset CountVar = 0> 
 
<!-- Loop until CountVar = 5 --> 
<cfloop condition = "CountVar LESS THAN OR EQUAL TO 5"> 

  <cfset CountVar = CountVar + 1> 
  The loop index is <cfoutput>#CountVar#</cfoutput>.<BR> 

</cfloop>
The result of this loop in a browser appears as: 

The loop index is 1. 
The loop index is 2. 
The loop index is 3. 
The loop index is 4. 
The loop index is 5.

<cfset x = 0>
<cfloop condition = x LT 26>
Do Stuff
<cfset x = x +1>
</cfloop>

DeZiner
Never be afraid to try something new.
Remember that amateurs built the Ark.
Professionals built the Titanic
 
Or, you could just loop through your query, then tell CF to start a new table row after each record. Something like this:
Code:
<table>
<tr>
<cfset Count = 0>
 <cfoutput>
  <cfloop query="query_name">
   <td valign="top">#Authors#</td>
   <cfset Count = (Count + 1)>
    <cfif Count EQ 2>
     </tr>
     <tr>
    <cfset Count = 0>
   </cfif>
  </cfloop>
 </cfoutput>
</tr>
</table>



Hope This Helps!

Ecobb

&quot;My work is a game, a very serious game.&quot; - M.C. Escher
 
Good to see you're still lurking in the forum Deziner

Can I suggest a mod to your code Ecobb? For html-friendliness?

Code:
<table>
<tr>
<cfset Count = 0>
 <cfoutput>
  <cfloop query="query_name">
   <td valign="top">#Authors#</td>
   <cfset Count = (Count + 1)>
    <cfif Count EQ 2>
     </tr>
     <tr>
    <cfset Count = 0>
   </cfif>
  </cfloop>
  [b]<cfif count eq 0>
    <td colspan="2">&nbsp;</td></tr>
  <cfelseif count eq 1>
    <td>&nbsp;</td></tr>
  </cfif>[/b]
 </cfoutput>
</table>

Though looking at it now, the cfloop query="" and cfoutput seem a little redundant together, wouldn't it be easier to say..

Code:
<table>
  <tr>
  <cfset Count = 0>
    <cfoutput query="query_name">
      <td valign="top">#Authors#</td>
      <cfset Count = (Count + 1)>
      <cfif Count EQ 2>
        </tr>
        <tr>
        <cfset Count = 0>
      </cfif>
    </cfoutput>
    [b]<cfif count eq 0>
      <td colspan="2">&nbsp;</td></tr>
    <cfelseif count eq 1>
      <td>&nbsp;</td></tr>
    </cfif>[/b]
</table>

But you still get my vote.

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Hey, thanks for cleaning that up webmigit! I didn't even realize I was doing the whole cfoutput/cfloop thing. And thanks for helping me figure out how to close the <tr>/<td> tags properly!





Hope This Helps!

Ecobb

&quot;My work is a game, a very serious game.&quot; - M.C. Escher
 
This started me in a direction, but I'm a little lost.
This is what I have(see below). If there a way to dynamicaly populate a query??

Thanks!!

<cfquery name="list" datasource="alist">
SELECT *
FROM sites
where id=#count#
ORDER BY last_name
</cfquery>

<table height="300">
<cfloop condition = "Count LESS THAN 26">
<tr height="300">
<td width="1" valign="top" bgcolor="gray">
<img src="image/space.gif" width="1" height="1">
</td>
<cfset count = count + 1>
<td width="313">
<cfoutput query="list"><a href="#url#" target="new">#name#</a><br></cfoutput>
</td>

<td width="1" valign="top" bgcolor="gray">
<img src="image/space.gif" width="1" height="1">
</td>
<cfset count = count + 1>
<td width="314">
<cfoutput query="list"><a href="#url#" target="new">#name#</a><br></cfoutput>
</td>
<td width="1" valign="top" bgcolor="gray">
<img src="image/space.gif" width="1" height="1">
</td>
</tr>
</cfloop>
</table>
</td>
 
Code:
<cfquery name="list" datasource="alist">
  SELECT *
  FROM sites
  ORDER BY last_name
</cfquery>

<table>
  <tr>
  <cfset Count = 0>
    <cfoutput query="list">
      <td valign="top"><a href="#url#" target="new">#name#</a></td>
      <cfset Count = (Count + 1)>
      <cfif Count EQ 2>
        </tr>
        <tr>
        <cfset Count = 0>
      </cfif>
    </cfoutput>
    <cfif count eq 0>
      <td colspan="2">&nbsp;</td></tr>
    <cfelseif count eq 1>
      <td>&nbsp;</td></tr>
    </cfif>
</table>

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Hey I hope this thread is still active,

I'm using your technique above, and I have a small problem. I'm displaying pictures in 3 columns by x rows. Under each picture, I want to display the picture ID which is a field in the database (it's just a counter), but what is coming out is 123456789, for the first picture, 23456789 for the second, 3456789 for the third, and so on.

here's my code

<table width="651" border="0" cellspacing="0" cellpadding="0">
<tr>
<cfset count = 0>
<cfoutput query="getPics">
<td>
<a href="lg_img.cfm?imgid=#getPics.imgid#"><img src="pics/thumbnails/#getPics.sm_image#" border="0" /></a>
<p>
<cfoutput>#getPics.imgid#</cfoutput>
</p>
</td>
<cfset count = (count + 1)>
<cfif Count EQ 3>
</tr>
<tr>
<cfset Count = 0>
</cfif>

</cfoutput>
<cfif count eq 0>
<td colspan="2">&nbsp;</td></tr>
<cfelseif count eq 1>
<td></td></tr>
</cfif>

</table>





the part that is supposed to display the picture ID is this

<p>
<cfoutput>#getPics.imgid#</cfoutput>
</p>

What's going on here?

Thanks in advance
 
Code:
<cfoutput>#getPics.imgid#</cfoutput>

should just be

#getPics.imgid#

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
thank you so much. That did it. Only that now the imgId numbers look like a mess because of the difference in height of each picture.

Is there a way that I can divide each cell where the picture is into 2 rows, so that the imgID numbers are lined up in the bottom sub-row? Have you come accross this problem before?

thanks in advance for your quick response.
 
Yes I've come across it.

And no there's not an easy enough way..

Try changing the td in:

<cfoutput query="getPics">
<td>

to

<Td align="bottom">

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
This is a shot in the dark.. you may have to tweak some.. In a hurry sorry..

Code:
<cfset imgIDs=ArrayNew(1)>
<table width="651" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <cfset count = 0>
    <cfoutput query="getPics">
      <td>
        <a href="lg_img.cfm?imgid=#getPics.imgid#"><img src="pics/thumbnails/#getPics.sm_image#" border="0" /></a>
        <cfset imgidx=listappend(count)>
      </td>
      <cfset count = (count + 1)>
      <cfset imgidx=listappend(count)>
      <cfset imgids[count]=getPics.imgID>
      <cfif Count EQ 3>
        </tr>
        <tr><cfset img
          <cfif listfind(imgidx,1)><td>#imgids[1]#</td></cfif>
          <cfif listfind(imgidx,2)><td>#imgids[2]#</td></cfif>
          <cfif listfind(imgidx,3)><td>#imgids[3]#</td></cfif>
        <tr>
        <cfset Count = 0>
      </cfif>
    </cfoutput>
    <cfif count eq 0>
      <td colspan="2">&nbsp;</td></tr>
      <tr>
        <cfif listfind(imgidx,1)><td>#imgids[1]#</td></cfif>
        <cfif listfind(imgidx,2)><td>#imgids[2]#</td></cfif>
        <td>&nbsp:</td>
      </tr>
    <cfelseif count eq 1>
      <tr>
        <cfif listfind(imgidx,1)><td>#imgids[1]#</td></cfif>
        <td>&nbsp:</td>
        <td>&nbsp:</td>
      </tr>
    </cfif>
</table>

ALFII.com
---------------------
If this post answered or helped to answer your question, please reply with such so that forum members with a similar question will know to use this advice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top