views:

90

answers:

6

Hey guys, I really am stuck here. I keep getting an error on this code and I have absolutely no idea why. Basically what I am trying to do is check a form for a field called quantity_# with # being a number. If it finds it I want to go through all those fields and add them onto a 2 dimension array so I can store the actual value of that field and the number that was on the end. I have tried my code below and it does not work, giving me the error:

When using named parameters to a function, every parameter must have a name.

I have no idea why it is not working, can someone please help? My code is below, thanks.

      <!--- Check if there are multiple quantities --->
  <cfif ListContains(form.fieldnames, "quantity_")>
    <cfset quantities = ArrayNew(2)>
    <cfloop index="i" list="form.fieldnames">
      <cfif i contains "quantity_">
        <cfset product = ArrayNew(1)>
        <cfset product = ArrayAppend(product, form.[i])>
        <cfset product = ArrayAppend(product, ListGetAt(i, 2, "_"))>
        <cfset product = ArrayAppend(quantities)>
      </cfif>
    </cfloop>
    <cfset form.quantity = quantities>
  <cfelse>
    <!--- Make it so if the quantity is blank we set it to 0 so it passes validation --->
    <cfif form.quantity EQ "">
      <cfset form.quantity = 0>
    </cfif>
  </cfif>
+1  A: 

ArrayAppend returns true/false on completion - not the array itself.

Try this instead:

<cfif i contains "quantity_">
    <cfset product = ArrayNew(1)>
    <cfset ArrayAppend(product, form.[i])>
    <cfset ArrayAppend(product, ListGetAt(i, 2, "_"))>
  </cfif>
scrittler
Hi, I still get the error: When using named parameters to a function, every parameter must have a name.The CFML compiler was processing:An expression beginning with ArrayAppend, on line 17, column 24.This message is usually caused by a problem in the expressions structure.A cfset tag beginning on line 17, column 18.A cfset tag beginning on line 17, column 18.A cfset tag beginning on line 17, column 18.A cfset tag beginning on line 17, column 18.A cfset tag beginning on line 17, column 18.
Darren
As @bpanulla mentioned, that is because of the dot in the form field name. ie form.[i] instead of form[i]
Leigh
+4  A: 

The list attribute of CFLOOP also needs to be a list literal, not a variable name. You need pound signs:

<cfloop index="i" list="#form.fieldnames#"> ... </cfloop>

bpanulla
I still get the error.
Darren
What line of code does the error message actually reference?
bpanulla
This line: <cfset product = ArrayAppend(product, form.[i])>
Darren
I think the dot may be the problem. Try: ArrayAppend(product, form[i]). That treats the Form struct as an associative array. If you want the dot there, you'd need to use something like eval("form.#i#")
bpanulla
No need for eval(). "form[i]" is spot on.
Al Everett
+1  A: 

Also, you are calling arrayAppend(quantities), but arrayAppend() takes 2 parameters.

Ben Doom
A: 

Ok.. With the info you gave I have tried to do what I think you are trying to achieve.

<cfset form.fieldnames = "">
<cfloop from="1" to="10" index="j">
 <cfset "form.quantity_#j#" = j*10>
 <cfset form.fieldnames = listAppend(form.fieldnames, "quantity_#j#")>
</cfloop>

<cfset Product = ArrayNew(1)>

<!--- Check if there are multiple quantities --->
<cfif ListContains(form.fieldnames, "quantity_")>
 <cfloop index="i" list="#form.fieldnames#">
  <cfif i contains "quantity_">
   <cfset Product[ListGetAt(i,2,"_")] = evaluate(i)>
  </cfif>
    </cfloop>
</cfif>

<cfdump var="#Product#">

This does take all the info from the form and then put it into an array, but maybe a structure would be more suitable for what you are trying to achieve.

Fred

FreddyF
+1  A: 

This does what I think you want. There is some test data at the start and a dump of the quantities array at the end so that you can run it without a form.

I removed the outside cfif. It is just as expensive to test if the list contains something as it is to loop over it, so just do it once.

Using contains to test for "quantity_" is unreliable, as it looks anywhere in the string, not just the beginning. That could lead to unexpected bugs down the track.

You don't need to initialize a 2d array for quantities, because you are going to push an array on to it.

form.[i] should be form[i]

Plus a few mistakes with arrayAppend.

<!--- test data --->
<cfset form.quantity_1 = 5>
<cfset form.quantity_2 = 1>
<cfset form.quantity_3 = 10>
<cfset form.fieldnames = "quantity_1,quantity_2,quantity_3">

<!--- Check if there are multiple quantities --->
<cfset quantities = ArrayNew(1)>
<cfloop index="i" list="#form.fieldnames#">
  <cfif left( i, 9 ) eq "quantity_">
    <cfset product = ArrayNew(1)>
    <cfset ArrayAppend(product, form[i])>
    <cfset ArrayAppend(product, ListGetAt(i, 2, "_"))>
    <cfset ArrayAppend(quantities, product)>
  </cfif>
</cfloop>

<cfset form.quantity = quantities>

<cfif arrayLen( quantities ) eq 0>
  <!--- Make it so if the quantity is blank we set it to 0 so it passes validation --->
  <cfset form.quantity = 0>
</cfif>

<!--- check results --->
<cfdump var="#quantities#">
Jon Hart
A: 

Hi guys, thanks for all the help and I took your advice and this is what I got to work:

  <!--- Check if there are multiple quantities --->
  <cfif ListContains(form.fieldNames, "QUANTITY_") neq 0>
    <cfset quantities = ArrayNew(1)>
    <cfloop index="i" list="#form.fieldnames#">
      <cfif i contains "QUANTITY_">
        <cfset product = ArrayNew(1)>
        <cfset product_id = ListGetAt(i, 2, "_")>
        <cfset ArrayAppend(product, evaluate("form.PAGETITLE_#product_id#"))>
        <cfset ArrayAppend(product, evaluate("form.#i#"))>
        <cfset ArrayAppend(product, "#product_id#")>
        <cfset ArrayAppend(quantities, product)>
      </cfif>
    </cfloop>
    <cfset form.quantity = quantities>
  <cfelse>
    <!--- Make it so if the quantity is blank we set it to 0 so it passes validation --->
    <cfif form.quantity EQ "">
      <cfset form.quantity = 0>
    </cfif>
  </cfif>

Thanks again and I hope my rusty ColdFusion skills will become shinier.

Darren