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!
brihol44Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ArgentiCommented:
Hello,

Try initializing your variables by putting
<cfset totalSpending = 0 >

Open in new window

and
<cfset cartTotal = 0 >

Open in new window

before your code.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brihol44Author Commented:
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

0
ArgentiCommented:
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?
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

_agx_Commented:
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.

I don't see any obvious reason that should happen, based on what you've shown.

Sorry if you guys covered this already, but ...
1. What's  the actual code causing the problem? ie Original code for QuerySelectCustomerTotalSpent?

2. What kind of variable is #cartTotal# - URL, FORM, ...?

3. Run the original code, and output all of the variables, what are the values?

<cfset totalsQuery = QuerySelectCustomerTotalSpent(
                                           customer_id = arguments.customer_id
                                           , pastDate=pastDate)>
<cfset totalSpending = val(cartTotal) + val(totalsQuery.total) > 

<cfdump var="#totalsQuery#" label="Query Dump">
<cfoutput>
raw totalsQuery.total = #totalsQuery.total#<br>
raw cartTotal = #cartTotal#<br>

val(totalsQuery.total) = #val(totalsQuery.total)#<br>
val(cartTotal) = #val(cartTotal)#<hr>
totalSpending = #totalSpending #<br>
</cfoutput>

Open in new window

0
brihol44Author Commented:
So the solution was ...

<cfset var rsCustomerOrderTotals.total = 0 >

Initializing the var but adding the query object as well.
0
gdemariaCommented:
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>
0
gdemariaCommented:
oops, I sat on the page waaay to long, came back and just submitted to find it was already answered!  Sorry.
0
_agx_Commented:
> <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 ;-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.