Re: Creating 'Next' & 'Previous' for PHP Photo Gallery

From: Date: Tue, 10 Jul 2007 15:04:30 +0000
Subject: Re: Creating 'Next' & 'Previous' for PHP Photo Gallery
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-258552@lists.php.net to get a copy of this message
Rahul Sitaram Johari wrote:
I¹m trying to write a Photo Gallery in PHP. Everything else is pretty much worked out ­ like thumbnails, indexes, titles & all ­ the one thing I¹m stuck at is the Next & Previous links for the Photos on the main Photo Page. It¹s a simple program where you drop images in a folder and glob() picks up the thumbnails ­ displays them on a page ­ which are automatically linked to the Full Size Images. This is the code: <? $ID = $_GET['ID'];
The following code is completely untested. I typed it straight into the email client. You might have to track down a few bugs. It should give you a good start though # Set your page display limit $display_limit = 20; # Collect a list of files $filelist = glob("$ID/thumbnails/*.jpg"); # get the current page number, set the default to 1 $pageNum = 1; if ( !empty($_GET['pageNum']) ) { $pageNum = (int)$_GET['pageNum']; } # count the total number of files to list $total_files = count($filelist); # calculate the total number of pages to be displayed $total_pages = ceil($total_files/$display_limit); # Initialize the link holder array $links = array(); # Since I don't know the name of your script, I assume that it should link to itself # So I will use $_SERVER['SCRIPT_NAME'] to get the scripts true name # Create Previous tag is needed if ( $pageNum > 1 ) { $links[] = "<a href='{$_SERVER['SCRIPT_NAME']}?ID={$_GET['ID']}&pageNum=".($pageNum-1)."'>Previous</a>"; } # Create Next tag is needed if ( $pageNum < $total_pages ) { $links[] = "<a href='{$_SERVER['SCRIPT_NAME']}?ID={$_GET['ID']}&pageNum=".($pageNum+1)."'>Next</a>"; } # Display the link if they exist echo '[ '.join(' | ', $links).' ]';
foreach ($filelist as $key=>$value) {
$title = rtrim(basename($value),'.jpg'); $newtitle = preg_replace('/_/', ' ', $title); ?> <A HREF="photo.php?photo=<?php echo basename($value); ?>&title=<?php echo $newtitle; ?>&ID=<?php echo $_GET['ID']; ?>&KEY=<?php echo $key; ?>" TARGET="photoFrame"> <IMG SRC="<?php echo $ID; ?>/thumbnails/<?php echo basename($value); ?>"></A> <?php echo $newtitle; ?> <? } ?> I know that $key holds the sequence of images that are being picked up by glob() - I¹m just trying to figure out a way to use this $key to generate the Next & Previous link. The problem is ­ everything is passed on to a separate page (photo.php) ... What I¹m thinking is determining which Photo File would be Next or Previous to the one being selected, and passing that along to the (photo.php) page. I¹m just not able to figure out how to pick out the next & previous filename and place it in the Query String. Any suggestions? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Rahul Sitaram Johari CEO, Twenty Four Seventy Nine Inc. W: http://www.rahulsjohari.com E: sleepwalker@rahulsjohari.com ³I morti non sono piu soli ... The dead are no longer lonely²
-- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare

« previous php.general (#258552) next »