user5916784
user5916784

Reputation:

converting a structre into query

i am trying to convert a small complex piece of coldfusion structure to query to use in my page:

i have the following code which i tried some conversion

<cfset l = "Pame=Program A&Co_Test=0&Programs2Product_ID=1
                &Product_Type_ID=1&Clients2Product_ID=422&Program_ID=1
                &S_Name=MASQW&Product_Template=BGTt&Name=MMMLD
">
<cfset q = queryNew("")>
<cfloop list="Pame,Co_Test,Programs2Product_ID,Product_Type_ID,
  Clients2Product_ID,Program_ID,S_Name,Product_Template,Name" index="k">
    <cfset queryAddColumn(q, "#k#", listToArray(listlast(l,"="),"&"))>
</cfloop>
<cfdump var="#q#">

it converts to the query, but for all columns it just adds the last vakue which is MMMLD

Can anyone check what i am doing wrong here

Upvotes: 0

Views: 93

Answers (1)

beloitdavisja
beloitdavisja

Reputation: 1509

Your problem is you are using listLast() on the variable l, which is the entire string, so it will always give you the last value based on the delimiter (in this case MMMLD). I got this working for me:

<cfset q = queryNew("")>
<cfset l = "Pame=Program A&Co_Test=0&Programs2Product_ID=1
                &Product_Type_ID=1&Clients2Product_ID=422&Program_ID=1
                &S_Name=MASQW&Product_Template=BGTt&Name=MMMLD
">

<cfset keyValueArray = listToArray(l, '&')>
<cfloop array="#keyValueArray#" index="i">
    <cfset keyValuePair = listToArray(i, '=')>
    <cfif arrayLen(keyValuePair) EQ 2>
        <cfset queryAddColumn(q, keyValuePair[1], [keyValuePair[2]])>
    </cfif>
</cfloop>

Upvotes: 6

Related Questions