简体   繁体   English

我如何控制while循环到另一个while循环

[英]how can i controll while loop into another while loop

Suppose I have a while loop like: 假设我有一个while循环,例如:

$sql = mysql_query("SELECT * FROM tablename");
while($row = mysql_fetch_array($sql)){

  $id = $row["id"];
  $sql_2 = mysql_query("SELECT * FROM secondtable WHERE id != $id ");
  while($ro = mysql_fetch_array($sql_2)){
   $id2 = $ro["id2"];
   echo $id2;
   }

}

then if first query return 5 results ie 1-5 and second query returns 3 results than if i want to echo out second query it gives me like this.......... 然后,如果第一个查询返回5个结果,即1-5,第二个查询返回3个结果,那么如果我想回显第二个查询,它会给我这样的..........

111112222233333 111112222233333

than how can i fix to 123 so that the second while loop should execute according to number of times allowed by me........!! 比我怎么能固定到123,以便第二个while循环应根据我允许的次数执行........ !! how can i do that.........!! 我怎样才能做到这一点.........!!

I'm not sure I 100% understand your question - it's a little unclear. 我不确定我是否100%理解您的问题-尚不清楚。

It's possible you could solve this in the query with a GROUP BY clause 您可以使用GROUP BY子句在查询中解决此问题

$sql_2 = mysql_query("SELECT id FROM secondtable WHERE id != $id GROUP BY id");

But that would only work if you need just secondtable.id and not any of the other columns. 但是,如果你需要的仅仅是只会工作secondtable.id ,而不是其他任何列的。

When you say "number of time allowed by me" do you mean some sort of arbitrary value? 当您说“我允许的时间”时,您是说某种任意值吗? If so, then you need to use a different loop mechanism, such as Greg B's solution. 如果是这样,则您需要使用其他循环机制,例如Greg B的解决方案。

$sql = mysql_query("SELECT * FROM tablename");
$tmp = array();
while($row = mysql_fetch_array($sql)){

    $id = $row["id"];
    if(!in_array($id, $tmp)) {  
        $sql_2 = mysql_query("SELECT * FROM secondtable WHERE id != $id ");
        while($ro = mysql_fetch_array($sql_2)){
            $id2 = $ro["id2"];
            echo $id2;
        }
        $tmp[] = $id;
    }
}

Saving all queried $id's in an array to check on the next iteration if it has already been queried. 将所有查询的$ id保存在数组中,以检查是否已查询下一次迭代。 I also think that GROUPing the first query result would be a better way. 我还认为,将第一个查询结果分组是一种更好的方法。

Do you want to explicitly limit the number of iterations of the inner loop? 您是否要明确限制内部循环的迭代次数?

Have you considered using a for loop? 您是否考虑过使用for循环?

$sql = mysql_query("SELECT * FROM tablename");

while($row = mysql_fetch_array($sql)){
    $id = $row["id"];
    $sql_2 = mysql_query("SELECT * FROM secondtable WHERE id != $id ");

    for($i=0; $i<3; $i++){
       $ro = mysql_fetch_array($sql_2);
       $id2 = $ro["id2"];
       echo $id2;
    }
}

Your first while loop is iterating over all 5 results, one at a time. 您的第一个while循环将一次遍历所有5个结果。

Your second while loop is iterating over each of the 5 results, producing it's own set of results (ie 3 results for each of the 5 iterations, totaling 15 results). 您的第二个while循环将对5个结果中的每个进行迭代,产生它自己的结果集(即5个迭代中的每个3个结果,总共15个结果)。

I believe what you are trying to do is exclude all IDs found in your first loop from your second query. 我相信您要尝试的是从第二个查询中排除在第一个循环中找到的所有ID。 You could do that as follows: 您可以按照以下步骤进行操作:

$sql = mysql_query("SELECT * FROM tablename");
$exclude = array();
while($row = mysql_fetch_array($sql)) {
    array_push($exclude, $row['id']);
}

// simplify query if no results found
$where = '';
if (!empty($exclude)) {
    $where = sprintf(' WHERE id NOT IN (%s)', implode(',', $exclude));
}

$sql = sprintf('SELECT * FROM secondtable%s', $where);
while($row = mysql_fetch_array($sql_2)) {
   $id2 = $row["id2"];
   echo $id2;
}

I agree with Leonardo Herrera that it's really not clear what you're trying to ask here. 我同意莱昂纳多·埃雷拉(Leonardo Herrera)的观点,实际上您不清楚要问的是什么。 It would help if you could rewrite your question. 如果您可以重写您的问题,将有所帮助。 It sounds a bit like you're trying to query one table and not include id's found in another table. 听起来好像您正在尝试查询一个表,但不包括在另一个表中找到的ID。 You might try something like: 您可以尝试以下方法:

SELECT * FROM secondtable t2
WHERE NOT EXISTS (SELECT 1 FROM tablename t1 WHERE t1.id = t2.id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM