loop won't display all images

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
bluesman333
Forum Commoner
Posts: 52
Joined: Wed Dec 31, 2003 9:47 am

loop won't display all images

Post by bluesman333 »

The following code displays a table of images, but for some reason will not display every image. Also, it prints one empty cell at the end.

Can anyone tell me why?

Code: Select all

<?php
$result = mysql_query( "SELECT * FROM images" );
$num_rows = mysql_num_rows($result);
print $num_rows;
?>
<table border=1 width=100% height=100%>
<tr>
<?php
for ($count=0; $count <= $num_rows; $count++) {
	$a_row = mysql_fetch_array($result);
		if (($count % 4) == 0 ) {
			print "</tr><tr>\n";
        	}else{
		print "\t<td><img src = "/images/thumbnails/$a_row[imagename]"></td>\n";
}	
}
?>
</table>
<?php mysql_close( $link ); ?>
[Edit: Added php tags for eyecandy --JAM]
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

I had to rewrite it abit to test, so...
But generally, I changed the way to loop it. Play around with the below so it again fits your needs.

Worked for me...

Code: Select all

<?php
$result = db( "SELECT * FROM images" );
$num_rows = mysql_num_rows($result);
print $num_rows; // I got 11
?>
<table border=1>
<tr>
<?php
$count = 0;
while ($r = mysql_fetch_array($result)) {
    $count++;
    echo '<td>'.$r['value'].'</td>';
    if (($count % 4) == 0 ) { echo "</tr><tr>\n"; }
}
?>
</table>
Result:
Image
Post Reply