Nested WHILE loops?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kostas Xenos
    New Member
    • Jul 2011
    • 1

    Nested WHILE loops?

    Hello guys,

    I'll try and explain in full.

    I have 2 tables in my database ( extras, extraphotos ).

    Table extras is holding the title, descr and extraphotos is holding the picname. Both of the have an extra row with the name of fname so i can bind these two together.

    What I'm trying to do is to read from my DB the Title, Description and all the picname for a gallery that i'm trying to create.

    Basically it is set up with the Cycle js plugin so I have 1 thumbnail that has extra photos ( picname ).

    I'm struggling to get arround on this ->

    Code:
    <?php
    
    $query="SELECT * FROM  extras";
    $result=mysql_query($query);
    $num=mysql_numrows($result);
    $i=0;
    $j=0;
    
      while ($i < $num) {
    
        $descr=mysql_result($result,$i,"descr");
        $title=mysql_result($result,$i,"title");
        $fname=mysql_result($result,$i,"fname");                         
        echo '<div class="image" title="'.$title.'">';
        echo '<img src="http://bytes.com/images/work/th/kolie1.jpg" alt="" />';
        echo '<div class="image-label"><span>'.$title.'</span></div>';
        echo '</div>';
        echo '<div class="cycle">';
    
    $query2="SELECT * FROM extraphotos WHERE fname = " .$fname;
    $result2=mysql_query($query2);
    $num2=mysqlnumrows($result2);
        
    while ($j < $num2) {
        $picname=mysql_result($result2,$j,"picname");    
        echo '<img src="http://bytes.com/images/work/"'.$picname.'.jpg" width="710px" height="460px" />';
        $j++;
    }  
        echo'</div>';
        echo '<div class="info">';
        echo '<div class="title"><h2>'.$title.'.</h2></div> ';
        echo '<div class="descr">'.$descr.'</div>';
        echo '<div class="item_url">'.$fname.'</div>';
        echo '</div>';
        echo '</div>';
        $i++;
    }
    
    ?>
    So basically my first 'while loop' is creating the divs for the thumbnails, and i need a second 'while loop' so it can create the extra photo divs inside the main one.

    Any suggestions? I started coding like 2 weeks ago and I thought it would be easier. ^_^

    Thanks in advance,
    Xenos K.
    Last edited by Niheel; Jul 28 '11, 09:25 AM.
  • Chris T
    New Member
    • Jul 2011
    • 5

    #2
    I think you don't need that 2nd while loop. Just use SELECT for your photos table with WHERE condition pointing to proper photo. Try to remove while loop,but leave the code inside it.

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      If fname is a string, then you need to delimit it as such in your query using single quotes.

      Comment

      Working...