Skip to main content
Known Participant
May 29, 2009
Question

listtoarray -adding values with commas throws an error?

  • May 29, 2009
  • 9 replies
  • 1266 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 is closed to new replies. Start a new post to keep the conversation going.

    9 replies

    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>

    Inspiring
    May 29, 2009

    Alternatively, you can use a structure or query object instead of an array.