Link to home
Start Free TrialLog in
Avatar of brihol44
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.total#</cfoutput> and I see the actual number and not 0.

      
	<cfset totalsQuery = QuerySelectCustomerTotalSpent(customer_id = arguments.customer_id, pastDate=pastDate)>

		<cfset totalSpending = val(cartTotal) + val(totalsQuery.total) > 

Open in new window


Thanks for the insight!
ASKER CERTIFIED SOLUTION
Avatar of Argenti
Argenti
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of brihol44
brihol44

ASKER

Hmm.. still getting the issue. I just pulled the query into the code directly. I am using a cffunction. It might not be the best way but when I insert

<cflocation url="test.cfm?var=#rsCustomerOrderTotals.total#"> after declaring,  I see the value within the URL page change so I know the query is correct. It's just coming across as "0" when being returned now.

or I can hard code it so it says...

<cfset totalSpending = val(cartTotal) + 100 > and that will also return the correct value.

<cffunction....

		<cfquery name="rsCustomerOrderTotals" datasource="#application.cw.dsn#" username="#application.cw.dsnUsername#" password="#application.cw.dsnPassword#">
		SELECT
			SUM(orders.order_total) AS total
		FROM
			orders
		WHERE
			orders.order_customer_id = '#arguments.customer_id#' <!---AND dateDiff(order_date, NOW()) > #arguments.pastDate# ---> 
		ORDER BY
			orders.order_date DESC
		</cfquery>

		<cfset var totalSpending = 0>
             
		<cfset totalSpending = val(cartTotal) + val(rsCustomerOrderTotals.total) >

... /cffunction>

Open in new window

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:
<cfset 
    var variable_name = expression>

Open in new window

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
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So the solution was ...

<cfset var rsCustomerOrderTotals.total = 0 >

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 = QuerySelectCustomerTotalSpent(customer_id = arguments.customer_id, pastDate=pastDate)>

<cfoutput>#totalsQuery.total#</cfoutput>
<cfset totalSpending = val(cartTotal) + val(totalsQuery.total) > 

<cfoutput>#totalSpending#</cfoutput>
oops, I sat on the page waaay to long, came back and just submitted to find it was already answered!  Sorry.
> <cfset var rsCustomerOrderTotals.total = 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.total = 0 >

FWIW, that creates a structure (not a query) with a key named "total". So do not try and use it as a query ;-)