|
1
|
|
|
<?php namespace EvolutionCMS\Support; |
|
2
|
|
|
|
|
3
|
|
|
use EvolutionCMS\Interfaces\PaginginateInterface; |
|
4
|
|
|
|
|
5
|
|
|
// ===================================================== |
|
6
|
|
|
// FILE: Paging.php |
|
7
|
|
|
// |
|
8
|
|
|
// ===================================================== |
|
9
|
|
|
// Description: This class handles the paging from a query to be print |
|
10
|
|
|
// to the browser. You can customize it to your needs. |
|
11
|
|
|
// |
|
12
|
|
|
// This is distribute as is. Free of use and free to do anything you want. |
|
13
|
|
|
// |
|
14
|
|
|
// PLEASE REPORT ANY BUG TO ME BY EMAIL :) |
|
15
|
|
|
// |
|
16
|
|
|
// ========================= |
|
17
|
|
|
// Programmer: Pierre-Yves Lemaire |
|
18
|
|
|
// [email protected] |
|
19
|
|
|
// ========================= |
|
20
|
|
|
// Date: 2001-03-25 |
|
21
|
|
|
// Version: 2.0 |
|
22
|
|
|
// |
|
23
|
|
|
// Modif: |
|
24
|
|
|
// Version 1.1 (2001-04-09) Remove 3 lines in getNumberOfPage() that were forgot after debugging |
|
25
|
|
|
// Version 1.1 (2001-04-09) Modification to the exemple |
|
26
|
|
|
// Version 1.1 (2001-04-10) Added more argv to the previous and next link. ( by: [email protected] ) |
|
27
|
|
|
|
|
28
|
|
|
// Version 2.0 (2001-11-22) Complete re-write of the script |
|
29
|
|
|
// Summary: The class will be make it easier to play with results... |
|
30
|
|
|
// * The class now only returns 2 arrays. All HTML, except href, tag were remove. |
|
31
|
|
|
// * Function printPaging() broken in two: getPagingArray() and getPagingRowArray() |
|
32
|
|
|
// * Function openTable() and closeTable() removed. |
|
33
|
|
|
// ===================================================== |
|
34
|
|
|
|
|
35
|
|
|
class Paginate implements PaginginateInterface |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Number of result to show per page (decided by user) |
|
40
|
|
|
* |
|
41
|
|
|
* @var int |
|
42
|
|
|
*/ |
|
43
|
|
|
public $int_num_result; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Total number of items (SQL count from db) |
|
47
|
|
|
* |
|
48
|
|
|
* @var int |
|
49
|
|
|
*/ |
|
50
|
|
|
public $int_nbr_row; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Current position in recordset |
|
54
|
|
|
* |
|
55
|
|
|
* @var int |
|
56
|
|
|
*/ |
|
57
|
|
|
public $int_cur_position; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Extra argv of query string |
|
61
|
|
|
* |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
public $str_ext_argv; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Paging constructor. |
|
68
|
|
|
* @param int $int_nbr_row |
|
69
|
|
|
* @param int $int_cur_position |
|
70
|
|
|
* @param int $int_num_result |
|
71
|
|
|
* @param string $str_ext_argv |
|
72
|
|
|
*/ |
|
73
|
|
|
public function __construct($int_nbr_row, $int_cur_position, $int_num_result, $str_ext_argv = "") |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
|
|
$this->int_nbr_row = $int_nbr_row; |
|
76
|
|
|
$this->int_num_result = $int_num_result; |
|
77
|
|
|
$this->int_cur_position = $int_cur_position; |
|
78
|
|
|
$this->str_ext_argv = urldecode($str_ext_argv); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* This function print the paging to the screen. |
|
83
|
|
|
* This function returns an array: |
|
84
|
|
|
* $array_paging['lower'] lower limit of where we are in result set |
|
85
|
|
|
* $array_paging['upper'] upper limit of where we are in result set |
|
86
|
|
|
* $array_paging['total'] total number of result |
|
87
|
|
|
* $array_paging['previous_link'] href tag for previous link |
|
88
|
|
|
* $array_paging['next_link'] href tag for next link |
|
89
|
|
|
* |
|
90
|
|
|
* @return array |
|
|
|
|
|
|
91
|
|
|
*/ |
|
92
|
|
|
public function getPagingArray() |
|
93
|
|
|
{ |
|
94
|
|
|
global $PHP_SELF; |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
$array_paging['lower'] = ($this->int_cur_position + 1); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
if ($this->int_cur_position + $this->int_num_result >= $this->int_nbr_row) { |
|
99
|
|
|
$array_paging['upper'] = $this->int_nbr_row; |
|
100
|
|
|
} else { |
|
101
|
|
|
$array_paging['upper'] = ($this->int_cur_position + $this->int_num_result); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$array_paging['total'] = $this->int_nbr_row; |
|
105
|
|
|
|
|
106
|
|
|
if ($this->int_cur_position != 0) { |
|
107
|
|
|
$array_paging['first_link'] = "<a href=\"$PHP_SELF?int_cur_position=0" . $this->str_ext_argv . "\">"; |
|
108
|
|
|
$array_paging['previous_link'] = "<a href=\"$PHP_SELF?int_cur_position=" . ($this->int_cur_position - $this->int_num_result) . $this->str_ext_argv . "\">"; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if (($this->int_nbr_row - $this->int_cur_position) > $this->int_num_result) { |
|
112
|
|
|
$int_new_position = $this->int_cur_position + $this->int_num_result; |
|
113
|
|
|
$array_paging['last_link'] = "<a href=\"$PHP_SELF?int_cur_position=" . $this->int_nbr_row . $this->str_ext_argv . "\">"; |
|
114
|
|
|
$array_paging['next_link'] = "<a href=\"$PHP_SELF?int_cur_position=$int_new_position" . $this->str_ext_argv . "\">"; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $array_paging; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* This function returns an array of string (href link with the page number) |
|
122
|
|
|
* |
|
123
|
|
|
* @return array |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getPagingRowArray() |
|
126
|
|
|
{ |
|
127
|
|
|
global $PHP_SELF; |
|
|
|
|
|
|
128
|
|
|
$array_all_page = array(); |
|
129
|
|
|
for ($i = 0; $i < $this->getNumberOfPage(); $i++) { |
|
130
|
|
|
// if current page, do not make a link |
|
131
|
|
|
if ($i == $this->getCurrentPage()) { |
|
132
|
|
|
$array_all_page[$i] = "<b>" . ($i + 1) . "</b> "; |
|
133
|
|
|
} else { |
|
134
|
|
|
$int_new_position = ($i * $this->int_num_result); |
|
135
|
|
|
$array_all_page[$i] = "<a href=\"" . $PHP_SELF . "?int_cur_position=$int_new_position$this->str_ext_argv\">" . ($i + 1) . "</a> "; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $array_all_page; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* This function returns the total number of page to display. |
|
144
|
|
|
* |
|
145
|
|
|
* @return float|int |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getNumberOfPage() |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->int_nbr_row / $this->int_num_result; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* This function returns the current page number. |
|
154
|
|
|
* |
|
155
|
|
|
* @return int |
|
|
|
|
|
|
156
|
|
|
*/ |
|
157
|
|
|
public function getCurrentPage() |
|
158
|
|
|
{ |
|
159
|
|
|
$int_cur_page = ($this->int_cur_position * $this->getNumberOfPage()) / $this->int_nbr_row; |
|
160
|
|
|
|
|
161
|
|
|
return number_format($int_cur_page, 0); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.