Jump to content


This topic is now archived and is closed to further replies.


is there a better, more efficient way of creating an image gallery

Recommended Posts

I wanted a really simple image gallery but all the ones i found were too complicated with admin areas and stuff that has no use for me.

Then I found some code which uses html and css to create the image gallery but the code could end up being repetitive and long so am wandering if there is soimething that can be one in php to make the code shorter

Here is a small part of the code whcih would display four images on one row.  Imagine if you had ten rows and how mmuch code that would take.
<a class="thumbnail" href="#"><img src="gallery/jack002_thumb.jpg" width="100px" height="75px" border="0" /><span><img src="gallery/jack002.jpg" /></span></a>

<a class="thumbnail" href="#"><img src="gallery/jack001_thumb.jpg" width="100px" height="75px" border="0" /><span><img src="gallery/jack001.jpg" /></span></a>

<a class="thumbnail" href="#"><img src="gallery/jack003_thumb.jpg" width="100px" height="75px" border="0" /><span><img src="gallery/jack003.jpg" /></span></a>

<a class="thumbnail" href="#"><img src="gallery/jack004_thumb.jpg" width="100px" height="75px" border="0" /><span><img src="gallery/jack004.jpg" /></span></a>

<a class="thumbnail" href="#"><img src="gallery/jack005_thumb.jpg" width="100px" height="75px" border="0" /><span><img src="gallery/jack005.jpg" /></span></a>

<br />[/code]

Is there something that could do this as many times as needed...probably ten?

Share this post

Link to post
Share on other sites
I would do this one of two ways.

I would ether store the names of the images in a database table then use mysql_fetch_assoc/array to display them

or you could, if you only have the gallery images it that dir, [code=php:0]readdir[/code]

like this
if ($handle = opendir('path/to/gallery')) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != "..") {
           echo "<a class=\"thumbnail\" href=\"#\"><img src=\"gallery/$file\" width=\"100px\" height=\"75px\" border=\"0\" /><span>";

You may want to refine this code a bit. It should give you a basic idea

Good luck,

Share this post

Link to post
Share on other sites
Yeah...storing image name in the database is what i do

How would i display 4 images at a time using mysql_fetch_accoc/array?

I've only ever displayed one at a time when pulling stuff out

Share this post

Link to post
Share on other sites
see (table method)


and here's a css method

include 'db.php';
<meta name="generator" content="PhpED Version 4.5 (Build 4513)">
<title>CSS example</title>
<meta name="author" content="Barand">
<STYLE type='text/css'>
DIV.item {
    width: 150px;
    height: 150px;
    border: 1px solid silver;
    margin: 5px;
    text-align: center;
    padding; 5px;
    float: left;
BR {
    clear: both;
        $sql = "SELECT a, b FROM tablename
                ORDER BY a, b";
        $res = mysql_query($sql);
        while (list($a, $b) = mysql_fetch_row($res)) {
            echo "<DIV class='item'>$a<br>$b</DIV>";
            if (++$count % 4 == 0) echo '<br>';


Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.