just go through this it may help u
<?php
$number_of_thumbs_in_row = 4;
$result = mysql_query( "SELECT photo_id,photo_caption,photo_filename,photo_category FROM gallery_photos");
while( $row = mysql_fetch_array( $result ) )
{
$result_array[] = "<img src='".$images_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."'/><br>$row[1]<br>$row[3]<br>$row[0]";
}
mysql_free_result( $result );
$result_final = "<tr valign='top' align='center' class='style1'>\n";
foreach($result_array as $thumbnail_link)
{
if($counter == $number_of_thumbs_in_row)
{
$counter = 1;
$result_final .= "\n</tr align='center' class='style1'>\n<tr align='center' class='style1'>\n";
}
else
$counter++;
$result_final .= "\n<td class='style1'>".$thumbnail_link."</td>\n";
}
if($counter)
{
if($number_of_photos_in_row==$counter)
$result_final .= "\n<td class='style1' colspan='".($number_of_photos_in_row=$counter)."'></td>\n";
$result_final .= "</tr>";
}
}
echo <<<__HTML_END
<html>
<head>
<title>Gallery View</title>
</head>
<body>
<table width='100%' border='0' cellpadding="10" cellspacing="10">
$result_final
</table>
</body>
</html>
__HTML_END;
?>