# Need help with count total function

**URL:** https://forum.kirupa.com/t/need-help-with-count-total-function/181200
**Category:** Uncategorized
**Created:** [March 7, 2006, 9:30am UTC](https://forum.kirupa.com/t/need-help-with-count-total-function/181200 "2006-03-07T09:30:51Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![pixeldude](https://avatars.discourse-cdn.com/v4/letter/p/57b2e6/32.png) [@pixeldude](https://forum.kirupa.com/u/pixeldude)
#### Post date: [March 7, 2006, 9:30am UTC](https://forum.kirupa.com/t/need-help-with-count-total-function/181200/1 "2006-03-07T09:30:51Z")

</div>

Hi…

okay I just need help with my totalcount function…the ideas is user can click how many items then it will be add in datagrid component…so my problems is I can’t make total sum for the cost of product… I mean like

the total sum value suppose to incrementing everytime the podducts being add…eg;

```
       100
     1000
      200
      100

```

total :1400

So how to achieve it?..maybe some little guide would be appreciate…

here’s my AS so far

```auto

stop();

var productArr:Array = [["tshirt","100"],["jeans","1000"],["shoes","200"]];

createEmptyMovieClip("paper",1);
for (var i = 0;i<productArr.length;i++){
	var mc = paper.attachMovie("item","item"+i,100+i);
	mc.ivar = i;
	mc._x = 100;
	mc._y = 50+(mc._width*i);
	mc.pro.text = productArr*[0];
	mc.cost.text = productArr*[1];
	mc.onPress = adddt;
}

// Add columns to grid and add data.
function adddt(){
	var item_obj:Object = {Product:productArr[this.ivar][0], Cost:productArr[this.ivar][1]};
	my_dg.addItem(item_obj);
	countTotal(productArr[this.ivar][1]);
}

//function calculate total
totalArr = [];
//this portion are little bit messy..and having problems
//really need help with it...
function countTotal(val){
	var totalval = 0; 
	totalArr.push(val);
	for(j=0;j<totalArr.length;j++){
		total.text = totalval + totalArr[j];
	}

}
   

```

tq in advanced
