>> GreenByte.info By Nick Tong (quiksilv) | Supported by: TalkWebSolutions.co.uk

How to create a list of database field types from a query

For a page I'm writing i need to be able to select what the field types are from the query we're running on the database. We allow dynamic table creation so getting this data helps us determine what output form fields we need to display when users are editing the data. Here is the code to get that data

<!--- get the query metadata and set a blank string --->
<cfset qMetaData = getmetadata(qEvents)>
<cfset queryFieldTypes = '' />
<!--- lets loop over the metaData and select the values from the query --->
<cfloop from="1" to="#arrayLen(qMetaData)#" index="loopCount">
   <cfset queryFieldTypes = qMetaData[ loopCount ].typeName & ',' & queryFieldTypes />
</cfloop>
<!--- show the list --->
<cfdump var="#queryFieldTypes#" label="queryFieldTypes" />

So what's going on?

<cfset qMetaData = getmetadata(qEvents)>
qEvents is the name of our query - we use the getMetaData command to get the values from the query. This returns an array full of the metadata (field names, field types)

<cfloop from="1" to="#arrayLen(qMetaData)#" index="loopCount">
This simply loops over the array

<cfset queryFieldTypes = qMetaData[ loopCount ].typeName & ',' & queryFieldTypes />
Now we have the array of data lets create a list and add the 'typeName' (returned in qMetaData) to it.

And that's all there is to it :)


 
Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.5.1.