This forum is archived. Posts are preserved for historical reference. For current help, join us on Discord.

Column Sum

In UserSpice 4.3 and Below · Started by matt on 2017-11-01 11:03 am · 6777 views · 2 replies

Im trying to add up the values of a single column but failing.....

<?php

$query = $db->query("SELECT SUM (sendout) from sendouts");
$count = $db->count();
echo $count;
var_dump($count);
?>
Count gives you the number of rows returned in your query. Use $db->first(); to return the first row which is all you will have given you are only selecting the sum. Also, after this, you will need to have the column you want to output.

I would:
-Change your select to be SUM(sendout) AS Total (or whatever you want to call it)
-Make your $count = to $db->first()->Total;

Then you can echo it :)
If you're still having a problem with this and getting your query right, you can do something like this.
$count = 0;
$query = $db->query("SELECT sendout FROM sendouts")->results();
foreach ($q as $sum){
$count = $count + $sum->sendout;
}
echo $count;

It's important to establish
$count = 0
outside of your foreach loop first.