0

Hi,
I have a folder with ten photos and i want to give link for that photo.when i click the link first photo will only display and the remaining photos will be viewed by clicking next button or previous how to do that using PHP.

<a href="photo/dec1209"><font color="#0000FF">Click Here to View Photos</font></a>

this dec1209 is a folder and it contains about ten photographs.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by paulrajj
1

Hi,

Save this as a php page and link to this page to display images one by one from a directory.

<?php
$imagesDir = 'dec1209/*';

$itemsPerPage = 1;
$currentPage = isset($_GET['page']) ? $_GET['page'] : 1;
$totalItems = getTotalItems($imagesDir);
$totalPages = ceil($totalItems / $itemsPerPage);

getItemsFromPage($currentPage, $itemsPerPage, $imagesDir);
getPager($totalPages, $currentPage);

//counts the number of files in the dir
function getTotalItems($imagesDir) {
	$numImages = count(glob($imagesDir));
	return $numImages;
}
//gets the files from the dir
function getItemsFromPage($page, $numItems, $dir) {
	$offset = ($page - 1) * $numItems;
	$x = 0;
	// get the first item and display
	foreach(glob($dir) as $image) {
		if($x < $offset) {
			$x++;
		} elseif($x < $numItems + $offset) {
			echo "<li><img src=\"$image\" alt=\"$image\" /></li>";
			$x++;
		} else {
			break;
		}
	}
}
//creates the pages and the links
function getPager($totalPages, $currentPage) {
	// if we're not on the first page, show the previous page and first page links
	if($currentPage > 1) {
		$prevPage = $currentPage - 1;
		echo " <a href='{$_SERVER['PHP_SELF']}?page=1' style='text-decoration:none;'> First </a> ";    
		echo " <a href='{$_SERVER['PHP_SELF']}?page=$prevPage' style='text-decoration:none;'> Previous </a> ";
	}
	$range = 1;
	// show what page we're on and the page numbers surround this page
	for($x = ($currentPage - $range); $x < ($currentPage + $range) +1; $x++) {
		if(($x > 0) && ($x <= $totalPages)) {
			if($x == $currentPage) {
				echo " [<b>$x</b>] ";
			} else {
				echo " <a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a> ";
			}
		}
	}
	// if we're not on the last page show the next page and last page links
	if($currentPage != $totalPages) {
		$nextPage = $currentPage + 1;
		echo " <a href='{$_SERVER['PHP_SELF']}?page=$nextPage'> Next </a> ";
		echo " <a href='{$_SERVER['PHP_SELF']}?page=$totalPages'> Last </a> ";
	}
} 
?>

Have Fun !

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.