|
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
Loop and Variable Names
I always seem to get this wrong.Les Mizzell 01/08/09 11:13 A That'll only work if guestdetail.guest is an array (i.e.John M Bliss 01/08/09 11:23 A #guestdetail["guest" & idx]# should work.morgan l 01/08/09 11:25 A 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:Jason Fisher 01/08/09 11:26 A Les Mizzell wrote:Ian Skinner 01/08/09 11:29 A > So now we can point out that anytime one sees something like "queryLes Mizzell 01/08/09 11:43 A You are trying to treat "guest" as a struct with a key called 1 throughbrad 01/08/09 11:21 A Right. Brad's syntax is better. :-)John M Bliss 01/08/09 11:24 A > <cfloop from="1" to="10" index="idx">Les Mizzell 01/08/09 11:49 A As I noted above, you have to have the rowCount element [1], too:Jason Fisher 01/08/09 12:06 P You might have gotten that message, but not when using the code below.brad 01/08/09 12:13 P > <cfloop from="1" to="4" index="idx">Les Mizzell 01/08/09 12:54 P 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! 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 ----- #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 ----- 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. 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. > 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. 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! 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 ----- > <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 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 ----- 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 > <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
|
Mailing Lists
|
Latest Fusion Authority Articles
|
||||||