title: Nested while loop is not working with my GROUP BY queries
body: <p>I am a bit stuck with a <code>while</code> loop and need some assistance to find the wrong code.</p>
  
  <p>My SQL table looks something like this:</p>
  
  <pre><code>make     model       model_body     variant   
  M1       A           ooo            Va1    
  M1       B           sss            Va2    
  M1       B           sss            Va3    
  M1       A           ooo            Va4
  M1       A           ooo            Va5    
  M1       B           jjj            Va6   
  M1       A           www            Va7
  </code></pre>
  
  <p>I would like to output the table contents in HTML first by doing <code>GROUP BY</code> for <code>model_body</code> values and list only the unique ones with a loop and then with a second loop list all the available <code>variant</code> values for each uqniue <code>model_body</code>. </p>
  
  <pre><code>The output should look something like this:   However what I end up with is this:    
  
  ooo    Va1                                    ooo    Va1
         Va4                                           Va4
         Va5                                           Va5
  www    Va7                                    www              
  sss    Va2                                    sss                
         Va3
  jjj    Va6                                    jjj              
  </code></pre>
  
  <p>The PHP code I wrote is this one, I tried several different possibilities but always end up with some kind of mismatch:</p>
  
  <pre><code>&lt;?php while (!$rs_main_model-&gt;EOF)   { ?&gt;                   
          &lt;div&gt;               
              &lt;div&gt;        
                  &lt;h2&lt;?php echo $rs_main_model-&gt;fields['model']; ?&gt;&lt;/h2&gt;
              &lt;/div&gt;                                    
              &lt;div&gt;      
                        &lt;?php while (!$rs_variant-&gt;EOF) { ?&gt;                                  
                        &lt;?php echo $rs_variant-&gt;fields['variant']; ?&gt;
                        &lt;?php $rs_variant-&gt;MoveNext(); } ?&gt;
                  &lt;/div&gt;
          &lt;/div&gt;
    &lt;?php $rs_main_model-&gt;MoveNext(); } ?&gt;
  </code></pre>
  
  <p>It is important to say that if I put some text instead of the second loop, the text appears against every <code>model_body</code> but when the loop is done the <code>variant</code> values appear only for the first <code>model_body</code> and the rest are blank.</p>
  
  <p>My SQL queries are listed below:</p>
  
  <pre><code>$catalogue_make =  $_GET['m'];
  
  $sql_all_models =  "SELECT * FROM tbl_catalog WHERE make = '$catalogue_make'"; 
  $rs_all_models  =  $db -&gt; Execute($sql_all_models);    
  
  $sql_main_model     = "SELECT * FROM tbl_catalog 
                          WHERE make = '".$rs_all_models-&gt;fields['make']."' 
                          GROUP BY model_body";
  $rs_main_model  =  $db -&gt; Execute($sql_main_model);    
  
  $sql_variant        = "SELECT * FROM tbl_catalog 
                          WHERE model_body = '".$rs_main_model-&gt;fields['model_body']."'";
  $rs_variant     =  $db -&gt; Execute($sql_variant);
  </code></pre>
  
  <p>Here is an SQL fiddle of the <code>GROUP BY</code> query if this servers for anything:
  <a href="http://sqlfiddle.com/#!2/96044/3" rel="nofollow">http://sqlfiddle.com/#!2/96044/3</a></p>
  
  <p>I am not sure this should be achieved with a second loop so any suggestions how to make this work will be appreciated.</p>
  
  <p><strong>EDIT</strong>
  This throws some light on the problem but still cannot get it to work in my code:
  <a href="http://board.phpbuilder.com/showthread.php?10373392-RESOLVED-Nested-While-Loops-and-mySQL" rel="nofollow">http://board.phpbuilder.com/showthread.php?10373392-RESOLVED-Nested-While-Loops-and-mySQL</a>
  Thank you!</p>