PHP double while 循环,第二个循环不重复多次 [英] PHP double while loop, second loop not iterating more than once

查看:50
本文介绍了PHP double while 循环,第二个循环不重复多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.


        }
    }
}

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

If anyone can help I would appreciate it. From Grant M.

推荐答案

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

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. If there was no pointer, how would the while loop otherwise ever terminate? It would just keep pulling out colours, over and over.

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

The way to resolve this is to reset the point in each iteration. This can be done using 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);
}

这篇关于PHP double while 循环,第二个循环不重复多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆