House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Search cf-talk

July 03, 2009

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
       1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31   

Home /  Groups /  ColdFusion Talk (CF-Talk)

Loop and Variable Names

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Les Mizzell
01/08/2009 11:13 AM

I always seem to get this wrong. For query results fields guestdetail.guest1 to guestdetail.guest10, why isn't the following working? <cfloop from="1" to="10" index="idx">    Guest #idx#: #guestdetail.guest[idx]#"<br> </cfloop> ..at least I'm not using evaluate!

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
John M Bliss
01/08/2009 11:23 AM

That'll only work if guestdetail.guest is an array (i.e. guestdetail.guest[1], guestdetail.guest[2], etc).  If it's not and you're really trying to get guestdetail.guest1, I think you need #Evaluate("guestdetail.guest" & idx)# On Thu, Jan 8, 2009 at 10:04 AM, Les Mizzell <lesmizz@bellsouth.net> wrote: ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
morgan l
01/08/2009 11:25 AM

#guestdetail["guest" & idx]# should work. On Thu, Jan 8, 2009 at 10:04 AM, Les Mizzell <lesmizz@bellsouth.net> wrote: ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jason Fisher
01/08/2009 11:26 AM

Actually, evaluate() would be perfect in this situation, because you're referring to an array that doesn't exist, you actually need to evaluate a dynamic string.   However, you can also do this: <cfloop from="1" to="10" index="idx">    Guest #idx#: #guestdetail["guest" & idx][1]#"<br> </cfloop> Note that the rowCounter ("[1]") would need to indicate the query row you're pulling from.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Ian Skinner
01/08/2009 11:29 AM

Les Mizzell wrote: > I always seem to get this wrong. > > For query results fields guestdetail.guest1 to guestdetail.guest10 Brad has provided the answer to your original question. So now we can point out that anytime one sees something like "query results fields guest1 to guest10", that is a huge red flag of a database design that should be normalized. The better practice would be to have a 'guest' table with a foreign key to this current table.  Then there would be one record in this related table for each 'guest' associated with the main record.  There are many advantages to such normalization, including that you would no longer need to concatenate your field names to loop over all the guests.

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Les Mizzell
01/08/2009 11:43 AM

> So now we can point out that anytime one sees something like "query > results fields guest1 to guest10", that is a huge red flag of a database > design that should be normalized. Well aware!!!! Not my database! It was already there and they just want some reports out of it. Somebody put *everything* in one table. I would have split it up into at least 3.

Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
brad
01/08/2009 11:21 AM

You are trying to treat "guest" as a struct with a key called 1 through 10.  You want to concatenate the word guest with the number, and THAT composite string is the name of the key in the guestdetail struct: <cfloop from="1" to="10" index="idx"> Guest #idx#: #guestdetail["guest" & idx]#<br> </cfloop> ~Brad I always seem to get this wrong. For query results fields guestdetail.guest1 to guestdetail.guest10, why isn't the following working? <cfloop from="1" to="10" index="idx"> Guest #idx#: #guestdetail.guest[idx]#"<br> </cfloop> ...at least I'm not using evaluate!

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
John M Bliss
01/08/2009 11:24 AM

Right.  Brad's syntax is better.  :-) On Thu, Jan 8, 2009 at 10:13 AM, <brad@bradwood.com> wrote: ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Les Mizzell
01/08/2009 11:49 AM

> <cfloop from="1" to="10" index="idx"> >  Guest #idx#: #guestdetail["guest" & idx]#<br> > </cfloop> I had tried that, and got: "Element GUEST is undefined in GUESTDETAIL" But I can directly output #guestdetail.guest1# through 10 with no problems. Hmmmm

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jason Fisher
01/08/2009 12:06 PM

As I noted above, you have to have the rowCount element [1], too: <cfloop from="1" to="10" index="idx">   Guest #idx#: #guestdetail["guest" & idx][1]#<br> </cfloop> ----- Excess quoted text cut - see Original Post for more -----

Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
brad
01/08/2009 12:13 PM

You might have gotten that message, but not when using the code below. I think I overlooked part of your OP though.  guestdetail is a query object, and you are probably inside of a <cfoutput query=""> OR your query object only has one record.  Is that correct? If guestdetail is a query, the syntax I gave you below will actually return an array of values for that column.  You will want to reference the current record you are accessing as well like so: <cfoutputquery="guestdetail">     <cfloop from="1" to="4" index="idx">     Guest #idx#: #guestdetail["guest" & idx][currentrow]#<br>     </cfloop> </cfoutput> Or... if this result set has only one record, you can just do this: <cfloop from="1" to="4" index="idx"> Guest #idx#: #guestdetail["guest" & idx][1]#<br> </cfloop> ~Brad > <cfloop from="1" to="10" index="idx"> > Guest #idx#: #guestdetail["guest" & idx]#<br> > </cfloop> I had tried that, and got: "Element GUEST is undefined in GUESTDETAIL" But I can directly output #guestdetail.guest1# through 10 with no problems. Hmmmm

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Les Mizzell
01/08/2009 12:54 PM

>     <cfloop from="1" to="4" index="idx"> >     Guest #idx#: #guestdetail["guest" & idx][currentrow]#<br> >     </cfloop> Yup - that did it. Don't know why I always have trouble with this... Thanks


<< Previous Thread Today's Threads Next Thread >>

Mailing Lists