Forum Moderators: coopster

Message Too Old, No Replies

Image Display Page with Next and Last links

Image display with next and last links

         

hostanything

1:24 am on Nov 19, 2008 (gmt 0)

10+ Year Member



Hi, this is my first post here :)

I've built a page to display an image based on the image name passed to the script. The problem I'm having is on that image display page I want to put a next and last link. I understand how to read in all the files in a directory, but getting confused on how to determine what the next image is in a directory based off the image currently being displayed. Hope that makes sense :)

Example, my script is named image.php. I have several images in an image directory. As of now they are named 01_01.jpg, 01_02.jpg, 01_03.jpg, 02_01.jpg, etc... If I want to display the second image I would link to the page image.php?p=01_02.jpg on that image.php page I would like to have a next and last link but can't figure out a short way to do it.

Thanks in advance for your help :)

willis1480

3:31 am on Nov 19, 2008 (gmt 0)

10+ Year Member



good starting place for learnin a little PHP and there a several ways to get started with this.

1)get a list of all files in a directory
2)based on current image determine next and previous

Here is simple example of what I would do:

<?php
$pic_dir = "path_to_pic_dir"; //only pic files in dir
$pic_array = scandir($pic_dir);

//http://mydomain.com/image.php?pic=01_01.jpg
$show = $_GET['pic']; //01_01.jpg
if($show == "" ¦¦ $show == NULL){
$show = $pic_array[2];
}

//start at 2 to avoid . and ..
for($i=2;$i<count($pic_array);$i++){
$value = $pic_array[$i];
//first pic in array
if($value == $show && $i==2){
$prev = "";
$current = $value;
$next = $pic_array[$i+1];
//middle pic in array
}elseif($value == $show && $i>2){
$prev = $pic_array[$i-1];
$current = $value;
$next = $pic_array[$i+1];
//last pic
}elseif($value == $show && $i==(count($pic_array)-1)){
$prev = $pic_array[$i-1];
$current = $value;
$next = "";
}
}
//previous link
if($prev !== "" && $prev!==NULL){
echo "<a href='http://mydomain.com/image.php?pic=$prev'><< Previous</a>\n";
}
//current image
echo "<img src='$pic_dir/$current' />\n";
//next link
if($next !== "" && $next!==NULL){
echo "<a href='http://mydomain.com/image.php?pic=$next'>Next >></a>\n";
}
?>

Tested it and seems to work fine for me.

Just change the mydomain and the path_to_pic_dir

Also, pic directory should have no files other than images...including this file

hostanything

6:27 pm on Nov 19, 2008 (gmt 0)

10+ Year Member



Thanks for the response. I did things a little different but I did take some inspiration from your code. scandir is a php5 only function and I still have a couple servers running version 4. Also, I like to validate ALL input because I'm paranoid :) Here's what I came up with so far... it works fine, but still have a few bugs to work out.


<?php

// Our sample images and their thumbnail directories
$thumb_dir = 'images/pictures/thumbnails';
$img_dir = 'images/pictures';

// Lets check our input and validate it. We only want jpg images
if (preg_match("/\w+\.jpg$/",$_GET['p'])) { $image = $_GET['p']; }

// Read the directory and only get images
if ($handle = opendir($img_dir)) {
while (false !== ($filename = readdir($handle))) {
if (preg_match("/\w+\.jpg$/",$filename)) {
$files[] = $filename;
sort($files);
}
}
closedir($handle);
}

// If no image was set, we'll make the first image the default
if ($image && file_exists("$img_dir/$image")) {} else { $image = $files[0]; }

for ($i=0;$i<count($files);$i++) {
if ($files[$i] == $image) {
$last = $files[$i-1];
$next = $files[$i+1];
$image = $files[$i];
break;
}
}
?>

Then in the body where I want to display the image and links...


<?php echo "<img src='$img_dir/$image'>"; ?>

<?php
if (file_exists("$img_dir/$last")) { echo "<a href='image.php?p=$last'> << Last Image </a> ¦ "; }
if (file_exists("$img_dir/$next")) { echo "<a href='image.php?p=$next'> Next Image >> </a>"; }
?>

If you see any potential problems, or a more efficient way of doing it, please let me know :)

Regards, Wayne

willis1480

10:50 pm on Nov 19, 2008 (gmt 0)

10+ Year Member



good work...I just wrote something real simple to give you an idea of a quick and dirty method. Yeah, forgot to mention the PHP5 stuff.

I would add php5 to the server. I have come across a lot of useful functions that are now built in.

Last loop will have issue...if file showing is at $i=0, then $file[$i-1] may cause a problem (dont think PHP will yell at you though)