首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >while循环中特定ID所在的分组结果

while循环中特定ID所在的分组结果
EN

Stack Overflow用户
提问于 2011-06-07 03:17:39
回答 1查看 93关注 0票数 0
代码语言:javascript
复制
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 Lang.谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-07 03:25:15

假设您的结果是按inputid排序的

代码语言:javascript
复制
$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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6256896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档