Page 1 of 1

next and back in an image gallery

Posted: Wed Sep 28, 2005 3:28 pm
by lostinphp
im still stuck up wid this image gallery thingy..
i need to navigate within the gallery using back and next btns..but i dont have a database...one idea wud be to get the name of all the images in a folder in an array and then call it to display the next and back images..

Code: Select all

<?php
//get directory contents
$contents = array();
$dir = opendir('thimg');
while (false !== ($file = readdir($dir))) {
	$contents[] = $file;
}
closedir($dir);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Get Directory</title>
<meta http-equiv="Content-Type" content="text/html; 
charset=iso-8859-1">
</head>
<body>
<?
/* // Print file names
foreach($contents as $file){
	if(($file != '.') and ($file != '..')){
		print ($file . '<br>');
	}
} */
?>
</body>
</html>
<a href='<? echo"display.php?img=$file[]"?>'>previous</a>//i dont know how to call it here
<a href='<? echo"display.php?img=$file[]"?>'>next</a>
<?php
$url_path = "upimg";
$dir = dir($url_path);
$imagename=$_GET['img'];
{if($entry == "." || $entry == "..") { 
        continue; 
    } 
echo"<center>";?> 
<a href="image.php"><img src='<? echo"$url_path/$imagename"?>'></a>
<? echo "</center>";}?>

Posted: Thu Sep 29, 2005 12:59 am
by ruchit
you would be better served if you use session variables rather than read the contents of the folder every time the page is requested... create an array of image names... sort this array (alphabetically maybe).. on the values... keep a counter, again as a session variable to pass as offset value to the array of image names...
however, the new images uploaded during a particular user's session won't be available to him. So, if image addition/updation is more frequent on your site.. you can read the no. of images in the folder and compare that with the count of ur array of images.. if they don't match you can use push/pop in the array... & this explains the purpose of sorting of array based on image names.
PS: This is as far as I can go, pls don't ask for examples. I've given you enough pointers to get you started.

Posted: Thu Sep 29, 2005 2:37 am
by harrisonad
I like the way friendster do their image gallery. It browses all the image without going to the next page.
Kind of client-side effort
All the thumbnails have onclick event handler something like this

Code: Select all

onclick='imgframe.src=this.value'

Posted: Thu Sep 29, 2005 3:22 am
by jayshields
read all the files from your image dir. now loop through them doing the HTML as you go, put some sort of counter in the loop and then when it is in multiples of 5 (or however many images you want per page...) make it do the HTML to cut the page off and start a new one.

its just an idea, but if you wanna do it well you should read into using a pagination method; there are some good threads in this forum about it, just search.