Jump to content

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


AdRock

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.
[code]
<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?
Link to comment
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
[code]
<?php
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>";
       }
   }
   closedir($handle);
}
?>[/code]

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

Good luck,
Tom
Link to comment
Share on other sites

see (table method)

http://www.phpfreaks.com/forums/index.php/topic,105762.msg422600.html#msg422600

and here's a css method

[code]<?php
include 'db.php';
?>
<html>
<head>
<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;
}
</STYLE>
</head>
<body>
    <DIV>
    <?php
        $sql = "SELECT a, b FROM tablename
                ORDER BY a, b";
        $res = mysql_query($sql);
        $count=0;
        while (list($a, $b) = mysql_fetch_row($res)) {
            echo "<DIV class='item'>$a<br>$b</DIV>";
            if (++$count % 4 == 0) echo '<br>';
        }

    ?>
    </DIV>
</body>
</html>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.