php_beginner_83 Posted April 24, 2009 Share Posted April 24, 2009 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 :-) Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 24, 2009 Share Posted April 24, 2009 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> Quote Link to comment Share on other sites More sharing options...
DjMikeS Posted April 24, 2009 Share Posted April 24, 2009 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.... Quote Link to comment Share on other sites More sharing options...
php_beginner_83 Posted April 29, 2009 Author Share Posted April 29, 2009 Thanks fellas for your help and advice. I do eventually plan to use a database. I'm still pretty new to php so thought this would be an easier way to start off and build up to the database. Thanks again :-) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.