Skip to main content
Known Participant
May 29, 2009
Question

listtoarray -adding values with commas throws an error?

  • May 29, 2009
  • 1 reply
  • 1246 views

I've got a simple shopping cart that stores into in an array.  If the product name has commas, and it attempted to be added to the cart, it throws an error:

"The value cannot be converted to a number". Error message is telling me this is the problem line:

<cfset subtotal = subtotal + (session.cart[4] * session.cart[3])>

What gives? I've not seen this before? Do the commas in the product name need to be escaped somehow?? And where?

-Preserved

    This topic has been closed for replies.

    1 reply

    PreservedAuthor
    Known Participant
    May 29, 2009

    Ok more.

    It seems the item name is getting added to the array but the commas are creating new positions in the array which is distrupting other pages that display the contents of that array.

    I'm adding the item as #attributes.name#, is there a way to escape the commas in the name as in #realcommas(attributes.name)# ??

    ilssac
    Inspiring
    May 29, 2009

    You can't really escape the commas in the data, but all of ColdFusion's list functions have a delimiter parameter so that you can use something other then the defualt comma charater to seperate list items, like a pipe character "|", a dash character "-", an underscore character "_", etc.

    <cfset aList = "">

    <cfset aList = listAppend("A,String,With,Commas","|")>

    <cfset aList = listAppend("another Value","|")>

    <cfset aList = listAppend("yet another Value","|")>

    <cfoutput>

    #aList# #listLen(aList,"|")#

    </cfoutput>

    ilssac
    Inspiring
    May 29, 2009

    Ian,

    This comes close, and it makes sence to me:

    tempvalue = listtoarray('#attributes.id#|#attributes.name#|#attributes.price#|#attributes.q uantity#|#attributes.category#','|');

    However, what's weird is that if attributes.name is "PART -  1/8" LONGER" everything after the 8 is dropped? Something does not like the double quotes (inche mark)? I don't get an error, the "LONGER is just lost?


    I do not beleive this line is the source of that error.  This test code worked just fine for me.

    <cfset aString = 'I,Am,A,String'>
    <cfset bString = 'Blue'>
    <cfset cString = 'George'>
    <cfset dString = 'PART -  1/8" LONGER'>

    <cfset foobar = listToArray('#aString#|#dString#|#bString#|#cString#','|')>

    <cfdump var="#foobar#">

    I think you will need to look earlier in your logic flow for where that value is getting truncated.  You need to be careful of strings with quotes in them used in declaration of string variables.  The quotes inside the string can be confused with the quotes in the declaration line.