您的位置:首页 > 编程语言 > PHP开发

PHP实现分页效果

2017-05-14 19:50 399 查看
写了一个php的分页类,仅供参考

原理:使用mysql中的limit关键字

语法:select * from you_table limit start_number,number;

该语句的意思是从第start_number条数据开始,共查询number条数据。

下面是代码:

page.class.php

<?php
class Page {
private $total;    //数据表中总记录数
private $listRows; //每页显示行数
public $limit;    //分页语句
private $uri;      //url地址
private $pageNum;  //页数
private $listNum=7;//最多显示的页数
private $page;//当前页

/*
构造方法:对分类进行初始化
$total:记录总数
$listRows:每页显示多少条数据
$init:初始化在第几页
*/
public function __construct($total,$listRows=10,$init="") {
$this->total = $total;
$this->listRows = $listRows;
$this->uri = $this->getUri($init);
$this->page=!empty($_GET["page"]) ? $_GET["page"] : 1;
$this->pageNum=ceil($this->total/$this->listRows);
$this->limit=$this->setLimit();

}

/*
使用Limit进行分页

4000
*/
private function setLimit() {
return "Limit ".($this->page-1)*$this->listRows.", {$this->listRows}";
}
/*
获取地址栏的URL方法
这里不能直接用$_SERVER['REQUEST_URI']获取,
如果用$_SERVER['REQUEST_URI']获取会导致地址栏上有很多的参数
*/
private function getUri($init) {
$url = $_SERVER['REQUEST_URI'].(strpos($_SERVER["REQUEST_URI"], '?')?'':"?").$init;
$parse=parse_url($url);
if(isset($parse["query"])) {
parse_str($parse['query'],$params);
unset($params["page"]);
$url=$parse['path'].'?'.http_build_query($params);
}
return $url;
}

/*
首页
*/
private function first() {
$html = "";
if($this->page==1)
$html .='';
else
$html .="  <a href='{$this->uri}page=1'>首页</a>  ";
return $html;
}
/*
上一页
*/
private function prev() {
$html = "";
if($this->page == 1) {
$html.="";
} else {
$html .="  <a href='{$this->uri}page=".($this->page-1)
."'>上一页</a>  ";
}
return $html;
}

/*
点击页
*/
private function pageList() {

$linkPage="";
$num = floor($this->listNum/2);
for($i=$num; $i>=1; $i--) {
$page = $this->page - $i;
if($page<1) {
continue;
}
$linkPage.=" <a href='{$this->uri}page={$page}'>{$page}</a> ";
}
$linkPage.=" <span style='background:red;'>".$this->page."</span> ";
for($i=1; $i<=$num; $i++){
$page=$this->page+$i;
if($page<=$this->pageNum)
$linkPage.=" <a href='{$this->uri}page={$page}'>{$page}</a> ";
else
break;
}
return $linkPage;
}

/*
下一页
*/
private function next(){
$html = "";
if($this->page==$this->pageNum)
$html.='';
else
$html.="  <a href='{$this->uri}page=".($this->page+1)
."'>下一页</a>  ";
return $html;
}
/*
尾页
*/
private function last(){
$html = "";
if($this->page==$this->pageNum)
$html.='';
else
$html.="  <a href='{$this->uri}page=".($this->pageNum)
."'>尾页</a>  ";
return $html;
}
/*

*/
private function goPage(){
return '  <input type="text"
onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'
.$this->pageNum.')?'.$this->pageNum.':this.value;
location=\''.$this->uri.'page=\'+page+\'\'}"
value="'.$this->page.'" style="width:60px;text-align:center;">'
.'<input type="button" value="GO"
onclick="javascript:var page=(this.previousSibling.value>'
.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;
location=\''.$this->uri.'page=\'+page+\'\'">  ';
}
public function fpage() {
$fpage='';
$fpage.=$this->first();
$fpage.=$this->prev();
$fpage.=$this->pageList();
$fpage.=$this->next();
$fpage.=$this->last();
$fpage.=$this->goPage();
return $fpage;
}

}


使用page.class.php分页类

page.php

<?php
header("content-type:text/html;charset=utf-8");

$link = mysql_connect('localhost','root','root');
mysql_select_db('pre');
mysql_query('set names utf8');

echo <<<eof
<style type="text/css">
table {width:900px; border:1px solid black; margin:auto; border-collapse:collapse;}
td {border:1px solid black; }
</style>
<table>
<tr style='font-weight:bold'><td>序号</td><td>姓名</td><td>性别</td><td>年龄</td><td>生日</td></tr>
eof;

// 引入分页类
include "./myPage.class.php";
// 获得总条数、每页显示条数,
$sql = "select count(*) from train";
$qry = mysql_query($sql);
$per = 7;//每页条数
if(mysql_num_rows($qry)){
$data=mysql_fetch_array($qry);
$total = $data[0]; //总条数
}

// 实例化分页类对象
$page = new Page($total, $per);
// 设置sql语句获得每页信息
$sql3 = "select * from train ".$page->limit;
$qry3 = mysql_query($sql3);
// 获得页码列表信息
$page_list = $page -> fpage();
$page_num = isset($_GET['page'])?$_GET['page']:1;
$num = ($page_num-1)*$per+1;
while($rst3 = mysql_fetch_assoc($qry3)){
printf("<tr>");
printf("<td>%d</td>",$num);
printf("<td>%s</td>",$rst3['label']);
printf("<td>%s</td>",$rst3['clickTime']);
printf("<td>%d</td>",$rst3['conversionTime']);
printf("<td>%d</td>",$rst3['creativeID']);
printf("</tr>");
$num++;
}
printf("<tr><td colspan='5'>%s</td></tr>",$page_list);
echo "</table>";


使用时只需要把数据库的用户名密码,以及选择数据库的名字,表名改成你自己的就行了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: