Forums

Summary Data Help Needed

mindyk 25 Sep, 2009
Please forgive me, but my PHP coding skills are rudimentary at best.

Please bear with me!
My ChronoForm collects 2 user inputs, the date and the number of steps the person walked that day. Results are stored in a table called jos_chronoforms_walkform.

Field names are:
stepsdate
numofsteps

I managed to create a CC that displays a list of data based on user ID. I accomplished this using the WHERE clause
<?php
$user =& JFactory::getUser();
?>
WHERE `cf_user_id` = '<?php echo $user->id; ?>'


and using the appropriate {fieldname} tags in the body settomgs.

In the footer I want to display the sum of numofsteps for the logged in user. I've tried several variations of the code I've found in other posts, but while I don't get a sql error, nothing displays.

Can someone please help? Thank you!
mindyk 26 Sep, 2009
So I solved my own problem after some in-depth time on the Joomla docs wiki learning all about how to use database classes - http://docs.joomla.org/How_to_use_the_database_classes_in_your_script.

Here is my code, which is in the FOOTER of my CC in case some other poor person has to put a total at the end of a list. :wink:

<? 
//retrieve and summarize records for current user

$user =& JFactory::getUser();

$db =& JFactory::getDBO();

$query = "
  SELECT ".$db->nameQuote('cf_user_id').", sum(" .$db->nameQuote('numofsteps').") as 'SumOfSteps'
  FROM ".$db->nameQuote('jos_chronoforms_walkform')."  
  Where ".$db->nameQuote('cf_user_id')." = ".$db->quote($user->id)."
  Group BY ".$db->nameQuote(cf_user_id).";
  ";

$db->setQuery($query);
$result = $db->loadObject();
$totalSteps = $result->SumOfSteps;
$totalSteps = number_format($totalSteps)
?>


In the body of my CC, I have table with columns and rows for the logged in user. This code, which immediately follows the code in my footer, finishes off the table and displays the totalSteps result.

<tr>
<td colspan = "2"align="right">Total Steps:</td>
<td align="right"><?=$totalSteps?></td>
</tr>
</table>
GreyHead 26 Sep, 2009
Hi MidyK,

Great- glad you found my DB page useful (well not all mine).

You could possibly also have doen this by incrementing a variable through the body loop with something like $total_steps += $MyRow->steps

Bob
This topic is locked and no more replies can be posted.