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

    Hey Guys,

    I think I miss stated my problem. I am using a structure.

    This is my code to add one item at a time, which is failing when the product name has a comma or quotation marks. I don't get an error, but the comma or quote adds a new section to the structure.

    <cfscript>
                if (not(isdefined("session.cart"))) {                // Check to make sure that the Shopping cart structure exists.
                    session.cart = structnew();
                    }

                // The item structure we are going to use to store the items in the cart
                // is going to have four parts...
                //         1.  The item id
                //         2.  The item name
                //         3.  The price per unit
                //         4.  The quantity
                    //         5.  The category

    tempvalue = listtoarray('#attributes.id#,#attributes.name#,#attributes.price#,#attributes.quantity#,#attributes.category#');

                // if the item is not yet in the cart, simply add it to the cart
                if (not(structKeyExists(session.cart, attributes.id))) {
                    StructInsert(session.cart,attributes.id,tempvalue);
                }

                // if the item is already in the cart, update the item quantity
                else {
                    tempvalue[4]=session.cart[attributes.id][4]+attributes.quantity;
                    StructUpdate(session.cart,attributes.id,tempvalue);
                    tempvalue[3]=attributes.price;
                    StructUpdate(session.cart,attributes.id,tempvalue);

                }
            </cfscript>


    As I said before do not use commas as your list delimiters.

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

    Or otherwise be more explicit about your array creation.

    Or you could try implicit array declaration, this is a new CF8 feature.

    tempvalue = [#attributes.id#,#attributes.name#,#attributes.price#,#attributes.q uantity#,#attributes.category#];