Convert Previous/Next Buttons to Just Pic Link

Hello All… Having issue with converting this php code from buttons to a button. What i am trying to accomplish is simple.

I want to make a folder of pictures go from pic to pic by clicking the pic to go to the next in the folder. I want to click the pic to make this happen so the pic is the button… what i have is below… which has been made to use a previous and next text button which I want to convert to be the image. I dont know how to define the image as the button… I dont need a previous button i just want the pictures to go on forever until it reaches the last file and then begin at the begging.

[code]<?php
session_start();

$dir = ‘yorkwood/images’; //<<<---- Path to your images, NO TRAILING SLASH.

if(!isset($_SESSION[‘images’])) { //If the image array IS NOT saved in sessions

if ($handle = opendir($dir)) { //Open the image directory.

while (false !== ($file = readdir($handle))) { //while there are files to read.

if($file != “.” && $file != “…”) { //if the file isn’t current dir, or above dir.

$parts = explode(’.’,$file); //split the file on a period.

$c = count($parts); //count the parts array.

$ext = $parts[$c-1]; //ext will reside as the last value of the array.

if(strtolower($ext) == ‘png’ || strtolower($ext) == ‘jpg’ || strtolower($ext) == ‘gif’) { //if the ext is image type.

$images[] = $file; //save it to the image array.

}

}

}

closedir($handle); //close the directory.

}

$_SESSION[‘images’] = $images; //write the image array to a session variable.
} //this closes the if block, the above code will only run when the page is first open.

//Below is the displaying of the images, if you add images to the directory, you MUST close the browser window for
//this script to pick them up.

if(!isset($_SESSION[‘nextimage’])) { //if next image is NOT in the session array.

$next = $_SESSION[‘images’][1]; //next image will be the second image in the image array.

$current = $_SESSION[‘images’][0]; //current image will be the first image in the array.
}

else { //if next image is in the session array.

if(isset($_GET[‘next’])) { //and next is in the url bar.

$current = $_SESSION[‘nextimage’]; //current image is changed to the one held in Sessions nextimage.

}

elseif(isset($_GET[‘prev’])) { //or if prev is in the url bar,

$current = $_SESSION[‘previousimage’]; //we go to the previousimage in our sessions array.

}

}

$keys = array_keys($_SESSION[‘images’],$current); //find our array key of the current pic.
$n = $keys[0]; //the key will reside in the first place of the keys array.
$next = (array_key_exists($n+1,$_SESSION[‘images’])) ? $_SESSION[‘images’][$n+1] : $_SESSION[‘images’][0]; //if the next array key exists in the images array, set the next image, if it don’t the first image is next.
$previous = (array_key_exists($n-1,$_SESSION[‘images’])) ? $_SESSION[‘images’][$n-1] : end($_SESSION[‘images’]); //if the previous key exists in the images array, set the previous image to it, if it don’t set previous to the last value of the image array.

$_SESSION[‘nextimage’] = $next; //write the next to the session array.
$_SESSION[‘previousimage’] = $previous; //write the previous to the session array.

echo ‘images
’ . “\n”; // print the image element to the page.

echo (isset($previous)) ? ‘Previous
’ : NULL; //print the previous link to the page.
echo (isset($next)) ? ‘Next
’ : NULL; //print the next link to the page.
?>
[/code]

I suppose I’'m begging instead of beginning… this has got to be possible.

I believe the only part of this code that needs to be fixed is the array/link section

[code]
$keys = array_keys($_SESSION[‘images’],$current); //find our array key of the current pic.
$n = $keys[0]; //the key will reside in the first place of the keys array.
$next = (array_key_exists($n+1,$_SESSION[‘images’])) ? $_SESSION[‘images’][$n+1] : $_SESSION[‘images’][0]; //if the next array key exists in the images array, set the next image, if it don’t the first image is next.
$previous = (array_key_exists($n-1,$_SESSION[‘images’])) ? $_SESSION[‘images’][$n-1] : end($_SESSION[‘images’]); //if the previous key exists in the images array, set the previous image to it, if it don’t set previous to the last value of the image array.

$_SESSION[‘nextimage’] = $next; //write the next to the session array.
$_SESSION[‘previousimage’] = $previous; //write the previous to the session array.

echo ‘images
’ . “\n”; // print the image element to the page.

echo (isset($previous)) ? ‘Previous
’ : NULL; //print the previous link to the page.
echo (isset($next)) ? ‘Next
’ : NULL; //print the next link to the page.
?>[/code]

can it be done like this? Combining the three last lines above? I omitted the previous and the word next by enclosing the current pic with the href?

echo ' (isset($next)) ? '<a href="?next=1"><img src="' . $dir . '/' . $current . '" alt="images" /></a>' . "\n";

omg

Using This Code:

[PHP]<?php
session_start();

$dir = ‘/Test/images’; //<<<---- Path to your images,

if(!isset($_SESSION[‘images’])) { //If the image array IS NOT saved in sessions

if ($handle = opendir($dir)) { //Open the image directory.

while (false !== ($file = readdir($handle))) { //while there are files to read.

if($file != “.” && $file != “…”) { //if the file isn’t current dir, or above dir.

$parts = explode(’.’,$file); //split the file on a period.

$c = count($parts); //count the parts array.

$ext = $parts[$c-1]; //ext will reside as the last value of the array.

if(strtolower($ext) == ‘png’ || strtolower($ext) == ‘jpg’ || strtolower($ext) == ‘gif’) { //if the ext is image type.

$images[] = $file; //save it to the image array.

}
}
}

closedir($handle); //close the directory.

}

$_SESSION[‘images’] = $images; //write the image array to a session variable.

} //this closes the if block, the above code will only run when the page is first open.

//Below is the displaying of the images, if you add images to the directory, you MUST close the browser window for
//this script to pick them up.

if(!isset($_SESSION[‘nextimage’])) { //if next image is NOT in the session array.

$next = $_SESSION[‘images’][1]; //next image will be the second image in the image array.

$current = $_SESSION[‘images’][0]; //current image will be the first image in the array.

}

else { //if next image is in the session array.

if(isset($_GET[‘next’])) { //and next is in the url bar.

$current = $_SESSION[‘nextimage’]; //current image is changed to the one held in Sessions nextimage.

}

elseif(isset($_GET[‘prev’])) { //or if prev is in the url bar,

$current = $_SESSION[‘previousimage’]; //we go to the previousimage in our sessions array.
}
}

$keys = array_keys($_SESSION[‘images’],$current); //find our array key of the current pic.

$n = $keys[0]; //the key will reside in the first place of the keys array.

$next = (array_key_exists($n+1,$_SESSION[‘images’])) ? $_SESSION[‘images’][$n+1] : $_SESSION[‘images’][0]; //if the next array key exists in the images array, set the next image, if it don’t the first image is next.

$previous = (array_key_exists($n-1,$_SESSION[‘images’])) ? $_SESSION[‘images’][$n-1] : end($_SESSION[‘images’]); //if the previous key exists in the images array, set the previous image to it, if it don’t set previous to the last value of the image array.

$_SESSION[‘nextimage’] = $next; //write the next to the session array.
$_SESSION[‘previousimage’] = $previous; //write the previous to the session array.

$photo=$dir."/".$current;
echo “<a href=”?next=1"><img src="$photo" alt=“images” />"; // print the image element to the page.

?> [/PHP]

Displaying:

With this in the folder.

Any Thoughts?

Sponsor our Newsletter | Privacy Policy | Terms of Service