Displaying a Total from a Table Column

4 posts by 3 authors in: Forums > CMS Builder
Last Post: October 29, 2009   (RSS)

By (Deleted User) - October 27, 2009 - edited: October 27, 2009

I am tring to figure out how to create a Total Column

If I have a Table called servers and then column called number_boxes

I want to be get a code to take all the numbers in that number_boxes column and display the total.

And what do i have to put in the above call for PHP for it to work?

I know it was this

<?php SELECT SUM(weight) AS total FROM `weight_management`;?>

But do not know how to incorporate that with CMSBuilder

Re: [hegadmin] Displaying a Total from a Table Column

By ross - October 28, 2009

Hi there.

Thanks for posting!

The way I would do this is with a "total" variable sort of like this:

<?php $total = 0; ?>

<?php foreach ($serverRecords as $record): ?>
<?php $total .= $record['number_boxes']; ?>
<?php endforeach ?>

Does that make sense? Basically, you create one variable and then each time you cycle through the foreach loop, it adds the value of number_boxes for that record to the running total.

Anywhere after that loop you can display or use $total in an equastion. How does that sound?

Let me know what you think :).
-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

Re: [ross] Displaying a Total from a Table Column

By (Deleted User) - October 28, 2009

I tried that and it gave me either Blank Answer or Notice: Undefined variable: total in