next and back in an image gallery

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lostinphp
Forum Commoner
Posts: 44
Joined: Wed Jul 27, 2005 5:10 am
Location: Paris,France.

next and back in an image gallery

Post 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>";}?>
ruchit
Forum Commoner
Posts: 53
Joined: Mon Sep 26, 2005 6:03 am

Post 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.
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post 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'
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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.
Post Reply