-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpager.php
146 lines (133 loc) · 3.9 KB
/
pager.php
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
<?php
namespace modules\html;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of Pager
*
* @author Antonio
*/
class Pager {
//put your code here
private $url, $conn, $page_size = 10, $count ;
public $errors;
public $varSize = array(
'5' => 5,
'10' => 10,
'20' => 20,
'50' => 50,
);
public function __construct($conn, $url = null) {
$this->url = ($url === null)? "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : $url;
$this->conn = $conn;
}
public function setPageSize($s)
{
$this->page_size = $s;
}
protected function getPageSize()
{
return (isset($_GET['pSize']))? (int)$_GET['pSize'] : $this->page_size ;
}
public function setup($sql)
{
$tmp = $sql;
//$sql = str_ireplace('from', ' , count(*) as count_rows from ', $sql);
$sql = preg_replace('/^select(.*)from/is', ' select count(*) as count_rows from ', $sql);
$this->page_size = $this->getPageSize();
if($r = mysql_query($sql, $this->conn))
{
$data = mysql_fetch_row($r);
$this->count = $data[0];
/*$result = $this->conn->Execute($sql);
$this->count = $result->fields['count_rows'];*/
$active = $this->getActivePage();
$tmp .= ' limit '.$active*$this->page_size.', './*($active+1)**/$this->page_size;
return $tmp;
}
$this->errors = mysql_error($this->conn);
return false;
}
private function getActivePage()
{
return (isset($_GET['pNum']))? (int)$_GET['pNum'] : 0;
}
public function render()
{
$start = max(0, ($this->getActivePage() - 10));
$end = min( ($this->count/$this->page_size), ($this->getActivePage() + 10) );
?>
<div id="pageNav">
<ul>
<?
while( $start < $end )
{
?>
<li>
<?
if($start == $this->getActivePage())
{
?>
<div class="active"><? echo $start + 1; ?></div>
<?
}else
{
?>
<div>
<a href="<?echo $this->getUrl($start)?>"><? echo $start + 1; ?></a>
</div>
<?
}
$start++;
?>
</li>
<?
}
?>
</ul>
</div>
<div id="pageSize">
<ul>
<?
foreach($this->varSize as $label => $size)
{
$link = $this->getUrl($this->getActivePage())
?>
<li>
<div>
<a href="<?echo $this->getUrl($this->getActivePage(), $size) ?>"><? echo $label; ?></a>
</div>
</li>
<?
}
?>
</ul>
</div>
<?
}
private function getUrl($page, $size = null)
{
$tmp = $this->url;
$parse = parse_url($this->url);
$fragment = (isset($parse['fragment']))? $parse['fragment'] : '';
$tmp = str_ireplace($fragment, '', $tmp);
if(!empty($parse['query']) )
{
$tmp = preg_replace('/&pNum=([0-9]{1,})/', '', $tmp);
$tmp = preg_replace('/pNum=([0-9]{1,})&/', '', $tmp);
$tmp = preg_replace('/&pSize=([0-9]{1,})/', '', $tmp);
$tmp = preg_replace('/pSize=([0-9]{1,})&/', '', $tmp);
$tmp .= '&pNum='.$page;
}else
{
$tmp .= '?pNum='.$page;
}
$tmp .= '&pSize='.(($size != null)? $size : $this->page_size);
$tmp .= ($fragment)? '#'.$fragment : '';
return $tmp;
}
}
?>