【问题标题】:PHP double while loop, second loop not iterating more than oncePHP双while循环,第二个循环不重复多次
【发布时间】:2012-04-01 13:47:41
【问题描述】:

所以我正在制作“产品列表”,其想法是拥有产品名称,然后是从数据库中派生的颜色下拉列表。问题是我只能获得“颜色”while循环来迭代一次,我获得第一个产品名称和下拉菜单中的颜色一次但不是随后,我想要的帮助是,我该如何改变这段代码让它做我需要的,我已经尝试了一个星期,我真的可以使用一些帮助。

    $product_query = mysql_query('SELECT * FROM products WHERE id > 0');
$colour_query = mysql_query('SELECT * FROM product_colours WHERE id > 0');


while ($get_product_rows = mysql_fetch_assoc($product_query)){
echo $get_product_rows['name'];
    echo "<br \>";

    if ($get_product_rows['has_colour'] == '1'){
        while ($get_colour_row = mysql_fetch_assoc($colour_query)){
            // Drop down box population goes here.


        }
    }
}

如果有人可以提供帮助,我将不胜感激。 来自格兰特 M。

【问题讨论】:

  • 我不确定这一点,也没有时间测试,但我认为尝试同时运行两个这样的 mysql 查询是不可预测的。相反,您可以执行 while 循环,一次将查询转换为数组,然后执行嵌套的 foreach 循环。
  • 你能做到var_dump(mysql_fetch_assoc($color_query)) 并用输出更新你的问题吗?
  • @kingcoyote 为什么会不可预测?他在两个不同的表上有两个不同的查询,而且他在 while 循环之前执行他的查询。
  • @steveoh 这是一段模糊的记忆,但我记得指针相互混淆,其中嵌套的 mysql_fetch_assoc 与外部的冲突。所以不是去 1A、1B、1C、2A、2B、2C,而是去 1A、2B、3C 或类似的东西。
  • 颜色是否因产品而异?如果是这样,将products 连接到product_colours 可能会更有效(取决于许多因素,例如数据库链接延迟、每个结果集中的行数......)

标签: php mysql while-loop


【解决方案1】:

@Kristian Antonsen 是非常正确的 - 一旦你在“结束”处阅读了行,除非你倒退到结果集的开头。

替代发布的其他答案,因为颜色不依赖于产品 - 获取它们一次,然后在内存中重复使用它们。

// get the colours once
$colour_query = mysql_query('SELECT * FROM product_colours WHERE id > 0');
$colours = array();
while ($get_colour_row = mysql_fetch_assoc($colour_query)) {
  array_push($colours, $get_colour_row['colour']);
}

// loop through each product
$product_query = mysql_query('SELECT * FROM products WHERE id > 0');
while ($get_product_rows = mysql_fetch_assoc($product_query)) {
    echo $get_product_rows['name'];
    echo "<br />\n";

    if ($get_product_rows['has_colour'] == '1'){
        foreach ($colours as $colour) {
            echo $colour . "<br />\n";
        }
    }
}

【讨论】:

    【解决方案2】:

    问题是第二次,指针已经在$colour_query的记录末尾;它将继续返回null,因为它不知道嵌套循环是否完整。

    你可以通过使用mysql_data_seek重置指针来解决这个问题:

    $product_query = mysql_query('SELECT * FROM products WHERE id > 0');
    $colour_query = mysql_query('SELECT * FROM product_colours WHERE id > 0');
    
    while ($get_product_rows = mysql_fetch_assoc($product_query)) {
        echo $get_product_rows['name'];
        echo "<br />";
    
        if ($get_product_rows['has_colour'] == '1') {
            while ($get_colour_row = mysql_fetch_assoc($colour_query)) {
                // Drop down box population goes here.
            }
    
            // Reset the pointer:
            mysql_data_seek($colour_query, 0);
        }
    }
    

    另外,您的&lt;br \&gt; 是错误的;应该是&lt;br /&gt;。

    【讨论】:

      【解决方案3】:

      mysql_fetch_assoc() 的工作方式是它有一个内部指针,每次运行该方法时,都会得到下一项并且指针移动一次。如果没有指针,while 循环将如何终止?它只会一遍又一遍地拉出颜色。

      解决此问题的方法是在每次迭代中重置点。这可以使用mysql_data_seek() 来完成。

      $product_query = mysql_query('SELECT * FROM products WHERE id > 0');
      $colour_query = mysql_query('SELECT * FROM product_colours WHERE id > 0');
      
      while ($get_product_rows = mysql_fetch_assoc($product_query)) {
          echo $get_product_rows['name'];
          echo "<br />";
      
          if ($get_product_rows['has_colour'] == '1') {
              while ($get_colour_row = mysql_fetch_assoc($colour_query)) {
                  // Drop down box population goes here. 
              }
          }
          mysql_data_seek($colour_query, 0);
      }
      

      【讨论】:

      • 我尝试了你的添加,我最终得到了第二个 while 循环迭代了很多次,似乎已经根据查询返回的行数进行了迭代,但是,在循环对此进行了排序。非常感谢您的帮助。
      • @GrantMackenzie 不客气。如果这解决了您的问题,请记住通过单击左侧的 V 将其标记为答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多