Hi Guys.
Still learning PHP and I have been trying for the last few hours to display thumbnails from a database unsuccessfully. I would like them to be in the format:

image1 image2 image3 image4 image5
image6 image7 image8 image9 etc.

regardless of the number of rows returned.

My sql query to retrieve the thumbs is:

<?php require_once('Connections/connections.php'); ?>
<?php
//query user photo thumbs
$user_id = $_SESSION['UserSession'];
$query_pictures = "SELECT picture_id, picture_thumb_url FROM picture WHERE user_id='$user_id'";
$pictures_result = mysql_query($query_pictures) or die(mysql_error());	
$pictures = mysql_fetch_assoc($pictures_result);
$rows_pictures = mysql_num_rows($pictures_result);
?>

The thumbs are stored in a folder called User_Pictures. I have been able to retrieve them for example in the profile.php page as follows:

<?php do { ?>                                      
<table width="460" border="0">                     
<tr>
  <td height="50" width="50"><a href="profile.php">
  <img src="User_Pictures/<?php echo $avatar_thumb['picture_thumb_url']; ?>" 
  height="50" width="50" border="0"/></a></td>
  <td width="360" colspan="3" class="overide" id="update_box"><div>
  <?php echo $row_wid_updates['update_text']; ?></div></td>
  <td width="50"><a href="pictures.php">
  <img src="User_Pictures/<?php echo $row_wid_updates['picture_thumb_url']; ?>" 
  height="50" width="50" border="0"/></a></td>
</tr>  
</table> 
<?php } while ($row_wid_updates = mysql_fetch_assoc($wid_updates)); ?>

I have tried the following code but have been unsuccessful

<?php
echo "\n<table>";
$i = 5;
while ($pictures = mysql_fetch_assoc($pictures_result))
{
if($i==5) echo "\n\t<tr>";
echo "\n\t\t<td><img src=\"".$pictures['picture_thumb_url']."\" /></td>";
$i--;
if($i==0) {
echo "\n\t<tr>";
$i = 5;
}
}
if($i!=5) echo "\n\t\t<td colspan=\"$i\"></td>\n\t</tr>";
echo "\n</table>";
?>

I would greatly appreciate any help out there.

Member Avatar for brewbuff

This test worked for me.

<?php
$j=0;
$i = 5;
$pictures=array();
$pictures = "images/r.png";
echo "<table>";
while ($j<17){
if($i==5) echo "<tr>";
echo "<td><img src='$pictures[picture_thumb_url]' /></td>\n";
$i--;
$j++;
if($i==0) {
echo "<tr>\n";
$i = 5;
}
}
echo "</table>";
?>

Hello Brewbuff,
I tried your code but somehow it did not work for me. Will yours only display .png images?
I tried the following code but now it shows all images except the first one. Please advise.

<?php
echo "\n<table>";
$i = 5;
while ($pictures = mysql_fetch_assoc($pictures_result))
{
if($i==5) echo "\n\t<tr>";
echo "<td><img src='User_Pictures/$pictures[picture_thumb_url]' /></td>\n";
$i--;
if($i==0) {
echo "\n\t<tr>";
$i = 5;
}
}
if($i!=5) echo "\n\t\t<td colspan=\"$i\"></td>\n\t</tr>";
echo "\n</table>";
?>
echo '<table>';
$i = 0;
while ($pictures = mysql_fetch_assoc($pictures_result))
{
  if ($i % 5 == 0) 
    echo '<tr>';

  echo "<td><img src='User_Pictures/$pictures[picture_thumb_url]' /></td>";
  if ($i % 5 == 4)
    echo '</tr>';

  $i++;
}

if ($i % 5 > 0) 
{
  $i = 5 - ($i % 5);
  echo "<td colspan='$i'>&nbsp;</td></tr>";
}
echo '</table>';

I really appreciate the help I'm getting. Thank you too Pritaeas. I follow your arguments with the modulus. However, The 1st image is still not loading.

THE TABLE("picture"):

picture_id picture_url picture_thumb_url user_id
24 ../User_Pictures/90.png ../User_Pictures/Thumbs/90.png 92
25 ../User_Pictures/811.jpg ../User_Pictures/Thumbs/811.jpg 92
26 ../User_Pictures/641.jpg ../User_Pictures/Thumbs/641.jpg 95
28 ../User_Pictures/584.jpg ../User_Pictures/Thumbs/584.jpg 92
29 ../User_Pictures/320.jpg ../User_Pictures/Thumbs/320.jpg 92

$rows_pictures = mysql_num_rows($pictures_result);

When I echo $rows_pictures; I get 4 for user_id=92 which means the query is not failing. I however get only 3 thumbs: picture_id's 25,28 and 29. No 24.

Are you sure the file exists >?

The file indeed exists in the folder named User_pictures. I have been able to call in in other PHP pages. I just don't understand why it won't show. I decided to delete picture_id 24. Now the thumbs returned are just 2; 28 and 29.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.