本文实例讲述了php中分页及sqlhelper类用法。分享给大家供大家参考,具体如下:
文档目录结构如下:
sqlhelper.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
|
<?php
/**
* created by jetbrains phpstorm.
* user: lee
* date: 13-7-26
* time: 下午8:30
* to change this template use file | settings | file templates.
*/
class sqlhelper{
private $mysqli ;
private static $host = "localhost" ;
private static $user = "root" ;
private static $pwd = "" ;
private static $db = "world" ;
private $sql =false;
private $result =false;
function __construct(){
$this ->mysqli= new mysqli(self:: $host ,self:: $user ,self:: $pwd ,self:: $db );
if ( $this ->mysqli->connect_error){
die ( "连接数据库失败! " . $this ->mysql->connect_error);
}
$this ->mysqli->query( "set names utf8" );
}
function execute_dql_all( $sql ){
//执行查询语句
$arr = array ();
$this ->result= $this ->mysqli->query( $sql ) or die ( $this ->mysql->connect_error);
//将数据转存到$arr数组中
while ( $row =mysqli_fetch_array( $this ->result,mysql_both)){
$arr []= $row ;
}
$this ->result->free();
return $arr ;
}
function execute_dql_num( $sql ){
//执行查询语句
$arr = array ();
$this ->result= $this ->mysqli->query( $sql ) or die ( $this ->mysql->connect_error);
//将数据转存到$arr数组中
while ( $row =mysqli_fetch_array( $this ->result,mysqli_num)){
$arr []= $row ;
}
$this ->result->free();
return $arr ;
}
function execute_dql_assoc( $sql ){
//执行查询语句
$arr = array ();
$this ->result= $this ->mysqli->query( $sql ) or die ( $this ->mysql->connect_error);
//将数据转存到$arr数组中
while ( $row =mysqli_fetch_array( $this ->result,mysqli_assoc)){
$arr []= $row ;
}
$this ->result->free();
return $arr ;
}
//查询某表中的记录数
function execute_dql_counts( $table , $id = "*" ){
$this ->sql= "select count($id) from $table" ;
$this ->result= $this ->mysqli->query( $this ->sql);
$row =mysqli_fetch_all( $this ->result);
$this ->result->free();
return $row [0][0];
}
function execute_dml( $sql ){
//执行正删改
$this ->result= $this ->mysqli->query( $sql );
if (! $this ->result){
return -1; //执行正删改失败
} else {
if ( $this ->mysqli->affected_rows>0){
return 1; //执行正删改成功,影响行数
} else {
return 0; //执行正删改成功,但没有影响行数
}
}
}
}
|
paging.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
|
<?php
/**
* created by jetbrains phpstorm.
* user: lee
* date: 13-7-27
* time: 下午2:48
* to change this template use file | settings | file templates.
*/
header( "content-type:text/html;charset=utf-8;" );
require_once ( "sqlhelper.php" );
class paging {
private $sqlhelper =false;
private $pagecount =false; //页数
private $counts =false; //总记录数
private $returnarr =false; //分页超链接的分页
function __construct(){
$this ->sqlhelper= new sqlhelper();
$this ->returnarr= array ();
}
/*
* 参数说明
*
* $table 分页时对那个表的数据分页
* $id 辅助查询当前分页的数据表的总记录数
* $pagesize 每页显示多少条信息记录数
* $pagingsize 分页栏每次循环显示出来的个数
* $nowpage 当前是第几页,默认第一页
* $href 分页栏的超链接将要往哪里连接
*/
function paging_prev_next( $table , $id = "*" , $pagesize , $pagingsize , $nowpage =1, $href ){
$this ->counts= $this ->sqlhelper->execute_dql_counts( $table , $id );
$this ->pagecount= ceil ( $this ->counts/ $pagesize );
$this ->returnarr[ "count" ]= $this ->counts;
$this ->returnarr[ "start" ]=( $nowpage -1)* $pagesize ;
$this ->returnarr[ "limit" ]= $pagesize ;
if ( $nowpage > $this ->pagecount || $nowpage <=0){
return false;
}
$t =( ceil ( $nowpage / $pagingsize )-1)* $pagingsize +1;
$pre = $nowpage - $pagingsize ;
$nex = $nowpage + $pagingsize ;
echo "
<span class = 'paging-list-a paging-list-a-withbg' >{ $nowpage }/{ $this ->pagecount}</span>
<a href= '{$href}?nowpage={$pre}' class = 'paging-list-a' ><</a>";
for ( $i = $t ; $i < $t + $pagingsize ; $i ++){
if ( $i * $pagesize > $this ->pagecount* $pagesize ){
break ;
} else {
if ( $nowpage == $i ){
echo "
<a href= '{$href}?nowpage={$i}' class = 'paging-list-a paging-list-a-withbg' >{ $i }</a>";
} else {
echo "
<a href= '{$href}?nowpage={$i}' class = 'paging-list-a' >{ $i }</a>";
}
}
}
echo "
<a href= '{$href}?nowpage={$nex}' class = 'paging-list-a' >></a>";
return $this ->returnarr;
}
}
|
paging-list-link.css代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/**
* created by jetbrains phpstorm.
* user: lee
* date: 13-7-27
* time: 下午5:56
* to change this template use file | settings | file templates.
*/
.paging-list-a{
border:1px solid #b5b5af;
background-color:#efebed;
font-family: 'meiryo ui' ;
font-size: 16px;
font-weight: 600;
padding: 0px 8px 0px 8px;
/*cursor: pointer;*/
text-decoration: none;
color: #292927;
}
.paging-list-a-withbg{
background-color: #1d92e2;
color: white;
}
|
usepaging.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
|
<!doctype html>
<html>
<head>
<title></title>
<link rel= "stylesheet" type= "text/css" href= "paging-list-link.css" >
</head>
<body>
<?php
header( "content-type:text/html;charset=utf-8;" );
require_once 'paging.php' ;
$paging = new paging();
//参数说明
/*
* $table 分页时对那个表的数据分页
* $id 辅助查询当前分页的数据表的总记录数
* $pagesize 每页显示多少条信息记录数
* $pagingsize 分页栏每次循环显示出来的个数
* $nowpage 当前是第几页,默认第一页
* $href 分页栏的超链接将要往哪里连接,当前页链接地址
*/
//控制起始页为
$nowpage =1;
if (isset( $_get [ "nowpage" ])){
$nowpage = $_get [ "nowpage" ];
}
//定义分页所需参数
$meiyexiansi =10;
$meiyelianjieshu =10;
$receivearr = array ();
$receivearr = $paging ->paging_prev_next( "city" , "id" , $meiyexiansi , $meiyelianjieshu , $nowpage , "usepaging.php" );
//容错判断
if (! $receivearr ){
return ;
}
//查询每页需要显示的数据,大小限制存在 $receivearr 数组中
$sqlhelper = new sqlhelper();
$result = $sqlhelper ->execute_dql_num( "select * from city limit " . $receivearr [ 'start' ]. "," . $receivearr [ 'limit' ]. "" );
echo "<pre>" ;
print_r( $result );
echo "</pre>" ;
?>
</body>
</html>
|
所使用的数据库为 mysql5.6 所自带的 world 数据库
下面是运行的效果截图:
不过代码还有个 bug 。就是翻页到最后的时候会出现显示不了,原因在于 paging.php 文件的 41~43 行左右判断有问题。
错误代码如下:
1
2
3
|
if ( $nowpage > $this ->pagecount || $nowpage <=0){
return false;
}
|
希望本文所述对大家php程序设计有所帮助。