tags:

views:

4019

answers:

4

Hi,

Anyone knows how to get sum of number?

For example i have mysql column name package.

Package
3
4
1
3
4

If package 3, the value should be usd 5, if package 4, value should be usd 10, if package 1, value should be usd 1 and so on.

So the total value above should be => 5 + 10 + 1 + 5 + 10 = 31

So how do i get sum of it in php?

i tried array_sum .. but it gives me error, im not very expert in php.. Please help me

Thanking in advance.

+4  A: 

Get the database to do the work:

select sum(CASE WHEN package = 3 THEN 5 ELSE 0 END)
       + sum(CASE WHEN package = 4 THEN 10 ELSE 0 END)
       + sum(CASE WHEN package = 1 THEN 1 ELSE 0 END) AS total
from table_name

The right way of doing it, if you've got more than a few values be to modify the table to contain the amount field so you can do:

select sum(amount) from table_name

Alternatively have another table that you contained the relationship between the values (package, amount), and then do

select sum(package_amount) 
from table_name, lookup_values
where table_name.package = lookup_values.package

Or, if you really want to do it in PHP:

$amount = array(3=>5, 4=>10, 1=>1);  // define the amount of each value
$result = mysql_query("SELECT id FROM table_name");
$sum = 0;

while ($row = mysql_fetch_assoc($result))
{
    $sum += $amount[ $row['id'] ];
}
Richard Harrison
This is a pretty ugly solution and one that won't scale and will be hard to change/maintain. If the weighting (eg 5 for package 3) can't be obtained from another table/field (which leads to much cleaner SQL) I would go with the PHP calculation over this.
cletus
sometimes for a few values it is better to do it in the SQL using the method I describe - that way if you do decide to add a table the PHP code stays the same and you just change the SQL...
Richard Harrison
richard did i ever mention you are genious? your third way works like charm. thanks very much mate.
A: 

hmm lemme try, thanks

+1  A: 

You can do it in purely SQL like Richard did, the PHP version would be something like:

$result = mysql_query("SELECT `number` FROM `packages`");

while ($row = mysql_fetch_assoc($result))
{
    $curr = $row['number'];
    if ($curr == 3) $sum += 3;
    if ($curr == 4) $sum += 10;
    if ($curr == 1) $sum++;
}

This is assuming your database table is named 'packages' and the column that has the different package numbers is called 'number'. At the end of the loop you will be left with the sum in the $sum variable. Not sure if this is necessarily the most efficient way to do this in PHP.

Logan Serman
Unless you can pull those weightings from another table/field in the database I'd go with this option rather than hardcoding values with CASE statements in the SQL.
cletus
+1  A: 

I think a neater way to do it in PHP would be to declare an associative array of weights, this separates the data from the algorithm:

$weight=array(
  1=>1,
  3=>3,
  4=>10
);

$result = mysql_query("SELECT `number` FROM `packages`");

$sum=0;
while ($row = mysql_fetch_assoc($result))
{
    $n = $row['number'];
    if (isset($weight[$n])) 
    {
         $sum+=$weight[$n];
    }
    else
    {
          #handle error somehow, maybe with a default weight
          die("Unexpected value $n");
     }
}

An better alternative would be to make this entirely data driven and put your weights in a table

#create and populate the weights table
create table weight (number int, weight int, primary key(number));
insert into weight (number,weight) values (1,1),(3,3),(4,10);

#figure out total weight
select sum(weight) from package inner join weight using(number);

With weights in the database, you could provide a UI to alloww users of your application to adjust the weights.

Paul Dixon