Jump to content

[SOLVED] Help with creating photo gallery


php_beginner_83

Recommended Posts

Hi All

 

I'm been messing around with my code and finally have something working, but I'd like to ask your opinion on something. So I'm trying to create a photo gallery and so far I have 2 links, Previous and Next. Clicking on these will navigate through the pictures. However, at the minute I only have it displaying one predefined picture when you click 'Previous' and another when you click 'Next'. I want to ask your opinion on which way or how would be the best way to loop through the pictures or determine which one is being displayed and then get the next one or previous one in the sequence, depending on which link the user clicks.

My code so far is..

 

 

<?php
// directory where photos are kept
$dir = "images/thumbnails/";
$photos = array();

if($handle = opendir($dir))
{
while(false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." ) 
{
$photos[] = $file;
}
}
closedir($handle);
}

// count the no of photos
$noOfPics = count($photos);
$prev = 2;
$next = 4;
echo '<a href=My_Try_At_Album.php?p=' . $prev . '>Previous!!</a>';
echo '</br></br><a href=My_Try_At_Album.php?p=' . $next . '>Next!!</a>';

if (isset($_GET['p']))
{
if (is_numeric($_GET['p']))
{
$picIndex = $_GET['p'];
}
else
{
$picIndex = 1;
}

$currentPic = $photos[$picIndex];

echo '</br></br><img src="' . $dir . $currentPic .'"/>';

}

?>

Thanks again :-)

Link to comment
Share on other sites

The "best" way - in my opinion - would be to put the images into a database. You could then organize your photo albums and do things such add names, descriptions to the photos and even specify the order.

 

But, if you are only making this a simple file driven system, then I would suggest putting all the images in the directory into an arry. Then use the array index to detemine the previous, current & next images.

 

Here's a sample script. Just change the $directory to the path to a folder with jpg images.

<?php

$directory = 'images';
$imageList = glob("$directory/*.jpg");

$imgIndex = $_GET['img'];

if(!isset($imageList[$imgIndex]))
{
    $imgIndex = 0;
}

$currentImage = $imageList[$imgIndex];

if ($imgIndex<=0)
{
    $prev = "Previous";
}
else
{
    $prev = "<a href=\"".$_SERVER[php_SELF]."?img=".($imgIndex-1)."\">Previous</a>";
}
if ($imgIndex>=(count($imageList)-1))
{
    $next = "Next";
}
else
{
    $next = "<a href=\"".$_SERVER[php_SELF]."?img=".($imgIndex+1)."\">Next</a>";
}

?>
<html>
<body>
<?php

echo "$prev $next<br>";
echo "<img src=\"{$directory}/{$currentImage}\">";

?>
</body>
</html>

Link to comment
Share on other sites

In addition to mjdamato's post...

 

If you decide to put your images into the database, be sure to link to the file instead of putting the actual image into the database as blob as this will be more complex and will be much slower...

 

In general, you would have a folder on the filesystem where you store the images and put the location and filename into the database...

Then, if you need to delete images, first get them all, unlink() them and then delete the records from the database....

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.