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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jim Lucas wrote:
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");


New I would miss something,
As Nathan points out, the order of things might change.
To correct that, we should probably sort the list of files first

asort($filelist);


# 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).' ]';


And then I forgot to address this part of it.

# replace this
foreach ($filelist as $key=>$value) {

# with this
for( $i=(($display_limit*$pageNum)-1), $i<(min(($display_limit*$pageNum)-1,$total_files)), $i++) {
$key = $i;
$value = $filelist[$i];


$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@xxxxxxxxxxxxxxxx

³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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux