Subsribe to our RSS

latest reactions

christian louboutin shoes
With that kind of traffic you want to ad …
Rahul Anand
Thanks for the nice post. It works for m …
Serkan
thx a lot! …
msb
Thanks for above solution.There is ano …
Lori S.
FYI, I was using this successfully in CF …

Use OpenDNS

mxna feeds

cfmailparam behaviour change in CF9.01ColdFusion Job Opportunity in Houston, TXFacial Recognition in 14 Lines Of ColdFusionColdFusion UPS PackageColdFusion Job Opportunity in Arlington, VAColdFusion Job Opportunity in San Diego, CASome ColdFusionBloggers.org GuidelinesColdFusion Positions In CaliforniaListChangeDelimsunix epoch with coldfusionQuery scripting bug in Coldfusion 9Scoth On The Rock 2011 Tickets on salefoursquareCFC updated to version 1.0, now includes ColdFusion return typeFramework One Birds of a Feather session at CFUnitedWhat makes you a good ColdFusion programmer?

All files are strictly confidential: all information is classified.
© Copyright 2002 - 2010 mximize.com.
All right reserved.

MXNA webfeed

Visit Carlos Gallupa

Function: Query To Array Of Structures

I needed a simple function to convert a query to an array of structures, www.cflib.org provided one but I found that a bit 'out of date', so I wrote a new one to implement in my standard function component.

<cffunction name="querytoarray" returntype="array" output="No">
 <cfargument name="q" required="Yes" type="query">
 <cfset var aTmp = arraynew(1)>
 
 <cfif q.recordcount>
  <cfloop query="q">
   <cfset stTmp = structNew()>
   <cfloop list="#lcase(q.columnlist)#" index="col">
    <cfset stTmp[col] = q[col][currentRow]>
   </cfloop>
 
   <cfset arrayAppend(aTmp,stTmp)>
  </cfloop>
 <cfelse>
  <cfset stTmp = structNew()>
  <cfloop list="#lcase(q.columnlist)#" index="col">
   <cfset stTmp[col] = "">
  </cfloop>

  <cfset arrayAppend(aTmp,stTmp)>
 </cfif>

 <cfreturn aTmp>
</cffunction>


<cfset aRecordset= querytoarray(qRecordset)>
<cfdump var="#aRecordset#">

The reason I need this conversion is because I want to be able to change the 'default' values of a specific query cell... and because you can't change a recordset... wait a minute.. I just tested this on my MX7.01 server and you CAN overwrite a query recordset like this.

<cfset qRecordset.column[1] = "different value">
<cfdump var="qRecordset">

Hmmmm interesting.. do any of you know if this also works on MX6.x??

13849 viewed | 7 opinion(s)  | del.icio.us | Digg it | Tjarko @ 12/08/05 12:01 cet


Reactions:

MdiX wrote....

I use:
<cffunction name="convert" access="remote" returntype="array" hint="converts queryobject into array">
<cfargument name="theQuery" required="true" type="query">
<cfset var theArray = arraynew(1)>
<cfset var cols = ListtoArray(theQuery.columnlist)>
<cfset var row = 1>
<cfset var thisRow = "">
<cfset var col = 1>
<cfscript>
for(row = 1; row LTE theQuery.recordcount; row = row + 1){
thisRow = structnew();
for(col = 1; col LTE arraylen(cols); col = col + 1){
thisRow[cols[col]] = theQuery[cols[col]][row];
}
arrayAppend(theArray,duplicate(thisRow));
}
</cfscript>
<cfreturn theArray>
</cffunction>

04 September 2005 23:29 cet  

Tjarko wrote.... (site)

I discovered that an empty recordset doesn't give me any values so that's why I use the ELSE statement.. to have empty values that I can overwrite with default values if I want.

12 September 2005 9:38 cet  

Edward Beckett wrote.... (site)

I am fairly new to CF and programming however I am trying to get some assistance in querying an array. For instance, I have set the array in the application file as such

<code><CFSET URL.contactKeyword[1] [1]="South Florida Search Engine Optimzation" >
<CFSET URL.contactKeyword[1] [2]="south-florida-search-engine-optimization" ></code>

Next, I haveadded the following to my contact page as an anchor ...

<code><a href="http://www.edwardbeckett.com/Contact/"; name="<cfoutput>#URL.contactKeyword#</cfoutput>">Contact <cfoutput>#URL.contactKeyword#</cfoutput> </a></code>

The question is - How can I Loop through the array that is in the application file to output the second level in the array based on the URL variable that is defined in the first row?

28 October 2007 8:13 cet  

Tjarko wrote.... (site)

Hi Edward, If you are new to CF I would strongly recommend not using 2 dimensional arrays. They are quite hard to read and you are probably better off using a structure for this.

Also.. keep your application file as small as possible because this file will be executed each and every time a page is requested. And further more.. don't ever CFSET URL variables... those are variables that come from the URL scope and only from that scope.. it should not be used to put variables in :-) Use the Variables, Application or Session scope for this.

Back to your problem... I would use something like this in your case

<cfset stSEO = structNew()>
<cfset stSEO["s-e-o"] = "Search Engine Opt.">

If you have a link like this <a href="/contact/?name=s-e-o">link</a>

On the receiving page you can get the value of this by putting the URL variable URL.name straight into your structure as the key of that particular structure

<cfoutput>#stSEO[URL.name]#</cfoutput>

I'm pretty sure what you are trying to do, and I think this is not the way to do it. Did you read my search engine friendly articles??

http://www.mximize.com/how-to-get-search-engine-friendly-url-s

28 October 2007 11:37 cet  

Edward Beckett wrote.... (site)

Tjarko ... Thanks for your advice ... This will really helpme from "bleeding out" my keywords at the contact page ... I will definitely check out your articles for more insight.

This idea was a brainstorm that I adopted to try and support keyword - link path consistency. Currently all my keyword strength is pointed to /Contact/ ...

This is an attempt to create a "keywordname" contact page psuedo dynamically ... How well it will perform with the SERPS? That's another thing entirely... Thanks again.

31 October 2007 9:38 cet  

Chris Brown wrote.... (site)

Very useful function. Thanks for sharing!

14 January 2008 18:06 cet  

Michael wrote....

Great work all of you.

I testet both function with a query of 250000 datasets. The table has 8 columns all int values. Test Server is CFusion 8, Apache 2.2 and mysql5.

MdiX function take around 1500ms the first function takes around 5500ms. I think the CFTAGS are slower than the cfscript.

Thanks for the function.

17 September 2008 21:50 cet  

Leave your comment

Your name


Your url/website/link/email....


Some room for your reaction is placed here..



The answer to the ultimate question is?? (42 ;-))




URL en mail addresses are translated for you... life sometimes is that simple!!