Thanks...you gotta love CFLIB.org!!!!!
I can't believe I forgot about that resource.

-----Original Message-----
From: Matthew Friedman [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, December 11, 2002 3:37 PM
To: CF-Talk
Subject: RE: Query to Structure with keys...


If you are looking at moving a query to a structure there is a great UDF on
cflib that I use all the time.

Try this below (this is not my code!!!)

and then put the output into a cfdump to see the outcome.


For full documentation look at www.CFlib.org.

<cfscript>
/**
 * Converts a query object into a structure of structures accessible by its
primary key.
 *
 * @param theQuery       The query you want to convert to a structure of
structures.
 * @param primaryKey     Query column to use as the primary key.
 * @return Returns a structure.
 * @author Shawn Seley ([EMAIL PROTECTED])
 * @version 1, March 27, 2002
 */
function QueryToStructOfStructures(theQuery, primaryKey){
  var theStructure  = structnew();
  // remove primary key from cols listing
  var cols          = ListToArray(ListDeleteAt(theQuery.columnlist,
ListFindNoCase(theQuery.columnlist, primaryKey)));
  var row           = 1;
  var thisRow       = "";
  var col           = 1;

  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];
    }
    theStructure[theQuery[primaryKey][row]] = duplicate(thisRow);
  }
  return(theStructure);
}
</cfscript>

-----Original Message-----
From: Che Vilnonis [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, December 11, 2002 3:20 PM
To: CF-Talk
Subject: RE: Query to Structure with keys...



Code examples would be great!!!

-----Original Message-----
From: Matthew Friedman [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, December 11, 2002 3:24 PM
To: CF-Talk
Subject: RE: Query to Structure with keys...


you could use the valuelist(query.colname) function to do this with out the
loop



-----Original Message-----
From: Ian Skinner [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, December 11, 2002 3:13 PM
To: CF-Talk
Subject: RE: Query to Structure with keys...



First of all, if you want to use the Product ID's you need to return them
with the Select Clause.  Just using a field in the where clause does not
return them.

        <cfquery name="getProductDetails" datasource="XXX">
                SELECT Product_ID, PartNumber, ProductTitle
                FROM   Products P
                WHERE    Product_ID IN

(9909,9910,9911,9912,9913,9914,9371,9372,9378,9380,9855,8081,9631)
        </cfquery>

Then what I've done in the past is just loop over the query and create a
structure.  I'd love to know if there are any more direct ways to do this.

<cfscript>
        productStruct = newStruct();
        for (i=1;i<=getProductDetails.recordcount;i=i+1)
        {
                tempArray = newArray(1);
                tempArray[1] = getProductDetails.PartNumber;
                tempArray[2] = getProductDetails.ProductTitle;
                productStruc[getProductDetails.Product_ID][i] = tempArray;
        }
</cfscript>

Then you should be able to play with your new structure something like this.

<cfoutput>
        <td>PartNumber</td><td>ProductTitle</td>

<td>#productStruct['9911'][1]#</td><td>#productStruct['9911'][2]#</td>
</cfoutput>

Please pardon in problem with my <cfscript> code.  I just wrote that off the
top of my head, and I don't use <cfscript> all that often.

I hope this helps.

Ian Skinner

-----Original Message-----
From: Che Vilnonis [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, December 11, 2002 11:13 AM
To: CF-Talk
Subject: Query to Structure with keys...


Hey its Brainfart Wednesday. Everyday lately seems to be a brainfart.

I have this query...

<cfquery name="getProductDetails" datasource="XXX">
SELECT PartNumber, ProductTitle
FROM   Products P

WHERE    Product_ID IN
(9909,9910,9911,9912,9913,9914,9371,9372,9378,9380,9855,8081,9631)
</cfquery>

I would to place this query into a structure. The keys need to come from the
Product_IDs
in the where clause of the query. Each key has as its value, the column
names that are returned.

Any ideas on how to do this??? <cfscript> based code samples would be
appreciated.

Ché Vilnonis
Application Developer
Advertising Systems Incorporated
8470C Remington Avenue
Pennsauken, NJ 08110
p: 856.488.2211
f: 856.488.1990
www.asitv.com






~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription: 
http://www.houseoffusion.com/cf_lists/index.cfm?method=subscribe&forumid=4
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Signup for the Fusion Authority news alert and keep up with the latest news in 
ColdFusion and related topics. http://www.fusionauthority.com/signup.cfm

Reply via email to