Return to Snippet

Revision: 33425
at October 8, 2010 18:15 by stz184


Initial Code
function paginator($items, $perpage = 10, $maxlinks = 10) {
	$delitel = $maxlinks;
	$totalpages = ceil($items / $perpage);
	if(isset($_GET['page']) && is_numeric($_GET['page'])) {
		$page = $_GET['page'];
	} 
	else {
		$page = 1;
	}
		
	if($items != 0 && $page >= 1 && $page <= $totalpages) {
		if($totalpages > 0 && $totalpages <= $maxlinks) {	
			$maxlinks = $totalpages;
			$c = 1;
		}
		else {
			$c = $page;
			$min = floor($maxlinks /2);
			$max = $totalpages - $min;
			
			if(($page > $min) && ($page < $max)) {
				$c = $page - $min;
			}
			elseif($page > $min && $page >= $max) {
				$c = $max;
			}
			else {
			
				$c = 1;
			}
		}
		
		$link = '';
		if(isset($_SERVER['PATH_INFO'])) {
			$link .= $_SERVER['PATH_INFO'];
		}
		$link .= '?';
		foreach($_GET as $g => $v) {
			if($g != 'page') {
				$link .= $g.'='.$v.'&';
			}
		}
		
		$out = '';
		if($totalpages != 1 && $page != 1) {
			$out .= "<a class=\"page\" href=\"".$link."page=1\"><span>First page</span></a>";
		}
		if($page != 1) {
			$out .= "<a class=\"page\" href=\"".$link."page=".($page - 1)."\"><span>&laquo;</span></a>";
		}
			
		for($i=1; $i <= $maxlinks; $i++) {
				if($c <= $totalpages && $totalpages != 1) {
				if($c == $page) {
					$out .= "<a class=\"current\" href=\"".$link."page=".$c."\"><span>".$c."</span></a>";
				}
				else {
					$out .= "<a class=\"page\" href=\"".$link."page=".$c."\"><span>".$c."</span></a>";
				}
				$c++;
				}
			}
		if($totalpages == 1) {
			$out .= '<a href="#" class="current"><span>Page 1 of 1</span></a>&nbsp;';
		}
		if($page != $totalpages && $page < $totalpages) {
			$out .= "<a class=\"page\" href=\"".$link."page=".($page + 1)."\"><span>&raquo;</span></a>&nbsp;";
		}
		if($totalpages != 1 && $page != $totalpages) {
			$out .= "<a class=\"page\" href=\"".$link."page=".$totalpages."\"><span>Last page</span></a>";
		}
	}
	return $out;
}

Initial URL

                                

Initial Description
This function build page navigation links.\r\nParameters legend:\r\n- $items = total number of rows to be paginated\r\n- $perpage = how many rows to be displayed on a page\r\n- $maxlinks - how much page links to be build and shown for navigation ( < 1 2 3 4 5 > )

Initial Title
Paginator (page navigation)

Initial Tags
page, navigation

Initial Language
PHP