将结果分组到while循环中的特定ID

时间:2011-06-06 19:17:39

标签: php while-loop statements

Table with data:

id   score   inputid
1      1        1
2      4        1
3      3        1
4      1        2
5      2        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;
}

如上所述,我希望将所有结果与特定ID相加。但是因为会有更多的$ bind_inputid ID,我的问题是,如何为Id的更多结果制作自动语句?

必须在while循环中完成,而不是使用mysql select。 PHP郎。 THX。

1 个答案:

答案 0 :(得分:1)

假设您的结果按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;
}
相关问题