The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.2.25 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/showthread.php 28 require_once





× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Total Sum of Column
#2
For some reason, I'm a big fan of foreach loops. If you do the sum in a query, it's going to come back as an object and actually the total will be represented as a string, which is a little bit of a pain. I tend to foreach loop it...

Code:
$usageQ = $db->query("SELECT month_usage FROM devices");
Code:
$usage = $usageQ->results();
Code:
foreach ($usage as $u) {
Code:
$sum+= $u->month_usage;
Code:
}
Code:
echo $sum;

If you want to do the sum in the query, you were really close.
Code:
$usageQ = $db->query("SELECT SUM(month_usage) FROM devices");
Code:
$usage = $usageQ->results();
Then you can see your results with
Code:
dump($usage);
  Reply


Messages In This Thread
Total Sum of Column - by winterswolf - 11-29-2016, 11:56 PM
Total Sum of Column - by mudmin - 11-30-2016, 01:41 AM
Total Sum of Column - by winterswolf - 11-30-2016, 04:55 AM
Total Sum of Column - by mudmin - 12-01-2016, 03:43 AM

Forum Jump:


Users browsing this thread: 2 Guest(s)