Jump to content


Photo

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


  • Please log in to reply
3 replies to this topic

#1 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 26 August 2006 - 09:31 PM

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 />

Is there something that could do this as many times as needed...probably ten?
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 26 August 2006 - 09:41 PM

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,
readdir

like this
<?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);
}
?>

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

Good luck,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 26 August 2006 - 10:21 PM

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
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 26 August 2006 - 11:44 PM

see (table method)

http://www.phpfreaks....html#msg422600

and here's a css method

<?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> 

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users