Decent, simple, PHP Gallery script

RJM62

Touchdown! Greaser!
Joined
Jun 15, 2007
Messages
13,157
Location
Upstate New York
Display Name

Display name:
Geek on the Hill
I modified this simple script from something someone else posted on a board somewhere. It generates a gallery that can be easily inserted into any PHP page. It is liquid and automatically adjusts to browser resolution. My modification also integrates with the wonderful greybox lightbox that Jesse turned me on to.

It requires that greybox be installed, of course, and one MySQL database to store the thumbnail and full-size image paths. The "caption" value also sets the "title" attribute and serves as the heading for the greybox window, which is also good for optimization.

Here's the script (named "gallery.php"):

PHP:
<div id="gallery">
(Please click thumbnails to see larger images.)
  <p>&nbsp;</p>
<ul>
<?php
$username="****";
$password="****";
$database="critters_gallery";

mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM img_list WHERE page_name='$page'";
$result=mysql_query($query);

$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
    $thumb=mysql_result($result,$i,"thumb");
    $full_size=mysql_result($result,$i,"full_size");
    $caption=$title=mysql_result($result,$i,"caption");
    $alt=mysql_result($result,$i,"alt");
    print "<li><a href=\"Images/gallery/" . $full_size . "\" rel=\"gb_image[]\"" . "\" title=\"" . $title . "\"><img src=\"Images/gallery/" . $thumb . "\" alt=\"" . $alt . "\" border=\"0\"></a>" . PHP_EOL;
    print "<p>" . $caption . "</p></li>". PHP_EOL;
    $i++;
}
mysql_close();
?>
</ul>
</div>
The script is called from each page using a single line of code:

PHP:
<? $page="[page name]"; include("gallery.php") ?>
The "page name" isn't the full name of the page, but some short alpha name. For example, in the following page, the "page name" is simply "bats".

http://www.ridacritter.com/bats.php

That page also has a "featured pics" gallery in the left sidebar. It's basically the same script, but it gets the images from a different database.

I thought this might be useful to someone. It's simple, easy to implement and update, and it works well.

-Rich
 
Back
Top