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

Display table dynamically

Status
Not open for further replies.

xsw1971

Programmer
Jun 21, 2001
153
US
I want to create a form full of checkboxes. The checkboxes are displayed in a table format: 2 colums for each row as shown below.
Code:
<tr>
  <td><input type=&quot;Checkbox&quot; name=&quot;fruit&quot; value=&quot;banana&quot;> banana</td>
  <td><input type=&quot;Checkbox&quot; name=&quot;fruit&quot; value=&quot;mango&quot;> mango</td>
</tr>
I have upwards of 80 items to be displayed as checkboxes, and their values are always changing. I want them to be displayed alphabetically, but I don't want to hard code them because if one in the middle changes I have to change all of them (their positions).

I have created a CF list where I can add to, remove or change the list items regardless of their alphabetical position, then have it sort the list:
Code:
<cfset mylist = &quot;&quot;>
<cfset mylist = ListAppend(mylist,&quot;banana&quot;)>
<cfset mylist = ListAppend(mylist,&quot;strawberry&quot;)>
<cfset mylist = ListAppend(mylist,&quot;pear&quot;)>
<cfset mylist = ListAppend(mylist,&quot;mango&quot;)>
<cfset mylist = ListSort(mylist,&quot;textnocase&quot;,&quot;asc&quot;)>
What I want to do now is output the list so that it creates a form that looks like this:
Code:
O banana     O mango
O pear       O strawberry
I can't figure out how to dynamically code the <tr> and <td> tags so that it displays this way. What I get is:
Code:
O banana
             O mango
O pear
             O strawberry
Thanks for your help in advance!!
 
Try this:
Code:
<tr>
<cfset Count=0>
<cfloop index=&quot;i&quot; list=&quot;mylist&quot;>
<cfset Count= (Count + 1)>
<tr>
  <td><input type=&quot;Checkbox&quot; name=&quot;fruit&quot; value=&quot;#i#&quot;> #i#</td>
<cfif Count EQ 2>
</tr>
<tr>
<cfset Count = 0>
</cfif>
</cfloop>
</tr>



Hope This Helps!

Ecobb

&quot;Alright Brain, you don't like me, and I don't like you. But lets just do this, and I can get back to killing you with beer.&quot; - Homer Simpson
 
You'll want to change mylist to #mylist# in the cfloop tag..

But more importantly after the
Code:
</cfloop>
you'll want to use this code to keep the html syntax correct.. Replace the last
Code:
</tr>
with.

Code:
<cfif count eq 1><td>&nbsp;</td></tr></cfif>

That way if the last loop generated code is ... <tr><td>pear</td> (presently the third item in your list. This snippet of code will add to it so that the lsat row is...
Code:
<tr><td>pear</td><td> </td></tr>

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 both for your replies! However, it still didn't work, but I figured out what to change. The code you guys supplied was adding a <tr> to every single loop, which caused the following output:
Code:
O banana
O mango
O pear
O strawberry
I had to remove a couple <tr> tags and put a condition on the remaining <tr> to check if the Count was 1. So the entire code looks like this:
Code:
<table>
<cfset Count=0>
<cfloop index=&quot;i&quot; list=&quot;#mylist#&quot;>
  <cfset Count=(Count + 1)>
  <cfif Count EQ 1>
  <tr>
  </cfif>
    <td><input type=&quot;Checkbox&quot; name=&quot;fruit&quot; value=&quot;#i#&quot;> #i#</td>
  <cfif Count EQ 2>
  </tr>
  <cfset Count = 0>
  </cfif>
</cfloop>
<cfif Count eq 1><td> </td></tr></cfif>
</table>
I never would have figured this out on my own, so I thank you guys for the kick start!

Jennie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top