Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

listtoarray -adding values with commas throws an error?

New Here ,
May 29, 2009 May 29, 2009

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

1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 29, 2009 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)# ??

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 29, 2009 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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 29, 2009 May 29, 2009

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 29, 2009 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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 29, 2009 May 29, 2009

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#];

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 29, 2009 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
May 29, 2009 May 29, 2009

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 29, 2009 May 29, 2009

Found the problem, it was the form on the page that lists the products that can't deal with the quotes:

<input name="addName" value="Part Left -  1/8" longer="" type="hidden">
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 29, 2009 May 29, 2009
LATEST

This fixed it:

    <input type="hidden" name="addName" value="#HTMLEditFormat(productname)#">

I think I'm ok now...

Thanks Ian

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources