php 分页类怎么用啊?
发布网友
发布时间:2022-05-01 18:32
我来回答
共5个回答
懂视网
时间:2022-05-01 22:53
<?php
2 class SqlHelper{
3 private $conn;
4 private static $host="localhost";
5 private static $user="root";
6 private static $password="";
7 private static $db="emp";
8 //构造函数 完成初始化的任务
9 public function __construct(){
10 $this->conn= new mysqli(self::$host,self::$user,self::$password,self::$db);
11 if ($this->conn->connect_error){
12 die("连接失败".$this->conn->connect_error);
13
14 }
15 $this->conn->query("set names utf8");
16
17 }
18
19
20
21 //执行dql查询语句
22 public function execut_dql($sql){
23 $res=$this->conn->query($sql) or
24 die(‘sql语句错误‘.$this->conn->error);
25
26 //关闭连接
27 $this->conn->close();
28
29 return $res;
30 //释放资源
31 $res->free();
32
33 }
34 //执行dml语句
35 public function execut_dml($sql){
36
37 //操作$sql
38 $res=$this->conn->query($sql) or die("sql语句错误".$this->conn->error);
39
40
41 if (!$res){
42 return 0; //操作失败
43 }else{
44 if($this->conn->affected_rows>0){
45 return 1; //操作成功
46 }else {
47 return 2; //没有受影响的行数
48 }
49
50
51
52 }
53 //关闭连接
54 $this->conn->close();
55 //释放资源
56 $res->free();
57
58 }
59 public function execut_dql2($id){
60 $res=$this->conn->query($id) or die("sql语句错误".$this->conn->error);;
61 //查询的结果放进数组中
62 $emps=array();
63
64 while($row=$res->fetch_assoc()){
65 $emps[]=$row;
66
67 }
68
69 //关闭资源
70 $res->free();
71 //关闭连接
72 $this->conn->close();
73 return $emps;
74 }
75 //执行分页的的sql语句,并赋值给fenyepage类
76 public function execut_dql_fenye($sql1,$sql2,$fenyePage){
77 //执行要查询的语句 $res1返回一个结果集
78 $res1=$this->conn->query($sql1) or die("sql语句错误".$this->conn->error);
79 //用数组接收取出的值
80 $arr= array();
81 //取出结果集每一行,转移到数组中
82 while ($row=$res1->fetch_assoc()){
83 $arr[]=$row;
84 }
85 //释放资源
86 $res1->free();
87
88 $fenyePage->arr=$arr;
89
90 $res2=$this->conn->query($sql2) or die("sql语句错误".$this->conn->error);
91
92 $count=$res2->fetch_assoc();
93 $rowCount=$count[‘c‘];
94
95 $fenyePage->rowCount=$rowCount;
96 $fenyePage->pageCount=ceil($rowCount/$fenyePage->pageSize);
97 //释放资源
98 $res2->free();
99 //关闭连接
100 $this->conn->close();
101 //如果点击当前页大于1使用当前页-1,小于使用1
102 $prev= $fenyePage->pageNow>1 ? $fenyePage->pageNow-1:1;
103 //如果点击当前页小于总页,当前页加一,否则取总页
104 $next= $fenyePage->pageNow<$fenyePage->pageCount ? $fenyePage->pageNow+1:$fenyePage->pageCount;
105
106 $pageMix=$fenyePage->pageNow-$fenyePage->pageSpa<1 ? 1:$fenyePage->pageNow-$fenyePage->pageSpa;
107 $pageMax=$fenyePage->pageNow+$fenyePage->pageSpa-1<10 ? 10:$fenyePage->pageNow+$fenyePage->pageSpa-1;
108 $pageMaxd= $pageMax>$fenyePage->pageCount?$fenyePage->pageCount:$pageMax;
109 //首页
110 $fenyePage->naviga="<a href=‘$fenyePage->url?pageNow=1‘>首页</a> ";
111 //快速翻页,如果点击的页面小于10,隐藏
112 if($fenyePage->pageNow>10){
113 $fenyePage->naviga.="<a href=‘$fenyePage->url?pageNow=".($pageMix-5)."‘><<</a> ";
114 }
115 if($fenyePage->pageNow!=1){
116
117 //页面不等于1显示页面,否则不显示上一页
118
119 $fenyePage->naviga.= "<a href=‘$fenyePage->url?pageNow=$prev‘>上一页</a> ";
120 }
121
122
123 for($pageMix;$pageMix<=$pageMaxd;$pageMix++){
124
125 $fenyePage->naviga.="<a href=‘$fenyePage->url?pageNow=$pageMix‘>$pageMix</a> ";
126
127 }
128 if($fenyePage->pageNow < $fenyePage->pageCount){
129 $fenyePage->naviga.="<a href=‘$fenyePage->url?pageNow=$next‘>下一页</a> ";
130 }
131 if($fenyePage->pageNow+5<$fenyePage->pageCount){
132 $fenyePage->naviga.="<a href=‘$fenyePage->url?pageNow=".($pageMaxd+$fenyePage->pageSpa+1)."‘>>></a> ";
133 }
134 $fenyePage->naviga.="<a href=‘$fenyePage->url?pageNow=$fenyePage->pageCount‘>末页</a> ";
135 $fenyePage->naviga.="当前第{$fenyePage->pageNow}页/总共{$fenyePage->pageCount}页<br/><br/>";
136 }
137
138
139 }
140 ?>
SqlHelper.class.php+分页类方法
标签:const 返回 接收 div 查询 utf8 没有 ons 点击
热心网友
时间:2022-05-01 20:01
<?php
class Page {
private $total_rows;//数据库总条数
private $per_page_rows;//每页显示条数
private $limit;
private $uri;
private $total_pages;//总页数
private $config=array("header"=>"记录条数","prev"=>"上一页","next"=>"下一页","first"=>"首 页","last"=>"尾 页");
private $list_length=8;
public function __construct($total_rows,$per_page_rows=10,$url_args){
$this->total_rows=$total_rows;
$this->per_page_rows=$per_page_rows;
$this->uri=$this->get_uri($url_args);
$this->page = !empty($_GET['page']) ? $_GET['page'] : 1;
$this->total_pages=ceil($this->total_rows/$this->per_page_rows);
$this->limit=$this->set_limit();
}
private function set_limit() {
return "limit ".($this->page-1)*$this->per_page_rows.",{$this->per_page_rows}";
}
private function get_uri($url_args) {
$url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"],"?") ? "" : "?").$url_args;
$parse=parse_url($url);
if (isset($parse['query'])) {
parse_str($parse['query'],$params);//把url字符串解析为数组
unset($params['page']);//删除数组下标为page的值
$url=$parse['path'].'?'.http_build_query($params);//再次构建url
}
return $url;
}
public function __get($args) {
if ($args=="limit") {
return $this->limit;
}else{
return null;
}
}
private function start_page(){
if ($this->total_rows==0) {
return 0;
}else{
return (($this->page-1)*$this->per_page_rows)+1;
}
}
private function end_page(){
return min($this->page*$this->per_page_rows,$this->total_rows);
}
private function go_first() {
$html="";
if ($this->page==1) {
$html.=" {$this->config['first']} ";
}else{
$html.=" <a href='{$this->uri}&page=1'>{$this->config['first']}</a> ";
}
return $html;
}
private function go_prev() {
$html="";
if ($this->page==1) {
$html.=" {$this->config['prev']} ";
}else{
$html.=" <a href='{$this->uri}&page={$this->page}-1'>{$this->config['prev']}</a> ";
}
return $html;
}
private function go_next() {
$html="";
if ($this->page==$this->total_pages) {
$html.=" {$this->config['next']} ";
}else{
$html.=" <a href='{$this->uri}&page={$this->page}+1'>{$this->config['next']}</a> ";
}
return $html;
}
private function go_last() {
$html="";
if ($this->page==$this->total_pages) {
$html.=" {$this->config['last']} ";
}else{
$html.=" <a href='{$this->uri}&page={$this->total_pages}'>{$this->config['last']}</a> ";
}
return $html;
}
private function go_page() {
return ' <input type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->total_pages.')?'.$this->total_pages.':this.value;location=\''.$this->uri.'&page=\'+page+\'\'}" value="'.$this->page.'" style="width:25px;" /><input type="button" onclick="javascript:var page=(this.previousSibling.value>'.$this->total_pages.')?'.$this->total_pages.':this.previousSibling.value;location=\''.$this->uri.'&page=\'+page+\'\'" value="GO" /> ';
}
private function page_list() {
$link_page="";
$i_num=floor($this->list_length/2);
for ($i = $i_num; $i >= 1; $i--) {
$page=$this->page-$i;
if ($page<1) {
continue;
}else{
$link_page.=" <a href='{$this->uri}&page={$page}'>{$page}</a> ";
}
}
$link_page.=" {$this->page} ";
for ($i = 1; $i < $i_num; $i++) {
$page=$this->page+$i;
if ($page<=$this->total_pages) {
$link_page.=" <a href='{$this->uri}&page={$page}'>{$page}</a> ";
}else{
break;
}
}
return $link_page;
}
public function out_page($display=array(0,1,2,3,4,5,6,7,8)) {
$display_html='';
$html[0]=" 共有<b>{$this->total_rows}</b>{$this->config['header']} ";
$html[1]=" 每页显示<b>".($this->end_page()-$this->start_page()+1)."</b>条,本页显示从<b>{$this->start_page()}</b>--<b>{$this->end_page()}</b>{$this->config['header']} ";
$html[2]=" <b>{$this->page}</b>/<b>{$this->total_pages}</b>页 ";
$html[3]=$this->go_first();
$html[4]=$this->go_prev();
$html[5]=$this->page_list();
$html[6]=$this->go_next();
$html[7]=$this->go_last();
$html[8]=$this->go_page();
foreach ($display as $index){
$display_html.=$html[$index];
}
return $display_html;
}
}
?>
热心网友
时间:2022-05-01 21:19
分页的基本原理,就是在SQL语句后面添加limit,使得范围满足条件数据的第几页,在你的下面两句之间做手足:
$page=$_GET['page'];
$sql="select * from forum1 where fid=$id order by lasttime desc";
一般可以这样:
$page=$_GET['page'];
$start=($page-1)*20;
$sql="select * from forum1 where fid=$id order by lasttime desc limit $start, 20";
但是上面的只是雏形,完善的情况要判断参数范围,或者页大小可以调整,我相信你明白了上面的道理,能做出完美的。
热心网友
时间:2022-05-01 22:54
可以到网上下载一个实例自己慢慢研究一下。蛮简单的。
热心网友
时间:2022-05-02 00:45
建议自己写最好,有不懂的可以M我哦。