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
<?php
 
//=============================================================
//=== Class: pagination
//=== Dependancies: none 
//=== Author: VBAssassin ( www.coderprofile.com/coder/VBAssassin ) 
//=== Version: 1.00.00
//=== PHP version: 5.X
//=== License: GNU
//=== Description: takes care of the hard work when creating pagination and can carry across variables between pages in the pages URL. Multiple instances of pagination can be used on a single page.
//=============================================================
class pagination {
       
       //=============================================================
       //=== GLOBAL PROPERTIES
       //=============================================================
       private $instance = '';
       private $total_per_page = 1;
       private $total_items = 1;
       private $total_pages = 1;
       private $current_page = 1;
       private $query_string = '';              //does not include a ? character
       
       //=============================================================
       //=== SET THE MAIN PROPERTIES
       //=============================================================
       function __construct($instance, $total_per_page, $total_items) {
              
              //Add some basic vars to the object
              $this->instance = $instance;
              $this->total_per_page = $total_per_page;
              $this->total_items = $total_items;
              
              //calculate some more basic vars
              $this->total_pages = ceil($total_items / $total_per_page);
              $this->current_page = isset($_GET[$this->instance]) ? intval($_GET[$this->instance]) : 1;
              
              //check that the current page is not over the max pages
              if ($this->current_page > $this->total_pages) {
                     $this->current_page = $this->total_pages;
              }
              
              //check that the current page is not over the max pages
              if ($this->current_page < 1) {
                     $this->current_page = 1;
              }
              
              //generate the query string
              $this->query_string = $this->get_query_string();
              
       }
       
       //=============================================================
       //=== GET THE LIMIT NEEDED FOR SQL DATABASES
       //=============================================================
       function get_sql_limit() {
              return (($this->current_page-1) * $this->total_per_page) . ',' . $this->total_per_page;
       }
       
       //=============================================================
       //=== GENERATE THE HTML PAGINATION FOR PEOPLE TO CLICK
       //=============================================================
       function get_pagination() {
              
              //store the generated html
              $pages = '';
              
              //generate the html for the pagination
              for ($counter = 1; $counter <= $this->total_pages; $counter++) {
                     
                     //see if this page is currently selected
                     $selected_style = ($this->current_page == $counter ? ' style="font-weight:bold;color:green;"' : '');
                     
                     //put together the html for this page number
                     if ($counter == $this->total_pages) {
                            //last page number
                            $pages .= "<a href='{$_SERVER['PHP_SELF']}?{$this->query_string}{$this->instance}={$counter}'{$selected_style}>{$counter}</a>";
                            } else {
                            //more pages to come...
                            $pages .= "<a href='{$_SERVER['PHP_SELF']}?{$this->query_string}{$this->instance}={$counter}'{$selected_style}>{$counter}</a>, ";
                     }
                     
              }
              
              //return the generated html for the pagination
              return $pages;
              
       }
       
       //=============================================================
       //=== GETS THE QUERY STRING WITHOUT THE PAGINATION VARS
       //=============================================================
       private function get_query_string() {
              
              //get all the params
              $query_string = '';
              foreach ($_GET as $key=>$value) {
                     if ($key != $this->instance) {
                            $query_string .= "{$key}={$value}&";
                     }
              }
              return $query_string;
              
       }
       
}
 
/*
 
//Example of how to use this class
 
$pagination = new pagination('page', 25, 500);
//mysql_query("SELECT * FROM `table` WHERE `something` = '2' LIMIT " . $pagination->get_sql_limit());
print $pagination->get_pagination();
 
print '<br /><br />';
 
$pagination = new pagination('page_again', 25, 500);
//mysql_query("SELECT * FROM `table` WHERE `something` = '2' LIMIT " . $pagination->get_sql_limit());
print $pagination->get_pagination();
 
*/
 
?>