2011-06-03 57 views
0
... 
$count=1; 
while ($row = mysql_fetch_array($result)){ 
    $result1 = mysql_query("SELECT something from something where name like %$row['name']% "); 
    while ($data = mysql_fetch_array($result1)){ 
     echo $data[title]; 
     echo $count; 
    } 
    $count++ 
} 

我該如何在foreach中增加一個數字?第一個foreach結果爲第二個查詢。然後計算2ed foreach的所有結果。我的代碼引起如何在2 foreach中增加數字?

title1 1 
title2 1 
title3 2 
title4 2 
title5 2 
title6 3 

如何使

title1 1 
title2 2 
title3 3 
title4 4 
title5 5 
title6 6 

感謝。

回答

3
while ($data = mysql_fetch_array($result1)){ 
    echo $data[title]; 
    echo $count; 
    $count++; 
    } 

基本上把$count++內嵌套的同時

1

如果您不想要重置$計數每$行,代碼必須是這樣的。

$count=1; 
while ($row = mysql_fetch_array($result)){ 
    $result1 = mysql_query("SELECT something from something where name like %$row['name']% "); 
    while ($data = mysql_fetch_array($result1)){ 
     echo $data[title]; 
     echo $count++; 
    } 
} 

否則;

while ($row = mysql_fetch_array($result)){ 
    $count=1; 
    $result1 = mysql_query("SELECT something from something where name like %$row['name']% "); 
    while ($data = mysql_fetch_array($result1)){ 
     echo $data[title]; 
     echo $count++; 
    } 
}