繁体   English   中英

添加SQL计数值

[英]Adding values of SQL count

我有一个表,它使用foreach为每个用户回显一些SQL计数。 我想要一个总计,它将总计foreach的所有计数。 任何想法,将不胜感激。

 $students = get_course_students($course->id, 'lastname');    
    $toggle=0; 

if (!empty($students))  {
    foreach ($students as $student) {

$user=get_record('user','id',$student->id);

$atriskcountrecords = count_records_sql("SELECT COUNT(*) FROM mdl_ilp_tutorial WHERE template = 2 AND user = $user->id AND course = $course->id");
$personalcountrecords = count_records_sql("SELECT COUNT(*) FROM mdl_ilp_tutorial WHERE template = 1 AND user = $user->id AND course = $course->id");
$reportscountrecords = count_records_sql("SELECT COUNT(*) FROM mdl_ilp_reports WHERE user = $user->id AND course = $course->id");

echo '<tr class="r'.$toggle.'">
<td class="cell c'.$toggle.'" valign="top" nowrap="true" align="left">'.fullname($user).'</td>
<td class="cell c'.$toggle.'" valign="top" align="center">'.$atriskcountrecords.'</td>
<td class="cell c'.$toggle.'" valign="top" align="center">'.$personalcountrecords.'</td>
<td class="cell c'.$toggle.'" valign="top" align="center">'.$reportscountrecords.'</td>
</tr>';

if($toggle==0)$toggle=1;
else $toggle=0;         

}
}   

echo'<tr>
<td><strong>Totals</strong></td>
<td align="center"><strong>(TOTAL)</strong></td>
<td align="center"><strong>(TOTAL)</strong></td>
<td align="center"><strong>(TOTAL)</strong></td>
<td></td>
</tr>';


echo'</table>';

如果我真的了解您的问题,那很容易; 只需在foreach循环之前为每个计数器声明一个变量,然后将其增加计数值即可。 例如 :

$atriskcountrecordsGlobal = 0;
if (!empty($students))  {
    foreach ($students as $student) {
        ...
        $atriskcountrecords = count_records_sql("SELECT COUNT(*) FROM mdl_ilp_tutorial WHERE template = 2 AND user = $user->id AND course = $course->id");
        $atriskcountrecordsGlobal += $atriskcountrecords;
        ...
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM