# Need help with getting year totals

**URL:** <https://www.phphelp.com/t/need-help-with-getting-year-totals/21404>\
**Category:** General PHP Help\
**Created:** [October 29, 2013, 7:05am UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404 "2013-10-29T07:05:53Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![richei](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@richei](https://www.phphelp.com/u/richei)\
**Post date:** [October 29, 2013, 7:05am UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404/1 "2013-10-29T07:05:53Z")

</div>

ok, I have a script that that gets date ranges from a database to display as part of another script. I need to modify this script to display annual totals for the optgroup. This is the code as I have it now  
[php]$com\_cont = ‘’;  
$qry\_a = mysql\_query(“SELECT start\_date, end\_date FROM venzo\_app\_sales  
WHERE title IN (SELECT appname FROM venzo\_user\_apps WHERE uid = '”.$\_SESSION[‘uid’]."’)  
GROUP BY start\_date ORDER BY STR\_TO\_DATE(start\_date,’%m/%d/%Y’) desc"  
) or die(mysql\_error());

$sub = array();  
$sub[] = “”;  
$com\_cont.='Select Date Range: ';

while($r = mysql\_fetch\_array($qry\_a)) {

```
$tm = explode('/', $r['start_date']);
$yr = $tm[2];								
$dte = $r['start_date']. " - " .$r['end_date'];
				
$qry = mysql_query("SELECT partner_share_currency, extended_partner_share_currency AS psc FROM venzo_app_sales 
WHERE title IN (SELECT appname FROM venzo_user_apps WHERE uid = '".$_SESSION['uid']."')");
			
while($as = mysql_fetch_array($qry)) {
	//$as = mysql_fetch_array($qry);
	$as['total'] = $as['psc'] * $arr_currency[$as['partner_share_currency']];
				
}
//if($prev_yrs == $yr) {
$abs_total += $as['total'];
//	$prev_yrs = $tm[2];
//}
					
					
if($prev_yr != $yr) {
			
	$sub[] = "<optgroup label='$yr ($".number_format($abs_total, 2).")'>";
	$prev_yr = $tm[2];
}
$sub[] = "<option value='$dte'>$dte</option>";									
if($prev_yr != $yr) {
	$sub[] = "</optgroup>";
	$prev_yr = $tm[2];
}

```

}  
$sub[] = “”;

for($k = 0; $k \< count($sub); $k++) {  
$com\_cont.= $sub[$k];  
}[/php]  
This displays something like  
2013  
09/01/2013 - 09/30/2013  
08/01/2013 - 08/31/2013  
etc  
2012  
etc  
2011  
and so on

What I have to do is get the annual totals to display next to the year, so I end up with  
2013 ($1000.00)  
09/01/2013 - 09/30/2013  
08/01/2013 - 08/31/2013  
etc  
2012 ($800.00)  
etc  
2011 ($100.00)  
and so on

I’ve gotten it to return values, but they’re never correct. I know i’m close, but can’t figure out what i’m doing wrong. Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![Topcoder](https://www.phphelp.com/user_avatar/www.phphelp.com/topcoder/32/4356_2.png) [@Topcoder](https://www.phphelp.com/u/Topcoder)\
**Post date:** [October 29, 2013, 2:19pm UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404/2 "2013-10-29T14:19:56Z")

</div>

Richei, wouldn’t it be easier just to sum it up in the mysql query?

It also looks like your first query gets a date range, but the query inside the loop doesn’t limit it to the data range you selected, so it appears to be totaling everything.

---

<div class="post-metadata">

**Author:** ![richei](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@richei](https://www.phphelp.com/u/richei)\
**Post date:** [October 29, 2013, 4:54pm UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404/3 "2013-10-29T16:54:26Z")

</div>

No, because I don’t want to limit the calculation to just one month. I can’t use SUM in the query because the results to have to converted to US currency first

---

<div class="post-metadata">

**Author:** ![Topcoder](https://www.phphelp.com/user_avatar/www.phphelp.com/topcoder/32/4356_2.png) [@Topcoder](https://www.phphelp.com/u/Topcoder)\
**Post date:** [October 29, 2013, 6:20pm UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404/4 "2013-10-29T18:20:48Z")

</div>

And you can’t convert the currency in the query?

---

<div class="post-metadata">

**Author:** ![richei](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@richei](https://www.phphelp.com/u/richei)\
**Post date:** [October 29, 2013, 6:27pm UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404/5 "2013-10-29T18:27:55Z")

</div>

dont think so, wouldnt know how to it though. theres several counties in there too.

---

<div class="post-metadata">

**Author:** ![Topcoder](https://www.phphelp.com/user_avatar/www.phphelp.com/topcoder/32/4356_2.png) [@Topcoder](https://www.phphelp.com/u/Topcoder)\
**Post date:** [October 29, 2013, 6:42pm UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404/6 "2013-10-29T18:42:26Z")

</div>

What are you using to convert the currency, I don’t see anything that resembles that in what you posted.

---

<div class="post-metadata">

**Author:** ![richei](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@richei](https://www.phphelp.com/u/richei)\
**Post date:** [October 29, 2013, 7:03pm UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404/7 "2013-10-29T19:03:59Z")

</div>

theres an Included file that contains the code for that. the conversion is there though, as[psc]=

---

<div class="post-metadata">

**Author:** ![Topcoder](https://www.phphelp.com/user_avatar/www.phphelp.com/topcoder/32/4356_2.png) [@Topcoder](https://www.phphelp.com/u/Topcoder)\
**Post date:** [October 29, 2013, 8:02pm UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404/8 "2013-10-29T20:02:18Z")

</div>

I guess what I’m struggling with is this.

[php]$qry = mysql\_query(“SELECT partner\_share\_currency, extended\_partner\_share\_currency AS psc FROM venzo\_app\_sales  
WHERE title IN (SELECT appname FROM venzo\_user\_apps WHERE uid = '”.$\_SESSION[‘uid’]."’)");

```
while($as = mysql_fetch_array($qry)) {
	//$as = mysql_fetch_array($qry);
	$as['total'] = $as['psc'] * $arr_currency[$as['partner_share_currency']];
				
}[/php]

```

Are you expecting more then 1 row to come back? if you are, you keep resetting $as[‘total’] to the last row that’s returned from the query. I’m assuming you only get 1 row back.

---

<div class="post-metadata">

**Author:** ![richei](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@richei](https://www.phphelp.com/u/richei)\
**Post date:** [October 29, 2013, 8:05pm UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404/9 "2013-10-29T20:05:58Z")

</div>

Dude, you’re amazing 🙂 That’s supposed to be a += I’ll switch it when i get home tonight and see how it goes 🙂

---

<div class="post-metadata">

**Author:** ![richei](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@richei](https://www.phphelp.com/u/richei)\
**Post date:** [October 29, 2013, 9:21pm UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404/10 "2013-10-29T21:21:27Z")

</div>

That didn’t do the trick, was hoping that’s all it was ☹

---

<div class="post-metadata">

**Author:** ![richei](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@richei](https://www.phphelp.com/u/richei)\
**Post date:** [October 30, 2013, 8:34am UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404/11 "2013-10-30T08:34:32Z")

</div>

Well, I made a tiny bit of progress.

I decided to put all the dates into an array, took me 4 hours to do that, but i’m still not any further a long. I know what I need to do, just have no clue how to go about doing it. Updated code is below [php]\<?php  
session\_start();  
include ‘config.php’;  
//ini\_set(‘display\_errors’,1);  
//ini\_set(‘display\_startup\_errors’,1);  
//error\_reporting(-1);  
require ‘currency\_values.php’;  
$arr\_currency=get\_all\_currency\_values();

$com\_cont = ‘’;  
$prev\_year = ‘’;  
$prev\_yr = ‘’;

$qry\_a = mysql\_query(“SELECT start\_date, end\_date FROM venzo\_app\_sales  
WHERE title IN (SELECT appname FROM venzo\_user\_apps WHERE uid = '”.$\_SESSION[‘uid’]."’)  
GROUP BY start\_date ORDER BY STR\_TO\_DATE(start\_date,’%m/%d/%Y’) desc"  
) or die(mysql\_error());

$sub = array();  
$drange = array();

$sub[] = “”;  
$com\_cont.='Select Date Range: ';

while($r = mysql\_fetch\_assoc($qry\_a)) {

```
$tm = explode('/', $r['start_date']);
$yr = $tm[2];
$dte = $r['start_date']. " - " .$r['end_date'];

$sdate[] = $r['start_date'];
$months = array_unique($sdate);

if($prev_yr != $yr) {
	
	for($j=0; $j < count($months); $j++) {
		$qry = mysql_query("SELECT label, partner_share_currency, extended_partner_share_currency AS psc 
				 FROM venzo_app_sales WHERE sales_or_returns = 'S' AND title IN (SELECT appname FROM venzo_user_apps WHERE uid = '".$_SESSION['uid']."')
				 AND start_date = '$months[$j]'");
	 }
	
	while($row = mysql_fetch_assoc($qry)) {
		$row['psc_int'] = $row['psc'] * $arr_currency[$row['partner_share_currency']]; // currency conversion
		$rows[$row['label']][] = $row;
	}
	
	foreach($rows as $key => $row) {
		for($i = 0; $i < count($row); $i++) {
			$rows[$key]['total'] += $row[$i]['psc_int'];
			$total += $row['total'];					
			echo $total."<br />";
		}
	}
	
	$sub[] = "<optgroup label='$yr ($".number_format($total, 2).")'>";
	$prev_yr = $yr;
}

$sub[] = "<option value='$dte'>$dte</option>";

if($prev_yr != $yr) {
	$sub[] = "</optgroup>";
	$prev_yr = $tm[2];
}

```

}  
$sub[] = “”;

for($k = 0; $k \< count($sub); $k++) {  
$com\_cont.= $sub[$k];  
}  
echo $com\_cont;

?\>[/php]

---

<div class="post-metadata">

**Author:** ![Topcoder](https://www.phphelp.com/user_avatar/www.phphelp.com/topcoder/32/4356_2.png) [@Topcoder](https://www.phphelp.com/u/Topcoder)\
**Post date:** [October 30, 2013, 2:13pm UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404/12 "2013-10-30T14:13:21Z")

</div>

You’re still doing that loop without adding…

[php] while($row = mysql\_fetch\_assoc($qry)) {  
$row[‘psc\_int’] = $row[‘psc’] \* $arr\_currency[$row[‘partner\_share\_currency’]]; // currency conversion  
$rows[$row[‘label’]][] = $row;  
}[/php]

So are you really only get 1 row back per query?

---

<div class="post-metadata">

**Author:** ![richei](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@richei](https://www.phphelp.com/u/richei)\
**Post date:** [October 30, 2013, 5:45pm UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404/13 "2013-10-30T17:45:33Z")

</div>

it works in the other scripts, but its probably not working as it should.

im on my phonit works in the other scripts, but its probably not working as it should.

I think the totals script needs an overhaul at this point. what im using now is part of a date picking script for a sales script

---

<div class="post-metadata">

**Author:** ![Topcoder](https://www.phphelp.com/user_avatar/www.phphelp.com/topcoder/32/4356_2.png) [@Topcoder](https://www.phphelp.com/u/Topcoder)\
**Post date:** [October 30, 2013, 6:27pm UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404/14 "2013-10-30T18:27:54Z")

</div>

I understand it a little better now, but I don’t understand where $row[‘total’] gets populated. Wouldn’t $total always be blank… If you make the change below wouldn’t that sum up the totals?

Change:

[php] foreach($rows as $key =\> $row) {  
for($i = 0; $i \< count($row); $i++) {  
$rows[$key][‘total’] += $row[$i][‘psc\_int’];  
$total += $row[‘total’];   
echo $total."  
";  
}  
}[/php]

To:

[php] foreach($rows as $key =\> $row) {  
for($i = 0; $i \< count($row); $i++) {  
$rows[$key][‘total’] += $row[$i][‘psc\_int’];  
$total += $row[$i][‘psc\_int’];   
echo $total."  
";  
}  
}[/php]

---

<div class="post-metadata">

**Author:** ![richei](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@richei](https://www.phphelp.com/u/richei)\
**Post date:** [October 30, 2013, 7:24pm UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404/15 "2013-10-30T19:24:52Z")

</div>

Didn’t work.

I’m thinking maybe its the second query that’s messing things up. Maybe instead of trying to do it month by month, there’s a way to do it with a given range? I already have an array with the months in it, if that can be used to create 2 other variables, a fresh start date and an end date.

idk, i’m thinking out loud now. If you had this to do, how you would go about doing it?

---

<div class="post-metadata">

**Author:** ![Topcoder](https://www.phphelp.com/user_avatar/www.phphelp.com/topcoder/32/4356_2.png) [@Topcoder](https://www.phphelp.com/u/Topcoder)\
**Post date:** [October 30, 2013, 8:25pm UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404/16 "2013-10-30T20:25:11Z")

</div>

I would try to do it all in a sql statement and where ever you are pulling the data from for the currency conversion I would toss that in a database as well and get the results you want with 1 query.

---

<div class="post-metadata">

**Author:** ![richei](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@richei](https://www.phphelp.com/u/richei)\
**Post date:** [October 30, 2013, 8:53pm UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404/17 "2013-10-30T20:53:47Z")

</div>

the conversion rates are in a table. ill post the code for it.

---

<div class="post-metadata">

**Author:** ![richei](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@richei](https://www.phphelp.com/u/richei)\
**Post date:** [November 1, 2013, 7:21am UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404/18 "2013-11-01T07:21:43Z")

</div>

Well, I made a tiny bit of progress on this. I have part of the array created, but it gets wiped out when add in the conversions. This is the array print\_r[code]Array  
(  
[2013] =\> Array  
(  
[startdate] =\> Array  
(  
[0] =\> 09/01/2013  
[1] =\> 06/30/2013  
[2] =\> 06/02/2013  
[3] =\> 05/05/2013  
[4] =\> 03/31/2013  
[5] =\> 03/03/2013  
[6] =\> 02/03/2013  
)

```
    )

[2012] => Array
    (
        [startdate] => Array
            (
                [0] => 12/30/2012
                [1] => 12/02/2012
                [2] => 11/04/2012
                [3] => 08/05/2012
                [4] => 07/01/2012
                [5] => 06/03/2012
                [6] => 05/06/2012
                [7] => 04/01/2012
                [8] => 03/04/2012
                [9] => 02/05/2012
                [10] => 01/01/2012
            )

    )

[2011] => Array
    (
        [startdate] => Array
            (
                [0] => 11/27/2011
                [1] => 10/30/2011
            )

    )

```

)[/code]and this is the code[php]\<?php  
session\_start();  
$\_SESSION[‘uid’] = 62;  
function display($arg) {  
echo “

```
”;
print_r($arg);
echo “
```
”;  
}

include ‘config.php’;  
//ini\_set(‘display\_errors’,1);  
//ini\_set(‘display\_startup\_errors’,1);  
error\_reporting(-1);  
require ‘currency\_values.php’;  
$arr\_currency=get\_all\_currency\_values();

$com\_cont = ‘’;  
$total = 0;

$qry = mysql\_query(“SELECT start\_date, end\_date FROM venzo\_app\_sales WHERE title IN (SELECT appname FROM venzo\_user\_apps WHERE uid = '”.$\_SESSION[‘uid’]."’) GROUP BY start\_date ORDER BY STR\_TO\_DATE(start\_date,’%m/%d/%Y’) desc") or die(mysql\_error());

$sub = array();  
$dt = array();  
$dates = array();  
$sort = array();

$sub[] = “”;  
$com\_cont.='Select Date Range: ';

while($r = mysql\_fetch\_assoc($qry)) {

```
$tm = explode('/', $r['start_date']);
$yr = $tm[2];
$dte = $r['start_date']. " - " .$r['end_date'];
$month[$yr]['startdate'][] = $r['start_date'];

```

}   
display($month);  
$sub[] = “”;

for($k = 0; $k \< count($sub); $k++) {  
$com\_cont.= $sub[$k];  
}[/php]This is what I need the array to look like[code]  
[2011] =\> Array  
(  
[startdate] =\> Array  
(  
[0] =\> 11/27/2011  
[psc\_int] =\> Array  
(  
[0] =\> 1.00  
[1] =\> 2.00  
)  
[1] =\> 10/30/2011  
[psc\_int] =\> Array  
(  
[0] =\> 1.00  
[1] =\> 2.00  
)  
)

```
    )[/code]

```

But when I add in[php]foreach ($month as $year =\> $start) {  
$dates = array\_shift($month[$year]);  
}  
foreach($dates as $dt) {  
for($q=0; $q \< count($dt); $q++) {  
$qry\_b = “SELECT partner\_share\_currency, extended\_partner\_share\_currency psc FROM venzo\_app\_sales  
WHERE start\_date = '”.$dt[$q]."’ AND title IN (SELECT appname FROM venzo\_user\_apps WHERE uid = “.$\_SESSION[‘uid’].”)  
ORDER BY STR\_TO\_DATE(start\_date,’%m/%d/%Y’) desc";   
$qry\_c = mysql\_query($qry\_b) or die(mysql\_error());  
}  
}

```
while($row = mysql_fetch_assoc($qry_c)) {
	$month[$yr]['startdate']['psc_int'][] = $row['psc'];
}[/php]$month[$yr]['startdate']['psc_int'][] = $row['psc']; wipes out the array completely, leaving just the year
```

---

<div class="post-metadata">

**Author:** ![richei](https://www.phphelp.com/letter_avatar_proxy/v4/letter/r/e5b9ba/32.png) [@richei](https://www.phphelp.com/u/richei)\
**Post date:** [November 5, 2013, 7:26am UTC](https://www.phphelp.com/t/need-help-with-getting-year-totals/21404/19 "2013-11-05T07:26:29Z")

</div>

ok, well I’ve been told that what I need done can be done in the query if switch the data type to date, which I’ve done (which has pretty much broken the sales page for now). But I don’t know how to set up the query to get the results I need.
