Displaying a Total from a Table Column

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

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

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