Hi.
I'm trying to write a script that fetchs image codes from a mysql database to display them into a 3 columns table.

I got this so far:

$selection = $_GET['scelta'];

$query  = 'SELECT photo_code FROM'. $selection .';';
$result = mysql_query($query);
$rows_nb = mysql_num_rows($query);

print('<table width="75%" border="0" align="center">');

while ($row = mysql_fetch_assoc($result))
{
print('<tr>');

for ($tb_rows=0;$tb_rows<3;$tb_rows++)
{
print('<td><div align="center"><a href="show.php?code='. $row['id'] .'"><img src="imagedb/thumbs/'. $row['id'] .'_small.png" 		border="1" /></a></div></td>');
}

print('</tr>');
}

print('</table>');

The for will print 3 columns and then create one more row for the next 3 pictures but now all it does is printing three times the same pic for each row.
How do I change the $row inside the for cicle?

I edited my post because the info I placed was inacurate as I misunderstood your problem and I cannot delete the post

Okay I understand your problem now... I would have re-edited my post but it won't allow me so I will have ot double post.


You will probably have to add another if statement in there because if you do it this way, and you don't have a number of pictures that is divisable by 3 you will get broken images.

For example if you have 7 images. The first two rows will have images fine, but the third row will have one good image and two broken images.

Try something like this:

$selection = $_GET['scelta']; 

$query  = 'SELECT photo_code FROM'. $selection .';'; 
$result = mysql_query($query); 
$rows_nb = mysql_num_rows($query);
 
$pic_num = 0; // Set a new variable for counting our pics, and set it to 0.
$pic_code = mysql_fetch_array($result); Get our array of codes.

print('<table width="75%" border="0" align="center">'); 

while ($row = mysql_fetch_assoc($result)) 
{ 
print('<tr>'); 

for ($tb_rows=0;$tb_rows<3;$tb_rows++) 
{
 
if ($pic_num < $rows_nb)  //check that we have not exceeded the number of pictures
{ 
print('<td><div align="center"><a href="show.php?code='. $pic_code[$pic_num] .'"><img src="imagedb/thumbs/'. $pic_code[$pic_num] .'_small.png"         border="1" /></a></div></td>'); //change our row arrays to pic arrays.
$pic_num++  // Increase the number of pictures we have placed.
}
else
{
print('<td></td>');
}
 
} 

print('</tr>'); 
} 

print('</table>');

Changing you $row to the new $pic_code[$pic_num] should fix your problem with displaying the same pic as well.

Hi.
The code GliderPilot posted is great, but now it seems like the $pic_code[$pic_num] trick doesn't work.
Only the first picture gets displayed and all the other are broken.
This is what happens

<td><div align="center"><a href="show.php?code=a_011"><img src="imagedb/thumbs/a_011-small.jpg" border="1" /></a></div></td>
<td><div align="center"><a href="show.php?code="><img src="imagedb/thumbs/-small.jpg" border="1" /></a></div></td>

You can see that the link and location of the first image are ok but all the other aren't.
I tried printing out the $pic_code[$pic_num] value under each picture but apart from the first all are empty.
What should I do?

I know what went wrong. Each time you fetch the array it only gets the first row. So you have to put the fetch array inside the loop so it gets each row (each seperate pic info) like this:

$selection = $_GET['scelta']; 
 
$query = 'SELECT photo_code FROM'. $selection .';'; 
$result = mysql_query($query); 
$rows_nb = mysql_num_rows($result);
 
$pic_num = 0; // Set a new variable for counting our pics, and set it to 0.
 
 
print('<table width="75%" border="0" align="center">'); 
 
while ($row = mysql_fetch_assoc($result)) 
{ 
 
 
  print('<tr>'); 
 
  for ($tb_rows=0;$tb_rows<3;$tb_rows++) 
  {
 
    $pic_code = mysql_fetch_array($result); //Get our code array for current pic
 
    if ($pic_num <= $rows_nb) //check that we have not exceeded the number of pictures
    { 
 
      print('<td><div align="center"><a href="show.php?code='. $pic_code[0] .'"><img src="imagedb/thumbs/'. $pic_code[0] .'_small.png" border="1" /></a></div></td>'); //change our row arrays to pic arrays.
 
      $pic_num++ // Increase the number of pictures we have placed.
 
    }
    else
    {
      print('<td></td>');
    }
 
  } 
 
  print('</tr>'); 
 
} 
 
print('</table>');

I alos fixed a couple small errors in the code that I missed before. Sorry I wrote it a little quickly and made a couple dumb mistakes. But the code above should work.

The code is not good. With 10 images it return

2,3,4
6,7,8
10

Image no. 1, 5 and 9 is missing.

This is how phpBB 3 gets images. Maybe it'll help you out.

/**
* Get user avatar
*
* @param string $avatar Users assigned avatar name
* @param int $avatar_type Type of avatar
* @param string $avatar_width Width of users avatar
* @param string $avatar_height Height of users avatar
* @param string $alt Optional language string for alt tag within image, can be a language key or text
*
* @return string Avatar image
*/
function get_user_avatar($avatar, $avatar_type, $avatar_width, $avatar_height, $alt = 'USER_AVATAR')
{
	global $user, $config, $phpbb_root_path, $phpEx;

	if (empty($avatar) || !$avatar_type)
	{
		return '';
	}

	$avatar_img = '';

	switch ($avatar_type)
	{
		case AVATAR_UPLOAD:
			$avatar_img = $phpbb_root_path . "download/file.$phpEx?avatar=";
		break;

		case AVATAR_GALLERY:
			$avatar_img = $phpbb_root_path . $config['avatar_gallery_path'] . '/';
		break;
	}

	$avatar_img .= $avatar;
	return '<img src="' . $avatar_img . '" width="' . $avatar_width . '" height="' . $avatar_height . '" alt="' . ((!empty($user->lang[$alt])) ? $user->lang[$alt] : $alt) . '" />';
}

SRC: phpBB3.0.0 includes/functions_display.php

Try this:

<?php
$i = 0;
$image_id = '';
$image_path = '';
$td = 0;
echo('<tr>');
while($i <= 10)
{
     $sql = "SELECT * FROM ' . IMAGES_TABLE . '
     WHERE image_id = $image_id
     AND image_path = $image_path";
     while($td <=3)
     {
          echo('<td><img src="');
          echo($image_path);
          echo('"></td>');
          $td++;
          if($td == 3)
          {
               echo('</tr><tr>');
               $td = 0;
          }
     }
     $i++;
     echo('</tr>');
}

I hope that's going to help some.

Posted a second time because the system wouldn't let me post my edits.

Basicly all I did was take what phpBB3 does and simplify it for displaying multiple images.

Hi
Maybe this code can do the trick:

<?
$res = mysql_query(“[insert your mysql query here]“);
$rows = mysql_num_rows($res);
$counter = 1;
$cols = 2;
echo (“<table>\n”);
for($i = 0; $i < $rows/$cols; $i++) {
echo (“<tr>”);
for($j=0; $j < $cols && $counter <= $rows ;$j++, $counter++) {
echo (“<td>[insert the data you want to display here]</td>\n”);
}
echo (“</tr>\n”);
}
echo (“</table>\n”);
?>

You can see some further explanation here:
<snipped>

Edited 6 Years Ago by nav33n: URL snipped. Reason: Do not spam, advertise, plug your website, or engage in any other type of self promotion.

This article has been dead for over six months. Start a new discussion instead.