How can I use dynamically generated variables from cfloop individually?

281 views Asked by At

I'm not sure my title explains this very well. Frankly, I'm not sure how to put it into words, but here goes:

I am capturing variables from dynamically created form fields on another page, that have been submitted to a page containing the code below. So far, everything works as I want it to. I'm getting the dynamically named form fields. Now I need to add the subtotal dollar amounts together to create a grand total. Unfortunately, I can't figure out how to get the individual totals out of the loop so I can do the math for the final number, due to their dynamic nature.

This part of the code below generates the subtotals, and I need to be able to add all of them together to get a grand total, but I can't figure out how:

#dollarformat(val(getticket[item].ticketprice * form[item]))#

Maybe I need to take a different approach...suggestions/help appreciated.

Here's the full code:

<CFLOOP LIST="#form.fieldnames#" INDEX="item">
<cfoutput>

    <cfquery datasource="outertixdb" name="getticket[item]">
        select * from tickets
        where ticketid = '#item#'
    </cfquery>

    #getticket[item].ticketname#: #dollarformat(getticket[item].ticketprice)# x #form[item]# = #dollarformat(val(getticket[item].ticketprice * form[item]))#<br/>

</cfoutput>

3

There are 3 answers

1
Leigh On BEST ANSWER

Not sure I completely understand, but it sounds like you are simply trying to look up a bunch of ticket records, by "id". Then display the individual costs, plus a grand total. If that is all your doing, just give the fields the same name: ticketID. For example:

  <input name="ticketID" value="1" ...>
  <input name="ticketID" value="22" ...>
  <input name="ticketID" value="45" ...>
  <input name="ticketID" value="16" ...>

Then the values will be submitted as a list ie 1,22,45,16, which you can feed into your query using an IN clause. That lets you grab all of the data in a single query. (Generally you want to avoid running queries within a loop because performing a separate database query for each id generates a lot of unnecessary overhead and degrades performance).

* Change the cfsqltype as needed

SELECT TicketID, TicketPrice
FROM   YourTable
WHERE  TicketID IN ( <cfquerparam value="#FORM.ticketID#" 
                             list="true" 
                             cfsqltype="cf_sql_integer"> 
                  )

UPDATE:

form[item] is the value of the quantity select from the previous page.

That is a confusing naming convention. I would recommend using a slightly more intuitive name like "quantity". Something more descriptive of the contents. You can still use the ticket id to generate unique names, ie quantity_#ticketID#. For example, using the same ticket id's as above:

  <input name="quantity_1" ...>
  <input name="quantity_22" ...>
  <input name="quantity_45" ...>
  <input name="quantity_16" ...>

Once you have the results, there are several ways to generate a grand total. The simplest being to initialize a variable before your loop, then increment it as you iterate. Use the query ticket ID to grab the quantity value from the FORM scope:

 <cfset grandTotal = 0>
 <cfoutput query="yourQuery">
     <!--- extract quantity value --->
     <cfset currQuantity = FORM["quantity_"& yourQuery.ticketID ]>
     ... display price ...
     <cfset grandTotal += val(yourQuery.ticketPrice * currQuantity )> 
 </cfoutput>

 GrandTotal <cfoutput>#grandTotal#</cfoutput>
0
James A Mohler On

I would recommend throwing out the whole dynamically named query thing.

<cfset total = 0>

<CFLOOP LIST="#form.fieldnames#" INDEX="item">

    <cfquery datasource="outertixdb" name="getticket">
       select * from tickets
       where ticketid = <cfqueryparam cfsqltype="cf_sql_varchar" value="#item#">
    </cfquery>

 <cfset total += getticket.ticketprice />    


 <cfoutput query="getTicket">   
    #ticketname#: #dollarformat(ticketprice)# &times; #form[item]# 
    = #dollarformat(val(ticketprice * form[item]))#<br/>
</cfoutput>
</cfloop>

Output your total as needed

8
Scott Stroz On

You would need to set the name attribute of your cfquery using the following format:

<cfquery datasource="outertixdb" name="#getticket[item]#">

To handle the total, you would first need a variable before the cfloop

<cfset total = 0 />

Then, inside the loop, you simply add the price of the ticket to the total

<cfset total = total + getticket[item].ticketprice />

Also, you should be using cfqueryparam in your query. You can read more about it here

Lastly, if you do not need all the data in the tickets table, do not use 'select *..', only pull pack the data that you need.