2011-06-06 127 views
0
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循环中特定ID的组结果

它必须在while循环中完成,而不是使用mysql select。 PHP Lang。 Thx。

+2

如何至少提您所使用的编程语言? – 2011-06-06 19:19:33

+0

sry,; p,它的php – Cameleon 2011-06-06 19:21:21

回答

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; 
} 
+0

哈哈哈,太聪明了,thx托马斯 – Cameleon 2011-06-06 19:58:27

+0

@Cameleon - 创建更多变量(使用更多空间)常常解决很多问题。 – 2011-06-06 20:02:21