tags:

views:

33

answers:

3

i wanted to know how can i display the images like the on in the picture: i know that i have to retrive the images using a while loop, but then displaying them like this is the difficult bit, thanks!! :))

alt text

A: 

It's really not that hard, you just have PHP output the HTML of the table.

Here's a quick example :

$imagenum=0;
echo "<table>";
while( $imagenum < $maximages){
  echo "<tr>";
  for ( $i = 0; $i < 4; $i += 1) {
    echo "<td>";
    if( $imagenum < $maximages){
     echo "<img src='";
     echo get_url_of_my_image($imagenum);
     echo "' >";
    }
    echo "</td>";
    $image+=1;
  }
  echo "</tr>";
}
echo "</table>";
rlb.usa
thanks,is thier a way to do it without the use of tables
getaway
@Fanis answer will do it that way ; )
rlb.usa
A: 

Displaying the image is a matter of using the html <img> tag, and you can use the modulo operator % to know when you've displayed X images so you can close the existing row and open a new one.

$images ; //array of image urls
$countImages = count($images) ;

$imagesPerRow = 5 ;

for ($i = 0 ; $i < $countImages; $i++) {
    //display image here
    $image = $images[$i] ;
    echo "<img src='$image'>" ;

    if ($i % $imagesPerRow == 0) {
        //have displayed an entire row
        echo '<br>' ;
    }
}

Modulo returns the remainder of the division. So, lets say you want rows of 5 images. If $i is divided by 5 exactly then the remainder will be 0, which means 5 imageshave been shown so it's time for a new row.

Fanis
i love your answer, thanks, 5 images per row is exactly what i wanted aswell lol :)) but how about if i only one 4 rows to display, so basically 20 images! thanks
getaway
@getaway if you only want to display 4 rows of 5 images you can get only 20 images from the database, or if you can't limit those, make the loop go only up to 20 (assuming you have that many images)
Fanis
@getaway I suggest you take a look at Alex Howansky's answer as well. It's more fluid in the display. You just need to calculate the width you want them to appear in and the html will decide how many to put in. Then, if you resize the browser it will put less or more accordingly.
Fanis
+2  A: 

Maybe this:

<UL>
  <LI CLASS="image">
    <IMG SRC="..." />
  </LI>
  <LI CLASS="image">
    <IMG SRC="..." />
  </LI>
  <LI CLASS="image">
    <IMG SRC="..." />
  </LI>
</UL>

With this CSS:

.image {
  display: inline;
}
Alex Howansky
Note, this will automatically adjust the number of images to fit in your width, so you don't need to hardcode a number of columns.
Alex Howansky
Whoops, had my tags backwards, fixed.
Alex Howansky
Not sure why this got downvoted, here's an example of how it works, note you can resize on the fly and the columns adjust to match -- you won't get that with tables or forced line breaks: http://howansky.org/stackoverflow/3791019.html
Alex Howansky
I agree with Alex. This is how I do it, set the width of the containing UL element, and the width of the LIs and it will automatically put it in rows.
Liam Bailey