article

Saturday, September 7, 2013

Pagenation using php class

Pagenation using php class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
class paginate{
var $current;
var $rows;
var $start_row;
var $total_data;
 function paginate($current_page,$totaldata,$rows_page = 10){
  $this->rows = $rows_page;
  $this->total_data = $totaldata;
  if($current_page < 1 or $current_page > $this->getTotalPage()){
   $this->current = 1;
  }
  else{
   $this->current = $current_page;
  }
 }
 function getTotalPage(){
  $total = ceil($this->total_data / $this->rows);
  return $total;
 }
 function getLimit(){
  if($this->current <= 1){
   $this->start_row = 0;
  }
  else{
   $this->start_row = $this->rows*($this->current-1);
  }
  return ' LIMIT '.$this->start_row.','.$this->rows;
 }
 function getNext(){
  if($this->current < $this->getTotalPage()){
   return $this->current + 1;
  }
 }
 function getPrevious(){
  if($this->current > 1){
   return $this->current - 1;
  }
 }
 function getPages(){
  $first = false; $last = false;
  if($this->getTotalPage() == 0){
   return false;
  }
  elseif($this->getTotalPage() > 10){
   for($i=1; $i<=$this->getTotalPage();$i++){
    if($i == $this->current){
     $page[] = array(
      'link' => false,
      'page' => $i
     );
    }
    elseif($i < $this->current-3 and $i > 3 and $i < $this->current+5 ){
     if(!$first){
      $page[] = array(
       'link' => false,
       'page' => '...',
      );
     }
     $first = true;
    }
    elseif($i < $this->getTotalPage()-3 and $i > $this->current+5){
     if(!$last){
      $page[] = array(
       'link' => false,
       'page' => '...',
      );
     }
     $last = true;
    }
    else{
     $page[] = array(
      'link' => true,
      'page' => $i
     );
    }
   }
  }
  else{
   for($i=1; $i<= $this->getTotalPage();$i++){
    if($i == $this->current){
     $page[] = array(
      'link' => false,
      'page' => $i
     );
    }
    else{
     $page[] = array(
      'link' => true,
      'page' => $i
     );
    }
   }
  }
  return $page;
 }
 function navigation($url=null,$attr=array()){
  $parm = '';
  if(is_array($attr)){
   foreach($attr as $name => $val){
    $parm .= ' '.$name.'="'.$val.'"';
   }
  }
  $nav = null;
  if($this->getPrevious()){
   $nav .= '<a href="'.$url.'&page='.$this->getPrevious().'" '.$parm.'>Previous</a> | ';
  }
  if($this->getPages()){
   foreach($this->getPages() as $name => $val){
    if($val['page'] != 1){
     $nav .= ', ';
    }
    if($val['link']){
     $nav .= '<a href="'.$url.'&page='.$val['page'].'" '.$parm.'>'.$val['page'].'</a>';
    }
    else{
     $nav .= '<b>'.$val['page'].'</b>';
    }
   }
  }
  if($this->getNext()){
   $nav .= ' | <a href="'.$url.'&page='.$this->getNext().'" '.$parm.'>Next</a>';
  }
  return $nav;
 }
}
 
 
/** example **/
if(isset($_GET['page'])){
 $page = $_GET['page'];
}
else{
 $page = 1;
}
$totaldata = 200;
$row_per_page = 10;
$paginate = new paginate($page,$totaldata,$row_per_page);
 
echo 'Limit for Query: SELECT * FROM `table`'.$paginate->getLimit();
echo '<br/>';
echo 'Current Page: '.$paginate->current;
echo '<br/>';
echo 'Pages Navigation: '.$paginate->navigation('index.php?',array('id' => 'page'));
echo '<br/>';
echo 'Total Page: '.$paginate->getTotalPage();
?>

Related Post