php数据库改分页类 php mysql 分页( 二 )


return $display_page;
}
}
}
//下一页
private function next_page() {
if ($this-page_id()$this-page_num()) { //页数小于总页数
return "a href="https://www.04ip.com/post/.$this-php_self()."?page_id=".($this-page_id()+1).$this-url()."下一页/a ";
}elseif ($this-page_id() == $this-page_num()) { //页数等于总页数
return "a href="https://www.04ip.com/post/.$this-php_self()."?page_id=".$this-page_num().$this-url()."下一页/a ";
}
}
// 设置分页信息
public function set_page_info() {
$page_info = "共".$this-datanum."条 ";
$page_info .= "a href="https://www.04ip.com/post/.$this-php_self()."?page_id=1".$this-url()."首页/a ";
$page_info .= $this-pre_page();
$page_info .= $this-display_page();
$page_info .= $this-next_page();
$page_info .= "a href="https://www.04ip.com/post/.$this-php_self()."?page_id=".$this-page_num().$this-url()."尾页/a ";
$page_info .= "第".$this-page_id()."/".$this-page_num()."页";
return $page_info;
}
}
?
2.脚本2:
?php
//类的用法
// 读取分页类
include("pager.class.php");
// 数据库连接初始化
//$db = new mysql();
$impeach_host = '10.81.43.139';
$impeach_usr = 'vmtest15';
$impeach_passwd = 'vmtest15';
$impeach_name = 'ufeature';
$impeach_con = mysql_connect($impeach_host, $impeach_usr, $impeach_passwd) or
die("Can't connect ".mysql_error());
mysql_select_db($impeach_name, $impeach_con);
// 这是一个sql查询语句,并得到查询结果
$sql = "select word from ufeature.spam_accuse_word_list where flag='0'";
// 分页初始化
$page = new pager($sql,20);
// 20是每页显示的数量
// $res_1 = mysql_query($sql) or
//die("Can't get result ".mysql_error());
$result=mysql_query($page-sqlquery());
while($info = mysql_fetch_array($result,MYSQL_ASSOC)){
// while($info = mysql_fetch_array($res_1, MYSQL_ASSOC)){
echo $info["word"]."br/";
}
// 页码索引条
echo $page-set_page_info();
?
方法二:使用ajax的方法
1、首先了解SQL语句中的limit用法
SELECT * FROM table …… limit 开始位置,操作条数 (其中开始位置是从0开始的)
例子
取前20条记录:SELECT * FROM table …… limit0  ,  20
从第11条开始取20条记录:SELECT * FROM table …… limit10,20
LIMIT n 等价于 LIMIT 0,n 。
如select * from table LIMIT 5; //返回前5行,和select * from table LIMIT 0 , 5一样
2、分页原理
所谓分页显示,也就是讲数据库中的结果集,一段一段显示出来
怎么分段,当前在第几段 (每页有几条,当前再第几页)
前10条记录:select * from table limit 0,10
第11至20条记录:select * from table limit 10,10
第21至30条记录:select * from table limit 20,10
分页公式:
(当前页数 - 1 )X 每页条数,每页条数
Select * from table limit ($Page- 1) * $PageSize, $PageSize
3、$_SERVER["REQUEST_URI"]函数
预定义服务器变量的一种,所有$_SERVER开头的都叫做预定于服务器变量 。
REQUEST_URI的作用是取得当前URI,也就除域名外后面的完整的地址路径 。
例子:
当前页为:;cid=22
echo $_SERVER["REQUEST_URI"]
结果为:/home.php?id=23cid=22
4、parse_url()解析URL函数
parse_url() 是讲URL解析成有固定键值的数组的函数
例子
$ua=parse_url("");
print_r($ua);
结果:
Array
(
[scheme] = http;协议
[host] = hostname;主机域名
[user] = username;用户
[pass] = password;密码
[path] = /path;路径
[query] = arg=value;取参数
[fragment] = anchor;

推荐阅读