获取每条记录中所有行的总和

时间:2017-12-21 05:56:12

标签: php mysql

帮助我们。我在如何总结学生的所有记录方面遇到了问题 在每一行。这是我的PHP代码

 while ($students = mysql_fetch_array($result)) {
    echo '<tr>';
    echo "<td>{$students['id']}<input type='hidden' name='id[$i]' value='{$students['id']}' /></td>";
    echo "<td>{$students['fld_name']}</td>";
    echo "<td><input type='text' size='5' name='fld_quiz1[$i]' value='{$students['fld_quiz1']}' /></td>";
    echo "<td><input type='text' size='5' name='fld_quiz2[$i]' value='{$students['fld_quiz2']}' /></td>";
    echo "<td><input type='text' size='5' name='fld_quiz3[$i]' value='{$students['fld_quiz3']}' /></td>";
    echo "<td><input type='text' size='5' name='fld_quiz4[$i]' value='{$students['fld_quiz4']}' /></td>";
    echo "<td><input type='text' size='5' name='fld_quiz5[$i]' value='{$students['fld_quiz5']}' /></td>";
    echo '<td> total here </td>';
    echo '</tr>';

    ++$i;
}

这是我的输出

enter image description here

2 个答案:

答案 0 :(得分:2)

您可以添加所有值并将其存储在一个变量中并在最后一列中显示。请参阅下文,

while ($students = mysql_fetch_array($result)) {
   $total = $students['fld_quiz1'] + $students['fld_quiz2'] + $students['fld_quiz3'] + $students['fld_quiz4'] + $students['fld_quiz5'];
    echo '<tr>';
    echo "<td>{$students['id']}<input type='hidden' name='id[$i]' value='{$students['id']}' /></td>";
    echo "<td>{$students['fld_name']}</td>";
    echo "<td><input type='text' size='5' name='fld_quiz1[$i]' value='{$students['fld_quiz1']}' /></td>";
    echo "<td><input type='text' size='5' name='fld_quiz2[$i]' value='{$students['fld_quiz2']}' /></td>";
    echo "<td><input type='text' size='5' name='fld_quiz3[$i]' value='{$students['fld_quiz3']}' /></td>";
    echo "<td><input type='text' size='5' name='fld_quiz4[$i]' value='{$students['fld_quiz4']}' /></td>";
    echo "<td><input type='text' size='5' name='fld_quiz5[$i]' value='{$students['fld_quiz5']}' /></td>";
    echo '<td>'.$total.'</td>';// Show it here
    echo '</tr>';

    ++$i;
}

答案 1 :(得分:2)

$total将对所有测验进行总和,例如1到5,并将该变量打印到上次td,如下所示

while ($students = mysql_fetch_array($result)) {

    $total = ($students['fld_quiz1'] + $students['fld_quiz2'] + $students['fld_quiz3'] + $students['fld_quiz4'] + $students['fld_quiz5']);

    //if marks are from 100 then
    $avg = ($total * 100) / 500;

    echo '<tr>';
    echo "<td>{$students['id']}<input type='hidden' name='id[$i]' value='{$students['id']}' /></td>";
    echo "<td>{$students['fld_name']}</td>";
    echo "<td><input type='text' size='5' name='fld_quiz1[$i]' value='{$students['fld_quiz1']}' /></td>";
    echo "<td><input type='text' size='5' name='fld_quiz2[$i]' value='{$students['fld_quiz2']}' /></td>";
    echo "<td><input type='text' size='5' name='fld_quiz3[$i]' value='{$students['fld_quiz3']}' /></td>";
    echo "<td><input type='text' size='5' name='fld_quiz4[$i]' value='{$students['fld_quiz4']}' /></td>";
    echo "<td><input type='text' size='5' name='fld_quiz5[$i]' value='{$students['fld_quiz5']}' /></td>";
    echo '<td> ' .$total. ' </td>';
    echo '</tr>';

    ++$i;
}