brihol44
asked on
Coldfusion Adding Vars
Hello,
I'm working with the below code for a cart. I seem to be getting '0' for the value. I'm just not sure only because I can <cfoutput>#totalsQuery.tot al#</cfout put> and I see the actual number and not 0.
Thanks for the insight!
I'm working with the below code for a cart. I seem to be getting '0' for the value. I'm just not sure only because I can <cfoutput>#totalsQuery.tot
<cfset totalsQuery = QuerySelectCustomerTotalSpent(customer_id = arguments.customer_id, pastDate=pastDate)>
<cfset totalSpending = val(cartTotal) + val(totalsQuery.total) >
Thanks for the insight!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Excuse me, but
1. do you declare and use your variables (totalSpending and cartTotal) inside a function, then try to print them outside of your function?
Here is what they say:
2. what is the result returned by your function? May I see your code?
1. do you declare and use your variables (totalSpending and cartTotal) inside a function, then try to print them outside of your function?
Here is what they say:
<cfset
var variable_name = expression>
var - Optional - A keyword. Does not take a value. Identifies the variable as being local to a function. The variable only exists for the time of the current invocation of the function.if that is the case, you should declare your variables outside your function's scope.
2. what is the result returned by your function? May I see your code?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So the solution was ...
<cfset var rsCustomerOrderTotals.tota l = 0 >
Initializing the var but adding the query object as well.
<cfset var rsCustomerOrderTotals.tota
Initializing the var but adding the query object as well.
I think the position of your test output is very important, try doing like this where you test the total value before and the total spending just after. That should clarify what's happening. There doesn't appear to be anything wrong with this part of your code.
<cfset totalsQuery = QuerySelectCustomerTotalSp ent(custom er_id = arguments.customer_id, pastDate=pastDate)>
<cfoutput>#totalsQuery.tot al#</cfout put>
<cfset totalSpending = val(cartTotal) + val(totalsQuery.total) >
<cfoutput>#totalSpending#< /cfoutput>
<cfset totalsQuery = QuerySelectCustomerTotalSp
<cfoutput>#totalsQuery.tot
<cfset totalSpending = val(cartTotal) + val(totalsQuery.total) >
<cfoutput>#totalSpending#<
oops, I sat on the page waaay to long, came back and just submitted to find it was already answered! Sorry.
> <cfset var rsCustomerOrderTotals.tota l = 0 >
> Initializing the var but adding the query object as well.
If you forgot to VAR scope the query inside the function, that might explain it. Forgetting to localize function local variables can cause all sorts of weird threading problems. But .. you shouldn't need to add the ".total" at the end. Queries always exist - regardless of whether they return any records. So if yours doesn't there's something else wrong that you need to investigate.
> <cfset var rsCustomerOrderTotals.tota l = 0 >
FWIW, that creates a structure (not a query) with a key named "total". So do not try and use it as a query ;-)
> Initializing the var but adding the query object as well.
If you forgot to VAR scope the query inside the function, that might explain it. Forgetting to localize function local variables can cause all sorts of weird threading problems. But .. you shouldn't need to add the ".total" at the end. Queries always exist - regardless of whether they return any records. So if yours doesn't there's something else wrong that you need to investigate.
> <cfset var rsCustomerOrderTotals.tota
FWIW, that creates a structure (not a query) with a key named "total". So do not try and use it as a query ;-)
ASKER
<cflocation url="test.cfm?var=#rsCusto
or I can hard code it so it says...
<cfset totalSpending = val(cartTotal) + 100 > and that will also return the correct value.
Open in new window