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: [hegadmin] Displaying a Total from a Table Column

By Dave - October 29, 2009

Without seeing the viewer file it's hard to know what the problem might be.

If you know MySQL, though, you can use that directly. When you load the viewer_functions.php file it automatically connects to the CMSB database for you. So you can then just call mysql_query() and other functions as needed.

Or you can use a CMSB database function to return the first record like this:

require_once "cmsAdmin/lib/viewer_functions.php"; // update to match your server path
list($total) = mysql_query_fetch_row_array(" SELECT SUM(weight) AS total FROM `cms_weight_management`");
print "Total: $total";

Make sure you have a recent version of CMSB and that you replace cms_ with your CMSB table prefix.

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com