Group results where specific ID in a while-loop
Table with data:
id score inputid
1 1 1
2 4 1
3 3 1
4 1 2
5 2 开发者_如何学JAVA 2
6 3 5
7 1 6
8 1 6
while ($stmt_score->fetch())
{
if($bind_inputid == '1') $array['score'][0] += $bind_score;
if($bind_inputid == '2') $array['score'][1] += $bind_score;
if($bind_inputid == '5') $array['score'][2] += $bind_score;
if($bind_inputid == '6') $array['score'][3] += $bind_score;
}
As stated above I want to sum all results with specific ID. But since there will be more $bind_inputid ID's, my question is, how to make automatic statements for more results of an Id's ?
It has to be done in while loop not with mysql select. PHP Lang. Thx.
Assume your result is sorted by inputid
:
$inputid_curr = -1;
$score_array_index = -1;
while ($stmt_score->fetch())
{
if($inputid_curr != $bind_inputid)
{
$score_array_index = $score_array_index + 1;
$inputid_curr = $bind_inputid;
}
$array['score'][$score_array_index] += $bind_score;
}
精彩评论