Advertisement
Guest User

page.php

a guest
Dec 26th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.48 KB | None | 0 0
  1. <?php
  2. class page {
  3.     public $element_per_page = 10,
  4.         $nb_links = 2,
  5.         $nb_page = 0,
  6.         $page = 1;
  7.  
  8.     public function display_page($data) {
  9.         $this->nb_element = count($data);
  10.         $this->nb_page = ceil($this->nb_element / $this->element_per_page);
  11.  
  12.         if (!empty($_GET['p']) && $_GET['p'] > 0 && $_GET['p'] <= $this->nb_page) {
  13.             $this->page = $_GET['p'];
  14.         }
  15.  
  16.         return $this->getPagination();
  17.     }
  18.  
  19.     public function getPagination() {
  20.  
  21.         $pages = array();
  22.  
  23.         if ($this->page == $this->nb_links+2) {
  24.             $pages[]['value'] = 1;
  25.         } else if($this->page > $this->nb_links+2) {
  26.             $pages[]['value'] = 1;
  27.             $pages[]['value'] = '…';
  28.         }
  29.  
  30.         $start = $this->page-$this->nb_links;
  31.  
  32.         if ($start < 1) {
  33.             $start = 1;
  34.         }
  35.  
  36.         for ($i=$start; $i<=$this->page+$this->nb_links; $i++) {
  37.             if ($i>0 && $i<=$this->nb_page) {
  38.                 $pages[$i]['value'] = $i;
  39.                 if ($this->page == $i) {
  40.                     $pages[$i]['selected'] = 'selected';
  41.                 }
  42.             } else {
  43.                 break;
  44.             }
  45.         }
  46.  
  47.         if ($this->page < $this->nb_page-$this->nb_links-1) {
  48.             $pages[]['value'] = '…';
  49.         }
  50.  
  51.         if ($this->page <= $this->nb_page-($this->nb_links+1)) {
  52.             $pages[]['value'] = $this->nb_page;
  53.         }
  54.  
  55.         return $pages;
  56.  
  57.     }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement