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

How to get a total from forms

Explorer ,
Mar 22, 2009 Mar 22, 2009
Hi, I'd like to be able to total the results of a form.

I have a number of items that were generated from a database, I have a select for the quantity of each item listed. next I want to total the price dynamically based on the item price x quantity, looping through all the items in the list. when I do a dump of the form this is what I get

4 - 3
FIELDNAMES - 1,2,3,4,ORDER1
ORDER1 - order1
1 - 4
2 - 2
3 - 0

FIELDNAMES 1,2,3,4 represent the ITEM ID and the number next to it represents the quantity ordered. (4 - 3 means item ID 4 has a total of 3) so now I'm not sure what to do, how do I run a select based on unknown formfields. or is there another way to get this done?
519
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 ,
Mar 22, 2009 Mar 22, 2009
It would be more efficient if you changed this:
<option value="#tcount#" >#tcount#</option>

to this
<option value="#tcount#,#price#" >#tcount#</option>

Then your submission would look like this:
4 - 3,2.25
FIELDNAMES - 1,2,3,4,ORDER1
ORDER1 - order1
1 - 4,6.00
2 - 2,4.50
3 - 0,1.75

Where 4 - 3,2.25
means item 4, quantity 3, unit price = 2.25.

Then you loop through the form fields. Do something to exclude the one named order1 and do your math inside the loop. Something like this.

total = 0;

<cfloop list = "#form.fieldnames#" index = "thisfield">
<cfif isnumeric(thisfield)>
<cfset total = total + form[listfirst(thisfield)] * form[listlast(thisfield)]>
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
Explorer ,
Mar 23, 2009 Mar 23, 2009
Hi, good idea, however one of the issues with that is that the list is passed as text not as integer and cf will not perform calculations on the text string. do you know how I can get around that?
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
Explorer ,
Mar 23, 2009 Mar 23, 2009
Hi, good idea, however one of the issues with that is that the list is passed as text not as integer and cf will not perform calculations on the text string. do you know how I can get around that?
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 ,
Mar 23, 2009 Mar 23, 2009
What do you mean the list is passed as text? Do you mean
1,2,3
or
one,two,three?

If the former, the list elements are numbers so you are ok. If the latter, you may want to take another look at your database design.
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
Explorer ,
Mar 23, 2009 Mar 23, 2009
Hi, thanks for your help.

I tried to do it another way and that is why I got the error message. The issue now is that the delimiter is a comma and so it's separating the each item based on commas but then again the price and quantity. I've updated the code and i'm including it here. note: being that you are including the price and quantity in the select value, we don't have to include the item ID so I named the select tickets that way it does not have to be filtered in the loop.
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 ,
Mar 23, 2009 Mar 23, 2009
Here:
<cfoutput query="getevent">
#TicketName# #description# #price#
<select name="tickets">
<cfloop index="tcount" from="0" to="#limit#">
<option value="#price#,#tcount#">#tcount#</option>

Either make the id value part of the field name, or, include it in the value of the dropdown. If you do the latter, change the delimiter.
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
Explorer ,
Mar 23, 2009 Mar 23, 2009
LATEST
Hi thanks for your answer but I don't understand it, it seems like you were going to paste more info but only pasted some of the code that I had posted. either way I appreciate your help with this. I'll try to work it out.
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