I have the following code:
<cfif SideSell neq "">
<cftry>
<cfif listlen(SideSell, ",") gt 0>
<cfset sidesellvalid = true>
<cfelse>
<cfset sidesellvalid = false>
</cfif>
<cfcatch type="any">
<cfset sidesellvalid = false>
</cfcatch>
</cftry>
<cfif sidesellvalid>
<cfset AddPartNumber = "">
<cfset AddDescription = "">
<cfset AddQuantity = "">
<cfset AddPrice = "">
<cfset OptionPrice = "">
<cfset AddItem = "">
<cfloop list="FORM.SideSell" index="SideSellListIndex" delimiters=",">
<cfquery name="qSideSellParts" datasource="Pascal">
SELECT * from Part WHERE PartNumber = <cfqueryparam cfsqltype="cf_sql_varchar" list="yes" separator="," maxlength="45" value="#ListGetAt(SideSell, SideSellListIndex, ',')#">
</cfquery>
<cfset AddPartNumber = "#qSideSellParts.PartNumber#">
<cfset AddDescription = "#qSideSellParts.SubCategory#">
<cfset AddQuantity = "1">
<cfset AddPrice = "#qSideSellParts.PartPrice1#">
<cfset OptionPrice = "0">
<cfset AddItem = "Add To Cart">
<cfinclude template="checkpart.cfm">
</cfloop>
</cfif>
</cfif>
However, CF is throwing a typecasting exception, saying
“The value FORM.SideSell cannot be converted to a number.”
at the SQL line:
SELECT * from Part WHERE PartNumber = <cfqueryparam cfsqltype="cf_sql_varchar" list="yes" separator="," maxlength="45" value="#ListGetAt(SideSell, SideSellListIndex, ',')#">
The test data I have tried includes “PTI 19-1” and “PTI 19-1,PTI 19-2” so the problem exists regardless of the list item count. Am I simply missing or misusing a quote mark?
EDIT: Running CF9 on my testing server, CF8 on production server. I used a <cfdump> tag to verify the data is what I expected, and it is.
You’re misunderstanding how cfloop works with the list attribute – you don’t need ListGetAt since the value is contained in the index.
(Technically this isn’t accurate – it should be
itembecause that’s what you’re getting, but whoever originally implemented cfloop did it this way and we’ve been stuck with it.)Also note that list must be a string – not a variable name – so you need hashes to evaluate the variable to one.
Finally since you’re looping through your list one item at a time, you should not use the list attribute of cfqueryparam – (this is for when you are doing
WHERE PartNumber IN (x,y,z)style queries and want the parameter treated as multiple values.)In summary, your loop should look like this.
(If you don’t understand why, add a comment and I’ll try to explain further.)