SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot ameerulislam's Avatar
    Join Date
    Jul 2011
    Posts
    127
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Problem with Nested loop.. data coming from database..

    Having a loop problem.. I'm creating a nested loop to show images from my database 2/ row. With this code I was able to generate 2 images per row but the same image is appearing twice then the next image is going to the next line and showing it twice again.

    PHP Code:
    <!-- Table -->                        <div class="table">                                                    <div style="margin-left:50px;" id="images">                            <table width='100%' border='0' cellspacing='0' cellpadding='0'>                                                    <?php                                while($rows=mysql_fetch_array($result))                                {                                $link=$rows[company_logo];                                    $count=0;                                    echo "<tr>";                                                                        while($count<=1){                                                                                echo "<td><img src=\"upload/$link\" width=\"100px\" height=\"100px\"/></td>";                                            $count++;                                        }                                                                                                                        echo "</tr>";                                                                        }                                                                    ?>                                                                            </table>                                                            </div>
    I know what's wrong but don't know how to solve it.

    Thanks!

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2011
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do to rewrite the code in a more structured way, but a simple fix would be to add an extra database fetch inside the second loop like this:

    if ( $count==1 )
    {
    $rows=mysql_fetch_array($result))
    $link=$rows[company_logo];
    }
    But really you want to get rid of that inner while and alter the code to echo something different for odd and even images.

  3. #3
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,079
    Mentioned
    53 Post(s)
    Tagged
    0 Thread(s)
    I use for each:
    Code PHP:
    <div class="table">
    <div style="margin-left:50px;" id="images">
     
    <table width='100%' border='0' cellspacing='0' cellpadding='0'>
    <?php 
    $rows=mysql_fetch_array($result);
    echo "<tr>";
    for each $rows as $row { 
    echo '<td><img src="\upload\$row['company_logo']" width="100px" height="100px"/></td>' . "\n";
    }                                                                                                                        
    echo "</tr>";
    } ?>
    </table>
    </div>
    </div>

  4. #4
    SitePoint Zealot ameerulislam's Avatar
    Join Date
    Jul 2011
    Posts
    127
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys I actually solved it with an if statement.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •