如何使2个foreach联合增加数量?

时间:2011-06-03 22:36:18

标签: php foreach

...
$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结果进行第二次查询。然后计算来自2ed 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

感谢。

2 个答案:

答案 0 :(得分:3)

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

基本上将$count++放在嵌套的

答案 1 :(得分:1)

如果您不想为每个$行重置$ count,则代码必须如此。

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