PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 7ms RepoModel.GetById 34ms app.codeStats 0ms

/paginator.class.php

http://showslow.googlecode.com/
PHP | 96 lines | 86 code | 9 blank | 1 comment | 20 complexity | 5233e9647e671eb24930fed3866b63a7 MD5 | raw file
 1<?php
 2
 3class Paginator{
 4	var $items_per_page;
 5	var $items_total;
 6	var $current_page;
 7	var $num_pages;
 8	var $mid_range;
 9	var $return;
10	var $default_ipp = 50;
11	var $querystring;
12
13	function Paginator()
14	{
15		$this->current_page = 1;
16		$this->mid_range = 7;
17		$this->items_per_page = $this->default_ipp;
18	}
19
20	function paginate($base)
21	{
22		$this->num_pages = ceil($this->items_total/$this->items_per_page);
23		$this->current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1; // must be numeric > 0
24		if($this->current_page < 1 Or !is_numeric($this->current_page)) $this->current_page = 1;
25		if($this->current_page > $this->num_pages) $this->current_page = $this->num_pages;
26		$prev_page = $this->current_page-1;
27		$next_page = $this->current_page+1;
28
29		if($_GET)
30		{
31			foreach($_GET as $key => $value)
32			{
33				if ($key != "page") {
34					$this->querystring .= '&' . urlencode($key) . '=' . urlencode($value);
35				}
36			}
37		}
38
39		if($this->num_pages > 10)
40		{
41			$this->return = ($this->current_page != 1 And $this->items_total >= 10)
42				? '<a class="paginate" href="' . htmlentities($base) .
43					'?page='.urlencode($prev_page) .
44					$this->querystring . '">&laquo; Previous</a> '
45				: '<span class="inactive" href="#">&laquo; Previous</span> ';
46
47			$this->start_range = $this->current_page - floor($this->mid_range/2);
48			$this->end_range = $this->current_page + floor($this->mid_range/2);
49
50			if($this->start_range <= 0)
51			{
52				$this->end_range += abs($this->start_range)+1;
53				$this->start_range = 1;
54			}
55			if($this->end_range > $this->num_pages)
56			{
57				$this->start_range -= $this->end_range-$this->num_pages;
58				$this->end_range = $this->num_pages;
59			}
60			$this->range = range($this->start_range,$this->end_range);
61
62			for($i=1;$i<=$this->num_pages;$i++)
63			{
64				if($this->range[0] > 2 And $i == $this->range[0]) $this->return .= " ... ";
65				// loop through all pages. if first, last, or in range, display
66				if($i==1 Or $i==$this->num_pages Or in_array($i,$this->range))
67				{
68					$this->return .= ($i == $this->current_page
69						? '<a title="Go to page '.htmlentities($i).' of $this->num_pages" class="current" href="#">'.htmlentities($i).'</a> '
70						: '<a class="paginate" title="Go to page '.htmlentities($i).' of '.htmlentities($this->num_pages).'" href="'.$base.'?page='.htmlentities($i).$this->querystring.'">'.htmlentities($i).'</a> ');
71				}
72				if($this->range[$this->mid_range-1] < $this->num_pages-1 And $i == $this->range[$this->mid_range-1]) $this->return .= " ... ";
73			}
74			$this->return .= (($this->current_page != $this->num_pages And $this->items_total >= 10))
75				? '<a class="paginate" href="'.$base.'?page='.htmlentities($next_page).$this->querystring.'">Next &raquo;</a>'."\n"
76				: '<span class="inactive" href="#">&raquo; Next</span>'."\n";
77		}
78		else
79		{
80			for($i=1;$i<=$this->num_pages;$i++)
81			{
82				$this->return .= ($i == $this->current_page)
83					? '<a class="current" href="#">'.htmlentities($i).'</a> '
84					: '<a class="paginate" href="'.$base.'?page='.htmlentities($i).$this->querystring.'">'.htmlentities($i).'</a> ';
85			}
86		}
87	}
88
89	function display_pages()
90	{
91		if ($this->num_pages <= 1) {
92			return '';
93		}
94		return $this->return;
95	}
96}