Adding together all entries of a database field

3 posts by 2 authors in: Forums > CMS Builder
Last Post: April 2, 2009   (RSS)

Re: [dwelling] Adding together all entries of a database field

By ross - April 1, 2009

Hi Jeremy

Thanks for posting!

What I would do is create a separate page called totalScore.php or something like that and set it up like a list viewer page for this section.

All you'll need it to do is output the score from each member. You don't need any HTML. Just the view code, the foreach loop and the score field.

Right out side the foreach loop, create a variable called $totalScore. Then inside the foreach loop, increment that variable with the value from your score field.

$totalScore += $record['score']

After the forloop ends, put this: echo $totalScore

You should then be able to include this file on your members pages.

Does that make sense? Give it a shot and let me know if I can elaborate on any of this.
-----------------------------------------------------------
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] Adding together all entries of a database field

Ross,

Once again, thanks so much! That's exactly what I needed. I ended up creating a page with HTML on it just so I could test it on the server while I was working with it. Then, I just included the page itself, instead of the variable only.

In case anyone is interested, here's the code for the page

--------------------------------------------------------------------------

<?php
require_once "/home/content/p/h/c/phchurch/html/cmsAdmin/lib/viewer_functions.php";

list($membersRecords, $membersMetaData) = getRecords(array(
'tableName' => 'members',
'loadUploads' => '0',
'allowSearch' => '0',
));

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>

<?php $totalScore = 0;?>

<?php foreach ($membersRecords as $record):?>
<?php $record['totalPoints'];?>
<?php $totalScore += $record['totalPoints'];?>
<?php endforeach;?>

<?php if (!$membersRecords):?>
0
<?php endif; ?>


<?php echo number_format($totalScore); ?>

</body>
</html>

--------------------------------------------------------------------------

Then, to include the page: <?php include 'totalScore.php'; ?>

--------------------------------------------------------------------------

Thanks again! - Jeremy
---------------------------

Dwelling Productions

www.dwellingproductions.com