自己写的一个分页函数,可以使用

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
<?php
 
/**
* DEMO10
* 功能:分页
* author:wenzl
* 分页中心方法DEMO
* 功能智能提供数字分页
* $page 当前页码
* $pageSize 每页数据条数
* $dataTotal 数据总数
* | $number 设置可视分页项数
* | [1][2][3][4][5][6]
*/
function getPage($page=1,$pageSize=5,$dataTotal=100,$number=5){
    $pageCnt = ceil($dataTotal/$pageSize);//总页数
    $groupCnt = ceil($pageCnt/$number);
    $k=1;  
    $pageTemp = array();
    for($i=1;$i<=$groupCnt;$i++){
        if($i==1){
            for ($j=1; $j<=$number+1; $j++) {
                $k<=$pageCnt &&
                $pageTemp[$i][$j] = $k++;
            }
            $k--;
        }else{
            for ($j=1; $j<=$number+2; $j++) {
                $k<=$pageCnt &&
                $pageTemp[$i][$j] = $k++;
            }
            $k--;
        }
        $k--;
    }
    $bs = floor($page/$number);
    $ys = $page%$number>=1?1:0;
    $step = $bs + $ys;
    $step = $ys == 0 ? $bs : $step;
 
     
    $prevPage = isset($page) &&
    $page <= 1 ? 1 : $page - 1 ;
     
    $nextPage = isset($page) &&
    $page > $pageCnt ? $pageCnt : $page + 1;
    /**
    * 分页结果
    */
    return array(
            ‘page‘=>$page,
            ‘number‘=>$number,
            ‘pageCnt‘=>$pageCnt,
            ‘pageTemp‘=>$pageTemp[$step],
            ‘prevPage‘=>$prevPage,
            ‘pageSize‘=>$pageSize,
            ‘nextPage‘=>$nextPage,
            ‘groupCnt‘=>$groupCnt,
            ‘dataTotal‘=>$dataTotal,
        );
}
 
 
// echo "<pre>";
// print_r(getPage(10));
// echo "</pre>";
 
 
/**
* 分页用例
*/
 
$page = isset($_GET[‘page‘]) ? $_GET[‘page‘] : 1;
$pageSize = 5;
$dataTotal = 120;
$number = 6 ;
$pageArray = getPage($page,$pageSize,$dataTotal,$number);
 
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>
    <style>
    .page ul{
        list-style: none;      
    }
    .page ul li{
        display: inline-block;
        width: 14px;
        height: 14px;
        line-height: 14px;
        border:1px solid #C80000;
        padding:3px;
        color: #C80000;
        cursor: pointer;
        text-align: center;
    }
    </style>
</head>
<body>
    <div class="page">
        <ul>
            <li><a href="?page=1"><<</a></li>
            <li><a href="?page=<?php echo $pageArray[‘prevPage‘];?>"><</a></li>
            <?php foreach ($pageArray[‘pageTemp‘] as $key => $p) {?>              
            <li>
                <?php if($page != $p):?>
                <a href="?page=<?php echo $p;?>"><?php echo $p; ?></a>
                <?php else:?>
                <b><?php echo $p; ?></b>
                <?php endif;?>
            </li>
            <?php }?>
            <li><a href="?page=<?php echo $pageArray[‘nextPage‘];?>">></a></li>
            <li><a href="?page=<?php echo $pageArray[‘pageCnt‘];?>">>></a></li>
        </ul>
    </div>
</body>
</html>

自己写的一个分页函数,可以使用

上一篇:弹出窗口zDialog的使用


下一篇:socket相关的开机初始化