Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Summing Dynamic Variables

Status
Not open for further replies.

mptwoadmin

Programmer
May 15, 2006
46
US
Hi could someone help me understand why the follow code is not working?

<cfset Pcount1 = ''>
<cfset Pcount2 = ''>

<!-- Start Loop -->
<cfloop index="Add" from="1" to="2">
<cfset "Pcount_#add#" = "Pcount_#add#" + #add#>
<cfoutput>#evaluate("Pcount_#add#")#</cfoutput><br>
</cfloop>
 
Sorry, not getting I guess what your trying to explain?
Could you be more specific or supply an example.

Thank you
 
Below is the test I am running..I removed the "underscores"...But I still receive
the error "The value "dtime01" cannot be converted to a number".. The problem is probably so right in front of me but I cannot see it!

<cfset dtime01 = ''>
<cfset dtime02 = ''>
<cfset dtime03 = ''>

<cfquery name="hours" datasource="qry">
select id, machine, shift, date, start_date, end_date, start_time_hour,start_time_minute,end_time_hour,end_time_minute,downtime, meeting_number
from times
where date = 10-02-25 00:00:00.000'
and shift = '1'
ORDER BY start_time_hour desc
</cfquery>

<cfloop query = "hours">

<cfset start = "#dateformat(hours.start_date,"mm/dd/yyyy")# #hours.start_time_hour#:#hours.start_time_minute#">
<cfset end = "#dateformat(hours.end_date,"mm/dd/yyyy")# #hours.end_time_hour#:#hours.end_time_minute#">

<cfif meeting_number eq 1 and #start# lte '#dateformat(now(),"mm/dd/yyyy")# #timeformat(now(),"HH:mm")#' and #end# lte '#dateformat(now(),"mm/dd/yyyy")# #timeformat(now(),"HH:mm")#'>
<cfset "dtime#right(hours.machine,2)#" = datediff('n',start,end)>
</cfif>
<br>
<cfif meeting_number eq 2 and #start# lte '#dateformat(now(),"mm/dd/yyyy")# #timeformat(now(),"HH:mm")#' and #end# lte '#dateformat(now(),"mm/dd/yyyy")# #timeformat(now(),"HH:mm")#'>
<cfset "dtime#right(hours.machine,2)#" = "dtime#right(hours.machine,2)#" + datediff('n',start,end)>
</cfif>
<br>
<cfif meeting_number eq 3 and #start# lte '#dateformat(now(),"mm/dd/yyyy")# #timeformat(now(),"HH:mm")#' and #end# lte '#dateformat(now(),"mm/dd/yyyy")# #timeformat(now(),"HH:mm")#'>
<cfset "dtime#right(hours.machine,2)#" = "dtime#right(hours.machine,2)#" + datediff('n',start,end)>
</cfif>

</cfloop>
<br>
<cfoutput>downtime = #evaluate("dtime#right(hours.machine,2)#")#</cfoutput><br>
 
I think I got it...Used "Evaluate" in the second part of the equation.

<cfset "dtime#right(hours.machine,2)#" = "#evaluate("dtime#right(hours.machine,2)#")#" + downtime>

Thanks for the assist...It's good to talk things through with someone!

 
There is rarely a reason to need evaluate(). When you declare a variable without a scope, it is placed in the VARIABLES scope. Most CF scopes are structures. You can access the keys dynamically with associative array notation ie structName["keyName"]. Instead of evaluate(), just use the VARIABLES scope:

Code:
<cfset variables["Pcount"& add] = variables["Pcount""& add] + add>

<cfset Pcount1 = ''>

You can only perform arithmetic on valid numbers. So the variables should be initialized with a number ie 0, not an empty string "". Otherwise an error will occur as soon as you attempt the addition operation.

#start# lte '#dateformat(now(),"mm/dd/yyyy")# #timeformat(now(),"HH:mm")#'

DateFormat/TimeFormat are designed for presentation only. They return strings. Since you need to perform date calculations, leave the query values as date/time objects and use date functions (such as dateCompare)



----------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top