> -----Original Message----- > From: Daniel Kessler [mailto:[EMAIL PROTECTED] > Sent: Wednesday, June 22, 2005 9:47 AM > To: CF-Talk > Subject: testing for undefined > > I'm returning data back from a database and want to populate > variables the same way as columns are named. I have this working > fine (though any suggested improvements are welcome), but when the > values are undefined, in this case at least, I'd like to make the > variable have a '' (empty quotes) instead for repopulating form > fields. I'm not sure how to test for undefined. > > > Here's the code: > > <cfset the_db = StructNew()> > <cfoutput query="#the_db_name#"> > <cfloop list="#evaluate('#the_db_name#.columnlist')#" index="col"> > <cfset the_db_info[col] = > evaluate('#the_db_name#[col][currentRow]')> > <!--- attempting to make undefined an empty string ---> > <cfif the_db_info[col] EQ undefined> > <cfset the_db_info[col] = ''> > </cfif> > #col#:#the_db_info[col]#<br> > </cfloop> > </cfoutput>
Well - first off you don't need the evaluates in there - you can use normal notation. Something like this. <cfset the_db = StructNew()> <cfoutput query="#CurQuery#"> <cfloop list="#the_db_name.columnlist#" index="col"> <cfset the_db_info[col] = the_db_name[col][currentRow]> </cfloop> </cfoutput> Once that's set I'm confused... you're looping over a known column list so how could any be column name be "undefined"? I guess I'm confused with "the_db_info" variable and what it does... as it is in your current code it's being used as an indexed object, but it's never declared as one... even if it were it would end up overwritten with the last of row of data in any multi-row recordset. To convert a one-row recordset to a struct (which is sorta what your code is doing) I would do this (this will also work for multi-row record sets but will only convert the first row): <cfset MyStruct = StructNew() /> <cfloop list="#CurQuery.columnlist#" index="CurCol"> <cfset MyStruct[CurCol] = CurQuery[CurCol][1] /> </cfloop> By the same token to convert just the last row you modify it to do this: <cfset MyStruct = StructNew() /> <cfloop list="#CurQuery.columnlist#" index="CurCol"> <cfset MyStruct[CurCol] = CurQuery[CurCol][CurQuery.RecordCount] /> </cfloop> Now, to convert multi-record queries you need to have something in the structkey to hold multiple values. (This is where you're original code confuses me since it doesn't seem to do that.) For example a logical form is a struct of Arrays (which is, in fact, what a query object is). To do that conversion you'd do something like this: <cfset MyStruct = StructNew() /> <cfloop list="#CurQuery.columnlist#" index="CurCol"> <cfset MyStruct[CurCol] = ArrayNew(1) /> <cfloop from="1" to="#CurQuery.RecordCount#" index="Cnt"> <cfset ArrayAppend(MyStruct[CurCol], CurQuery[CurCol][Cnt]) /> </cfloop> </cfloop> Now there's a nifty simplification that comes into play here. Since Query columns are already arrays (and can be accessed as such you should also be able to do (more simply) something like this: <cfset MyStruct = StructNew() /> <cfloop list="#CurQuery.columnlist#" index="CurCol"> <cfset MyStruct[CurCol] = ArrayNew(1) /> <cfset MyStruct[CurCol] = CurQuery[CurCol] /> </cfloop> Those two blocks of code (barring any "coding in email email" errors should work identically). It's also useful in other ways. Let's say you don't want a struct of arrays, let's say you want a struct of lists (with each list representing the column of data values). You can do that even more simply like this: <cfset MyStruct = StructNew() /> <cfloop list="#CurQuery.columnlist#" index="CurCol"> <cfset MyStruct[CurCol] = ArrayToList(CurQuery[CurCol]) /> </cfloop> Neat, uh? I'm not sure if any of that actually addresses your problem... but I hope it does! Jim Davis ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Logware (www.logware.us): a new and convenient web-based time tracking application. Start tracking and documenting hours spent on a project or with a client with Logware today. Try it for free with a 15 day trial account. http://www.houseoffusion.com/banners/view.cfm?bannerid=67 Message: http://www.houseoffusion.com/lists.cfm/link=i:4:210215 Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4 Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4 Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4 Donations & Support: http://www.houseoffusion.com/tiny.cfm/54