0

So let's say that I have a number of images on my server, uploaded by users. Each picture also has a row in the database containing information like filename, category, filesize, extension and location.

When I want to show these images I use a mysql_query and a while loop. This works, but the pictures all have different sizes and they show up next to each other or under each other which looks really bad.

Let's asume that I have 20 of these images, how can I show 5 images next to each other, 4 rows long???

Student
  • 531
  • 4
  • 12
  • 28
  • I'm not quite sure what you mean... isn't this more of a layout issue than a database issue? – Johannes Klauß May 30 '12 at 16:35
  • 1
    @j08691 — No, not with a table, it isn't a tabular data structure. – Quentin May 30 '12 at 16:37
  • It is a layout issue, but I never know how many images were uploaded. I just want to show 5 images next to each other. The next 5 images come under that and so on. – Student May 30 '12 at 16:38
  • possible duplicate of -> http://stackoverflow.com/questions/3791019/how-do-i-get-images-to-display-in-a-rows-like-this-using-php-and-css for your layout issue. would suggest using the answer in the link i posted. and use a table so that you can assign a define height and width for your images when they are displayed. – magicianiam May 30 '12 at 16:47
  • I would definitely agree with @Quentin. Tables should only be used for tabular data. This is a layout issue and should be solved using container elements such as `
    ` positioned and resized using CSS.
    – n00dle May 30 '12 at 17:04

2 Answers2

2

Assuming a numerically indexed array of objects containing image data:

<div class="row">
<?php
// loop through all my images taking the index and image object
foreach( $images as $idx=>$image ) {

    // If this isn't the 1st image & the modulus of 5 and $idx is 0, make a new row.
    if( $idx > 0 && !(5%$idx) ) {
        </div><div class="row">
    }
    // Print out the image
    echo '<img src="' . $image->filename . '" />';
} ?>
</div>

The modulus is basically the remainder if you divide the first number by the second. Therefore when we've printed 5 images, 5/5=1 with no remainder, so we create a new row.

I believe that would create something like:

<div class="row">
<img src="img1.jpg" />
<img src="img2.jpg" />
<img src="img3.jpg" />
<img src="img4.jpg" />
<img src="img5.jpg" />
</div>
<div class="row">
<img src="img6.jpg" />
<img src="img7.jpg" />
<img src="img8.jpg" />
<img src="img9.jpg" />
<img src="img10.jpg" />
</div>

Which could then be styled using CSS:

<style type="text/css">
    div.row {
       clear:left;
    }
    div.row img {
       float:left;
    }
</style>

When it comes to resizing the images, you have a 2 options:

  1. The easy option: set the image width in the CSS (the height will scale accordingly). This is less than perfect and if the images are different aspect ratios (width vs. height), will still look messy.
  2. Resize (and crop if necessary) using a PHP library such as GD or imagick (a PHP layer on top of imagemagick). Personally I prefer GD as it's packaged with PHP so it's got wider support. That said, imagick is quicker, simpler code, so it's up to you. This site gives a good comparison between the two for image resizing.

EDIT: in answer to your question, you'd need to do something like this to get the array of images:

<?php
// Build an array of images
$sql = 'SELECT * FROM images';
$result = mysql_query( $sql );

while( $obj = mysql_fetch_object( $result ) ) {
    $images[] = $obj;
}
?>
n00dle
  • 5,949
  • 2
  • 35
  • 48
  • Ah yes, this is what I was looking for. $images would be the result from the query? – Student May 30 '12 at 16:56
  • Yes, with a bit of post processing - I've put together a quick example and appended it to my answer. – n00dle May 30 '12 at 16:59
  • Try putting all the images on the same row and float left. It will automatically wrap using normal document flow. That way, it scales to the parent container. – Marcus Adams May 30 '12 at 17:11
  • Yep that would also work. I forced the breaks as Student specified 5, but your approach is better. – n00dle May 30 '12 at 21:46
0

pseudocode

$i = 0;
while($row = mysql_fetch_object($result) {
   //displaying image
   ....
   if($i % 4 == 0) {
          echo '<BR>';
   }
   $i++;
}
Ivan Fateev
  • 1,032
  • 1
  • 10
  • 26
  • http://stackoverflow.com/questions/1934173/what-does-the-percent-sign-mean-in-php modulo operator. In your case this will echo
    every 5th iteration of the cycle
    – Ivan Fateev May 30 '12 at 17:03