简体   繁体   English

嵌套while循环问题,第二个循环仅循环一次并停止

[英]Nested while loop issue, second one loops only once and stops

I have coding issue that I am unable to resolve myself so need your help. 我的编码问题无法解决,因此需要您的帮助。

I have two queries fetching data from the same table: 我有两个查询从同一张表中获取数据:

$rs_main_model gets all unique models using GROUP BY $rs_main_model使用GROUP BY获取所有唯一模型

$rs_variant gets all models with their respective options, ie all columns from the table. $rs_variant获取所有具有各自选项的模型,即表中的所有列。

What I am doing is to run a while loop to list all unique models - $rs_main_model and then inside the same div run second loop to list all matching models from $rs_variant : 我正在做的是运行一个while循环以列出所有唯一模型- $rs_main_model ,然后在同一div运行第二个循环以列出$rs_variant中所有匹配的模型:

<?php while (!$rs_main_model->EOF)   { ?>                   
        <div>               
            <div>        
                <h2<?php echo $rs_main_model->fields['model']; ?></h2>
            </div>                                    
            <div>      
                      <?php while (!$rs_variant->EOF) { ?>           
                      <?php echo $rs_variant->fields['variant']; ?>
                      <?php $rs_variant->MoveNext(); } ?>
                </div>
        </div>
  <?php $rs_main_model->MoveNext(); } ?>

Below is an example of the actual and desired output: 以下是实际和所需输出的示例:

The output should look something like this: |  However what I end up with is this:    

    Model A    Model A - Variant1           |  Model A    Model A - Variant1   
               Model A - Variant2           |             Model A - Variant2  
               Model A - Variant3           |             Model A - Variant3  
    Model B    Model B - Variant1           |  Model B                    
    Model C    Model C - Variant1           |  Model C          
               Model C - Variant2           |

What changes should I make to the while loops to make this work? 为了使这项工作有效,我应该对while循环进行哪些更改? Thank you! 谢谢!

You should update the $rs_variant results on every iteration inside the outer loop by using as condition the $rs_main_model->fields['model']. 您应该使用$ rs_main_model-> fields ['model']作为条件,在外循环内的每次迭代中更新$ rs_variant结果。 Try the following 尝试以下

<?php while (!$rs_main_model->EOF)   { ?>                   
    <div>               
        <div>        
            <h2<?php echo $rs_main_model->fields['model']; ?></h2>
   <?php $sql_variant = "SELECT * FROM tbl_catalog WHERE model_body = '".$rs_main_model->fields['model_body']."'"; $rs_variant = $db -> Execute($sql_variant); ?>
        </div>                                    
        <div>      
                  <?php while (!$rs_variant->EOF) { ?>           
                  <?php echo $rs_variant->fields['variant']; ?>
                  <?php $rs_variant->MoveNext(); } ?>
            </div>
    </div>
<?php $rs_main_model->MoveNext(); } ?>

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

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