6 Replies - 631 Views - Last Post: 29 September 2009 - 08:40 AM

#1 chosen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 24-July 06

Add Cart Total

Posted 28 September 2009 - 08:28 AM

Hello,

I am having trouble adding the two prices received from the user input. Currently I have a basic .fla file that has 2 numeric steppers. The code takes the price * the quanitity chosen from the numeric stepper to display the total. However, I am getting two individual totals instead of both inputs added for a grand total. I was attempting to push the two totals into a function that will add both values but I had no success. I am posting my .fla for your review. Thanks in advance

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Add Cart Total

#2 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Add Cart Total

Posted 28 September 2009 - 08:33 AM

Without downloading, I suspect your problem is that your input is coming out as a string. You need to force the type of the variables to Number first.
var input1 = "2";
var input2 = "2";

trace(input1 + input2); //22

trace(Number(input1) + Number(input2)); //4


Was This Post Helpful? 0
  • +
  • -

#3 chosen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 24-July 06

Re: Add Cart Total

Posted 28 September 2009 - 09:52 AM

Thanks thehat for your reply. I tried your suggestion and got undefined. I have also set up a function that will now add the two values however the error I am receiving is the first number + undefined. See .fla below. Thank you!


import mx.controls.*;
import fl.controls.dataGridClasses.*;
class Cart2 {
	var CartCalc:String;
	var quantity:NumericStepper;
	var Product:Number;
	var mynum:String;
	var CartTotal:String;
	var CartTotal2:String;
	var CalcMe:Number;
	var Total:String;
	public function GetUserSelection(){ 
var quantity = _level0.TESTMC.myvalue.value;
trace("Quantity in AS"+ _level0.TESTMC.myvalue.value);
	
		Product = 5.00;
		var CalculateValue = (Product*quantity*100)/100;
		
		CartTotal=String(CalculateValue);
		var num_array = CartTotal.split(".");  
		//trace ("Num_array is  " + num_array);
		/*_level0.CartTotal=String(CalculateValue);
		_level0.CartTotal.indexOf(".") == -1 ? _level0.CartTotal += ".00" : _level0.CartTotal += "0";*/
		  drawCartTotal()
  }  
 

public function GetU() { 
var quantity = _level0.TESTMC.myvalue2.value;
//trace("Quantity AS"+ _level0.TESTMC.myvalue2.value);
	
		Product = 5.00;
		CalcMe = (Product*quantity*100)/100;
		
		CartTotal2=String(CalcMe);
		var num_array = CartTotal.split(".");  
		//trace ("Num_array is  " + num_array);
		
		/*_level0.CartTotal=String(CalcMe);
		_level0.CartTotal.indexOf(".") == -1 ? _level0.CartTotal += ".00" : _level0.CartTotal += "0";*/
   drawCartTotal()
 }  
 function drawCartTotal(MyTotal:Number){
	 Total= CartTotal+ CartTotal2
	 _level0.CartTotal=String(Total);
	_level0.CartTotal.indexOf(".") == -1 ? _level0.CartTotal += ".00" : _level0.CartTotal += "0";
 }
}


Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#4 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Add Cart Total

Posted 29 September 2009 - 06:04 AM

The reason you're getting an undefined is because you never set a value for CartTotal2. You are also trying to do an addition operation on two strings, which is just going to result in concatenation.

At the top of your class where you first define CartTotal and CartTotal2, set an initial value for them and change their type to number:
var CartTotal:Number = 0;
var CartTotal2:Number = 0;



Next, in your GetUserSelection function find these two lines:
CartTotal=String(CalculateValue);
var num_array = CartTotal.split(".");


Because CartTotal is now a number, you don't need the string conversion or the split operation, so they become this:
CartTotal=CalculateValue;



You're going to need to make similar changes to the GetU function to remove all the string references, so that this:
CartTotal2=String(CalcMe);
var num_array = CartTotal.split(".");


becomes this:
CartTotal2=CalcMe;



So now things should be working a bit better. Clicking add to cart now updates with a number and no errors. Your next problem is that the total is reset every time rather that being added to. This is because you are creating a new instance of your class every button press, so all the previous values are lost. The way to fix this is to create one instance of the class which both buttons can then use.

Change the code on your buttons to:
on(release){
	_parent.accesscart.GetUserSelection();
}


and
on(release){
	_parent.accesscart.GetU();
}



Finally, instantiate your class on the first frame of TESTMC and you're good to go.
var accesscart:Cart2= new Cart2;


This post has been edited by thehat: 29 September 2009 - 06:06 AM

Was This Post Helpful? 0
  • +
  • -

#5 chosen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 24-July 06

Re: Add Cart Total

Posted 29 September 2009 - 07:42 AM

Ok, thank you so much....I believe I am almost there!

The code CartTotal.split was used to add in the decimals in the Total Price. How do I go about doing that now since this line has been deleted?
var num_array= CartTotal.split(".");



Also, It add correctly on the first two times you input a quantity and submit but if you keep going back and adding numbers it seems the math is off a bit. Here is the latest code with the modifications based on your suggesstions as well as the .fla. Again thank you!

import mx.controls.*;
import fl.controls.dataGridClasses.*;
class Cart2 {
	var CartCalc:String;
	var quantity:NumericStepper;
	var Product:Number;
	var mynum:String;
	var CartTotal:Number=0;
	var CartTotal2:Number=0;
	var CalcMe:Number;
	var Total:String;
	
	
	public function GetUserSelection(){ 
	var quantity = _level0.TESTMC.myvalue.value;
	//trace("Quantity in AS"+ _level0.TESTMC.myvalue.value);
	
		Product = 5.00;
		var CalculateValue = (Product*quantity*100)/100;
		CartTotal=CalculateValue;
		trace("Cart Total" + CartTotal);
		
		/*_level0.CartTotal=(CalculateValue);
		_level0.CartTotal.indexOf(".") == -1 ? _level0.CartTotal += ".00" : _level0.CartTotal += "0";*/
	drawCartTotal();
		}  
 

	public function GetU() { 
	var quantity = _level0.TESTMC.myvalue2.value;
	//trace("Quantity AS"+ _level0.TESTMC.myvalue2.value);
	
		Product = 5.00;
		CalcMe = (Product*quantity*100)/100;
		CartTotal2= CalcMe;
		trace("Cart Total " + CartTotal2);
		/*_level0.CartTotal=(CalcMe);
		_level0.CartTotal.indexOf(".") == -1 ? _level0.CartTotal += ".00" : _level0.CartTotal += "0";*/

		drawCartTotal();
 }  
 function drawCartTotal(MyTotal:Number){
	 MyTotal= CartTotal+ CartTotal2;
	_level0.CartTotal=MyTotal;
	_level0.CartTotal.indexOf(".") == -1 ? _level0.CartTotal += ".00" : _level0.CartTotal += "0";

}


}



Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#6 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Add Cart Total

Posted 29 September 2009 - 08:22 AM

Because you're working with numbers now, any decimals that are required will be added and used normally. The final line in your drawCartTotal function adds any zeros that are missing.

As for the math being off, each click of the button doesn't currently add that value to the total. Instead, it performs adjustments to the number based on the changes to your quantities. Adding a new amount to total with every click needs to be done a bit differently. The good news is that it gives us an oppertunity to remove some code and make things a bit simpler.

First, change the declaration of Total to be a number and set it's value to 0:
var Total:Number = 0;



Next you need to make changes to the GetUserSelection and GetU functions. What's going to happen now is that your calculated value will be passed directly to the drawCartTotal function:
public function GetUserSelection(){ 
   var quantity = _level0.TESTMC.myvalue.value;
   trace("Quantity in AS"+ _level0.TESTMC.myvalue.value);
	
   Product = 5.00;
   var CalculateValue = (Product*quantity*100)/100; 
		
   drawCartTotal(CalculateValue)
}

public function GetU() { 
	var quantity = _level0.TESTMC.myvalue2.value;
	trace("Quantity AS"+ _level0.TESTMC.myvalue2.value);
	
	Product = 5.00;
	CalcMe = (Product*quantity*100)/100;
		 
	drawCartTotal(CalcMe);
}



Finally, the drawCartTotal function will now receive the value passed to it from the two previous functions, and simply add that on to Total:
function drawCartTotal(MyTotal:Number){
	Total += MyTotal;
	_level0.CartTotal=String(Total);
	_level0.CartTotal.indexOf(".") == -1 ? _level0.CartTotal += ".00" : _level0.CartTotal += "0";
}


Was This Post Helpful? 0
  • +
  • -

#7 chosen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 24-July 06

Re: Add Cart Total

Posted 29 September 2009 - 08:40 AM

Kudos! That works exactly like I was trying to get it to perform. Thanks again for all your help & tips! Much Success.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1