Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting sum of values in array 1

Status
Not open for further replies.

curvv

IS-IT--Management
Jan 11, 2001
103
ZA
if I have 2, 6, 9, 2, 3 values in an array, how do i get the sum of all those values without knowing how many values there are?

thanx ######## CtN ########
 
Several ways. You can do it manually:

[tt]
for ($i=0; $i<count($array); $i++) {
$tot += $array[$i];
}
[/tt]

Or you can use the PHP function array_sum() like so:

[tt]
$sum = array_sum($array);
[/tt]

Hope this helps.

-Vic vic cherubini
krs-one@cnunited.com
 
Hi V,

When I use array_sum(); I get double the correct value.


????

######## CtN ########
 
Hmm, I am not sure. array_sum() is a new function, and if you are using an older version of PHP (like version 4.04 or 4.05, then it might be buggy).

If that doesn't work, you can always try the first way. Thats guranteed to work.

Hope this helps.
-Vic vic cherubini
krs-one@cnunited.com
 
Hi V,
I must be doing something wrong.
in the array i have values 11, 10, 6. The answer i get is 6.
Here's a bit of the code:
$r_queryt=mysql_query(&quot;SELECT bill_hrs FROM tbl_time WHERE user_id='$user_id' AND date >= '$from_date' AND date <= '$to_date' AND project='$projectr'&quot;);

$array = mysql_fetch_array($r_queryt);

for ($i=0; $i<count($array); $i++) {
$tot += $array[$i];
} ######## CtN ########
 
Hmmm. I don't know what to say, assuming your SQL query returns a positive result.

Try printing out the values of the array first to make sure that you are getting them right and the array is populated.

You could also try something like this:

[tt]
$r_queryt=mysql_query(&quot;SELECT bill_hrs FROM tbl_time WHERE user_id='$user_id' AND date >= '$from_date' AND date <= '$to_date' AND project='$projectr'&quot;);

while ($row = mysql_fetch_array($r_queryt)) {
$tot += $row['tbl_time'];
}

[/tt]

Hope this helps.

-Vic
vic cherubini
krs-one@cnunited.com
 
You are returning multiple records!

Try:
Code:
while($array = mysql_fetch_array($r_queryt)) {
     $tot += $array[$i];
}

This should work.

Chad. ICQ: 54380631
 
Oh yeah, if you are only grabbing these numbers to perform a total sum, and doing nothing else with them, you can do:
Code:
$r_queryt=mysql_query(&quot;SELECT SUM(bill_hrs) 
                       FROM tbl_time 
                       WHERE user_id='$user_id' 
                       AND date >= '$from_date' 
                       AND date <= '$to_date' 
                       AND project='$projectr'&quot;);
$array = mysql_fetch_row($r_queryt);
$total = $array[0];

and $total will contain the total of all of your numbers.

Sincerely,
Chad. ICQ: 54380631
 
Hi,

thanx alot V,
This worked:

while ($row = mysql_fetch_array($r_queryt)) {
$tot += $row['bill_hrs'];
}

later ######## CtN ########
 
Your welcome.

Hope I can help some more in the future.

-Vic vic cherubini
krs-one@cnunited.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top