添加表的变量值以获得总计

时间:2013-08-29 15:28:28

标签: php mysql wordpress

我有一张表格,显示了25个问题的得分。答案范围为1 - 5.每个用户从数据库中检索答案,如下所示。我尝试了一些方法,但通常需要添加

希望有人可以帮助我

<table id="stats_table" border="1" width="50px">
    <tr>
        <th>Score</th>
    </tr>
    <?php
        global $wpdb;
        $current_user = wp_get_current_user();
        $result = $wpdb->get_results( "
            SELECT stats.*
            FROM wp_wp_pro_quiz_statistic stats
            JOIN wp_wp_pro_quiz_statistic_ref refs on stats.statistic_ref_id =   refs.statistic_ref_id
            WHERE refs.user_id= $current_user->ID ");
        foreach($result as $row) {
            echo "<tr><td><b>$row->points</b></td></tr>";
        }
    ?>
</table>

/*Final Score Table*/
<table width="960px" border="1">
    <tbody>
        <tr>
            <td width="445px">Total Score (Maximum 125)</td>
            <td width="50px">?</td>
        </tr>
     </tbody>
</table>

1 个答案:

答案 0 :(得分:1)

<table id="stats_table" border="1" width="50px">
        <tr>
            <th>Score</th>
        </tr>
<?php
global $wpdb;
$total_score = 0;
$current_user = wp_get_current_user();
$result = $wpdb->get_results( "
SELECT stats.*
  FROM wp_wp_pro_quiz_statistic stats
       JOIN wp_wp_pro_quiz_statistic_ref refs on stats.statistic_ref_id = refs.statistic_ref_id
WHERE refs.user_id= $current_user->ID ");
foreach($result as $row) {
echo "<tr><td><b>$row->points</b></td></tr>";
$total_score += $row->points;
}
?>
</table>

/*Final Score Table*/
<table width="960px" border="1">
<tbody>
<tr>
<td width="445px">Total Score (Maximum 125)</td>
<td width="50px"><?php echo $total_score; ?></td>
</tr>
</tbody>
</table>