简体   繁体   English

PHP双循环,第二循环不多次迭代

[英]PHP double while loop, second loop not iterating more than once

so I'm working on a "product listing", and the idea is to have the name of the product and then a drop down list of colours derived from the database. 所以我正在开发一个“产品列表”,其想法是获得产品的名称,然后是从数据库派生的颜色下拉列表。 The problem is I can only get the "colours" while loop to iterate the once, I get the first product name and the colours in the drop down menu the once but not subsequently, the help I'd like is, how can I change this code to make it do what I need, I've been trying for a week and I could really use some help. 问题是我只能得到“颜色”而循环迭代一次,我得到第一个产品名称和下拉菜单中的颜色一次但不是随后,我想要的帮助,我怎么能改变这段代码让它做我需要的,我已经尝试了一个星期,我真的可以使用一些帮助。

    $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.


        }
    }
}

If anyone can help I would appreciate it. 如果有人可以提供帮助,我会很感激。 From Grant M. 来自Grant M.

The way mysql_fetch_assoc() works is that it has an internal pointer, and every time you run the method, you get the next item and the pointer is moved one time. mysql_fetch_assoc()工作方式是它有一个内部指针,每次运行该方法时,都会得到下一个项目并且指针被移动一次。 If there was no pointer, how would the while loop otherwise ever terminate? 如果没有指针,那么while循环将如何终止? It would just keep pulling out colours, over and over. 它会一遍又一遍地拉出颜色。

The way to resolve this is to reset the point in each iteration. 解决此问题的方法是重置每次迭代中的点。 This can be done using mysql_data_seek() . 这可以使用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);
}

@Kristian Antonsen is quite right - once you've read the rows once you are at 'the end' unless you rewind to the beginning of the result set. @Kristian Antonsen是非常正确的 - 一旦你在'结束'时读过行,除非你回到结果集的开头。

Alternatively to the other answers posted, because the colours aren't dependent on the product - get them once and then reuse them in memory. 除了发布的其他答案之外,因为颜色不依赖于产品 - 获取它们一次然后在内存中重复使用它们。

// 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";
        }
    }
}

The problem is that the second time around, the pointer is already at the end of the record for $colour_query ; 问题是第二次,指针已经在$colour_query记录的$colour_query ; it will continue to return null because it doesn't know that the nested loop is complete. 它将继续返回null因为它不知道嵌套循环是否完整。

You can solve this problem by resetting the pointer using mysql_data_seek : 您可以通过使用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);
    }
}

Also, your <br \\> is wrong; 而且,你的<br \\>错了; it should be <br /> . 它应该是<br />

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

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