PHP BY EXAMPLE
quote generator
slideshow generator
photo gallery generator
photo paginator gallery generator
address book
basic blog
paypal generator
sc simple cms

qg P2G2 : PHOTO PAGINATOR GALLERY GENERATOR



Example

p2g2banner

PHP Code

<?php
$targetpage = "index.php"; 	
$limit = 34; 
$folder = opendir("photos"); // Use 'opendir(".")' if the PHP file is in the same folder as your images. Or set a relative path 'opendir("../path/to/folder")'.

$pic_types = array("jpg", "jpeg", "gif", "png");

$index = array();

while ($file = readdir ($folder)) {

  if(in_array(substr(strtolower($file), strrpos($file,".") + 1),$pic_types))
	{
		array_push($index,$file);
	}
}

$total_pages = (int)(count($index)/36)+1;
	
	$stages = 3;
	$page = mysql_real_escape_string(@$_GET['page']);
	if($page){
		$start = ($page - 1) * $limit; 
	}else{
		$start = 0;	
		}	
	
    // Initial page num setup
	if ($page == 0){$page = 1;}
	$prev = $page - 1;	
	$next = $page + 1;							
	$lastpage = 3;		
	$LastPagem1 = $lastpage - 1;			
	$paginate = '';
	if($lastpage > 1)
	{	
	  $paginate .= "
"; // Previous if ($page > 1){ $paginate.= "previous"; } else { $paginate.= "previous"; } // Pages if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up { for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page){ $paginate.= "$counter"; }else{ $paginate.= "$counter";} } } elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few? { // Beginning only hide later pages if($page < 1 + ($stages * 2)) { for ($counter = 1; $counter < 4 + ($stages * 2); $counter++) { if ($counter == $page){ $paginate.= "$counter"; }else{ $paginate.= "$counter";} } $paginate.= "..."; $paginate.= "$LastPagem1"; $paginate.= "$lastpage"; } // Middle hide some front and some back elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2)) { $paginate.= "1"; $paginate.= "2"; $paginate.= "..."; for ($counter = $page - $stages; $counter <= $page + $stages; $counter++) { if ($counter == $page){ $paginate.= "$counter"; }else{ $paginate.= "$counter";} } $paginate.= "..."; $paginate.= "$LastPagem1"; $paginate.= "$lastpage"; } // End only hide early pages else { $paginate.= "1"; $paginate.= "2"; $paginate.= "..."; for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page){ $paginate.= "$counter"; }else{ $paginate.= "$counter";} } } } // Next if ($page < $counter - 1){ $paginate.= "next"; }else{ $paginate.= "next"; } $paginate.= "
"; } // pagination echo $paginate; ?> <?php closedir($folder); ?>

PHP Code Explanation

Tweet Follow      © 2013 | webcula.com (all rights reserved) | php by example