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.

<!-- 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!

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.

I use for each:


<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>' . "\
";
}                                                                                                                        
echo "</tr>";
} ?>
</table>
</div>
</div>

Thanks guys I actually solved it with an if statement.