Paginator for my 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
Raziel
Forum Newbie
Posts: 1
Joined: Fri Apr 02, 2010 8:12 am

Paginator for my gallery

Post by Raziel »

Hi all! I have problem and I hope anybody can help me... I made simple php gallery, where php script take photo`s from image folder an put on site - make thumbnails and when I click, than open photo on full size. When I open my script it look like this:

Image

But I need paginator for this script. If I press button next, or 2 than it open other 9 images... Somethink like that:

Image

But I can`t find any tutorial, how to make paginator for my gallery :( Mby somebody can help me?

php script:

Code: Select all

<?php

$dir = "images/";
if ($opendir = opendir($dir)){
 $numb=0;
 while (($file = readdir($opendir)) !==false){
  if ($file!="."&&$file!=".."){
   $numb++;
   echo "<a href='$dir/$file' target='_self'><img class='photo' src='$dir/$file' width='150' height='150' border='0' /></a>";
   if ($numb%3==0) echo '<br>';
   if ($numb==9) break;
  }
 }
}


?>
Thanks!

PS. Sorry for my bad English :(
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Paginator for my gallery

Post by social_experiment »

Put all the images an array and use the functions below to pagination them -
(These two functions are from a class. Modify as needed)

Code: Select all

<?php
public function paginationWithoutDb($itemsPerPage, $value, $page) {
		//
		if (isset($_GET['p']) && is_numeric($_GET['p'])) {
			$pages = $_GET['p'];
		}
		else {
			if (is_array($value)) {
			$amount = count($value);					
			}
			else {
				echo 'The second argument for the function must be
				an array';
			}
			
			if ($amount > $itemsPerPage) {
				$pages = ceil($amount/$itemsPerPage);
			}
			else {
				$pages = 1;
			}
		}
		//
		if (isset($_GET['s']) && is_numeric($_GET['p'])) {
			$start = $_GET['s'];
		}
		else {
			$start = 0;			
		}
		
		//
		$i = $start;
		$x = 0;
		
		echo '<ul>';
			while ($x != $itemsPerPage) {				
				$className = ($className == "odd" ? "even" : "odd");
				if ($value[$i] != '') {
					echo "<li class=\"$className\">".$value[$i]."</li>";
				}
				$x = $x + 1;
				$i = $i + 1;
			}
		echo '</ul>';
			
		//			
		$this->createPages($page, $start, $itemsPerPage, $pages);
	}
	
	//
	public function createPages($page, $start, $itemsPerPage, $pages) {
		echo '<div id="pages">';
		if ($pages > 1) {
				$current_page = ($start/$itemsPerPage) + 1;
				if ($current_page != 1) {
					echo "<a href=\"$page?s=".($start-$itemsPerPage)."&p=".$pages."\">
					Previous</a>&nbsp;";
				}
				for ($i = 1; $i <= $pages; $i++) {
					if ($i != $current_page) {
						echo "<a href=\"$page?s=".(($itemsPerPage * ($i-1)))."&p=".$pages."\">".$i."</a>&nbsp;";
					}
					else {
						echo '<span>'.$i.'</span>&nbsp';
					}
				}		
				if ($current_page != $pages) {
					echo "&nbsp;<a href=\"$page?s=".($start+$itemsPerPage)."&p=".$pages."\">
					Next</a>";
				}			
		}
		echo '</div>';
	}
?>
On the page you implement the pagination the code looks as follows :

Code: Select all

<?php
//write all your image names into an array.
$value = array('Bill', 'Dick', 'Spot', 'Klaus', 'Nadia', 'Petra', 'Dieter', 'Juniper', 'Jalepeno');
 //5 is the amount of items per page.
 //$value is the name of the array
 //$_SERVER['PHP_SELF'] is the pagename (change as needed)
 $untitled->paginationWithoutDb(5, $value, $_SERVER['PHP_SELF']);
?>
Hope this helps.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Paginator for my gallery

Post by Christopher »

(#10850)
Post Reply