last changes
[ayans.git] / includes / paginate.php
blobc5ef1fbc0fed886466a76b48a9acf0a9d0e2fc09
1 <?php
2 /**
3 * @category News
4 * @package News_Paginate
5 * @copyright Copyright (c) 2008, Bellière Ludovic
6 * @license http://opensource.org/licenses/mit-license.php MIT license
7 */
9 /**
10 * @author Bellière Ludovic
11 * @category News
12 * @package News_Paginate
13 * @copyright Copyright (c) 2008, Bellière Ludovic
14 * @license http://opensource.org/licenses/mit-license.php MIT license
16 class paginate {
18 /**
19 * Nombre d'éléments par page.
21 * @var integer
23 public $limit_by_page=10;
25 /**
26 * Nombre d'éléments total
28 * @var integer
30 public $elements=0;
32 /**
33 * Nombre total de page qui seront générées
35 * @var integer
37 public $number_of_pages;
39 /**
40 * Variable a regarder pour connaître la page courrante.
42 * @var string
44 protected $page_variable="p";
46 /**
47 * Numéro de la page courrante
49 * @var integer
51 protected $current_page=1;
53 public function __construct($elements,$limit_by_page=10) {
54 $this->limit_by_page = (int) $limit_by_page;
55 $this->elements = (int) $elements;
56 self::set_current_page();
59 public function paginate($pageurl) {
60 $this->number_of_pages = ceil($this->elements / $this->limit_by_page);
61 if ($this->number_of_pages == 1) {
62 return "Page : 1.";
63 } else {
64 $pageurl = strpos($pageurl, '?') ? $pageurl.'&amp;' : $pageurl.'?';
65 $i=1;
66 $txt = ($this->current_page === 1) ? '' : '&lt; <a href="'.$pageurl.$this->page_variable.'='.($this->current_page-1).'">précédent</a> | ';
67 while($i < $this->number_of_pages) {
68 if ($i == $this->current_page) {
69 $txt.= "page $i, ";
70 } else {
71 $txt.= '<a href="'.$pageurl.$this->page_variable.'='.$i.'" class="paginator">page '.$i.'</a>, ';
73 $i++;
75 return substr($txt,0,-2).(($this->number_of_pages > $this->current_page) ? ' | <a href="'.$pageurl.$this->page_variable.'='.($this->current_page+1).'">Suivant</a> &gt; ':'').'.';
79 /**
80 * Return the SQL LIMIT statement for a sql queries
82 public function get_sql_limit_statement() {
83 $lines = $this->limit_by_page;
84 $offset = $this->limit_by_page*($this->current_page-1);
85 return "LIMIT $offset, $lines";
88 protected function set_current_page() {
89 if (empty($_GET['p'])) {
90 $this->current_page = 1;
91 } else {
92 $this->current_page = intval($_GET['p']);
96 public function get_current_page() {
97 return $this->current_page;