this loop inside another is driving me nuts!

Discussion in 'PHP' started by x0x, Jan 22, 2010.

  1. #1
    this is part of a table - and it works

     <tr align="center">
      
      
      <?php // first loop
       $get = $DB->query("SELECT * FROM $t[fam] WHERE uid = '$uid' ORDER BY id ASC limit ".($rows*3).", 3", __FILE__, __LINE__);
         while ($results = $DB->fetch_array($get)){
    	 ?>
        <td align="center" width="33%">
        
    <?=$results['name']?>
    
      </td>
       <? }?> 
        
      </tr>
    PHP:
    but now when I add another loop inside this one:

    <tr align="center">
      
      
      <?php // first loop
       $get = $DB->query("SELECT * FROM $t[fam] WHERE uid = '$uid' ORDER BY id ASC limit ".($rows*3).", 3", __FILE__, __LINE__);
         while ($results = $DB->fetch_array($get)){
    	 ?>
        <td align="center" width="33%">
        
    <?=$results['name']?>
    
     <?php // second loop
      $get = $DB->query("SELECT * FROM $t[mob] WHERE fam = '$results[id]' ORDER BY networth ASC", __FILE__, __LINE__);
         while ($memb = $DB->fetch_array($get)){
    		 ?>
    		
    		<br><?=moblink($memb['mob'],$rnd)?>      <? }?>
          
    
      </td>
        <? }?>
      </tr>
    PHP:
    It does not work properly. I need the list of "<br><?=moblink($memb['mob'],$rnd)?>" to be under the text the first loop creates.


    Results with first loop only:


    Text1 Text2 Text3

    (columns in a row)


    However, when I enter the second loop I expect this:

    Text1 Text2 Text3
    bla1 bla2 bla3
    bla1 bla2 bla3
    bla1 bla2 bla3
    bla1 bla2 bla3
    bla1 bla2 bla3


    Instead I get:

    Text1
    bla1
    bla1
    bla1
    bla1
    bla1


    What am I doing wrong?


    This all is in another loop btw that creates more rows in the table in case there are more results than three (three per row). But I didn't think that's relevant.
     
    x0x, Jan 22, 2010 IP
  2. ibg

    ibg Member

    Messages:
    25
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    38
    #2
    You're using the same $get result for both queries which leads to the loop ? Try using a different value?
     
    ibg, Jan 22, 2010 IP
  3. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #3
    I will create a function for the second loop and make the list outside the big loop... Annoying.

    I would still like to know why my code did not work.

    EDIT: to ibg: trying...
     
    x0x, Jan 22, 2010 IP
  4. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #4
    That worked. Duh! Thanks!
     
    x0x, Jan 22, 2010 IP