Frenlee

分页的处理

在Laruence 的博客中看到一篇关于 一个巧妙的分页方法 的文章,然后自己实践了一下,发现之中有一些不足的地方,就是在开始与结束的时候显示的页数不是自己设定的页数,而是页数的一半+1,在翻页过程中会出现显示的页数是不同的。

本实例在原有的程序上加以改进,使翻页时显示的也是使一个固定的值。

这里对于其基本的原理不做赘述,只是稍微修改了起始页和结束页的值,读者可以先看看 Laruence 的这个分页方法。下面就直接贴出代码:

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
<?php
/**
* <span class='wp_keywordlink_affiliate'><a href="http://blog.frenlee.cn/archives/tag/%e5%88%86%e9%a1%b5" title="View all posts in 分页" target="_blank">分页</a></span>显示
* Author: frli <frenlee@163.com>
* Sence: 2015/12/8 21:44
*/

class Pagination {
private $page,//当前页数
$total_page, //总页数
$total,//数据总条数
$page_size,//每页显示套数
$size;//显示的页码数

private $prev_str = "&lt",$next_str = "&gt";

private $class,
$selected_class = "selected",
$prev_class="prev",
$next_class="next",
$de_prev_class="de_prev",
$de_next_class="de_next";

private $url_prefix = "",$splite_char = "?";
private $param_name = "page", $target = "";

public function Pagination($url_prefix, $page_size, $total, $size=10, $conf = array())
{
$this->page = 1;
$this->page_size = $page_size;
$this->total = $total;
$this->total_page = intval(ceil($total/$page_size));
$this->size = $size;
if (!empty($conf)) {
$configure = array('prev_str', 'next_str', "class", "selected_class");
foreach ($conf as $key => $val) {
if(in_array($key, $configure)) {
$this->$val = $val;
}
}
}

$this->url_prefix = $url_prefix;
if (strstr($url_prefix, '?') !== false) {
$this->url_prefix .= '&' . $this->param_name . '=';
} else {
$this->url_prefix .= '?' . $this->param_name . '=';
}
}

public function generate($page)
{
$this->page = $page;
if (isset($this->page_str[$page])) {
return $this->page_str[$page];
}
$page_start = 1;
$half = intval($this->size/2);//取中间值
$page_start = max(1, $page-$half);//开始值
$page_end = min($page + $half, $this->total_page);//终止值
$page_start = ($page + $half) >= $this->total_page ? $this->total_page - $this->size : $page_start;//当前页数+显示页数的一半大于总页数 则令开始页等于总页数-显示页数
$page_end = $page_start <= $half ? $page_start + $this->size : $page_end;//当开始页为1时 令终止页等于开始页+显示也是
//保持显示页数为size+1页

$this->page_str[$page] = $this->build_nav_str($page_start, $page_end);
return $this->page_str[$page];
}

private function build_nav_str($page_start, $page_end)
{
$page_nums = range($page_start, $page_end);
$this->target = $this->target ? " target=\"{$this->target}\"" : '';
if ($this->page == 1) {
$page_str = <<<HTML
<span class="{$this->de_prev_class}">{$this->prev_str}</span>
HTML;
} else {
$page = $this->page - 1;
$page_str = <<<HTML
<span class="{$this->prev_class}">
<a href="{$this->url_prefix}{$page}"{$this->target}>{$this->prev_str}</a>
</span>
HTML;
}

foreach ($page_nums as $p) {
$page_str .= ($p == $this->page) ? <<<HTML
<span class="{$this->selected_class}">{$p}</span>
HTML
: <<<HTML
<span class="{$this->class}">
<a href="{$this->url_prefix}{$p}"{$this->target}>{$p}</a>
</span>
HTML;
}
if ($this->page == $this->total_page) {
$page_str .= <<<HTML
<span class="{$this->de_next_class}">{$this->next_str}</span>
HTML;
} else {
$page = $this->page + 1;
$page_str .= <<<HTML
<span class="{$this->next_class}"><a href="{$this->url_prefix}{$page}"{$this->target}>{$this->next_str}</a></span>
HTML;
}
return $page_str;
}

public function tidy_str()
{
;//void
}

public function __call($func_name, $arguments)
{
if (isset($this->$func_name)) {
return $this->$func_name;
}
}

public function __destruct()
{
unset($this->page_str);
unset($this);
}
}

$page = isset($_GET['page']) ? $_GET['page'] : 1;
$pagination = new Pagination('?', 10, 236, 10);
$str = $pagination->generate($page);
echo $str;

在程序中稍微改动的地方就只有下面这两句。

1
2
$page_start = ($page + $half) >= $this->total_page ? $this->total_page - $this->size : $page_start;//当前页数+显示页数的一半大于总页数 则令开始页等于总页数-显示页数
$page_end = $page_start <= $half ? $page_start + $this->size : $page_end;//当开始页为1时 令终止页等于开始页+显示也是