test.html
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<p>dfsdfsf</p>
<div>
</div>
</body>
</html>
<script type="text/javascript" src="../jquery-1.4.4.js"></script>
<script type="text/javascript">
function cgpage(obj){
var goal=$(obj).attr('href');
sendurl(goal);
}
function sendurl(url)
{
console.log(url);
$.ajax({
url:url,
dataType:'json',
type:"get",
success:function(msg){
var str='<table cellpadding="5" cellspacing="5" border="1" >';
for(var i=0;i<msg['pagesize'];i++)
str+="<tr><td>"+msg[i]['id']+"</td><td>"+msg[i]['num']+"</td></tr>";
str+="</table>";
str+=msg['pageinfo'];
$('div').html(str);
/* for(var)
$('div').html("
<?php foreach ($data as $v):?>
<tr><td><?php echo $v['id'];?></td><td><?php echo $v['num'];?></td></tr>
<?php endforeach;?>
</table>
<?php echo $pageinfo;?>")
}*/
}
});
}
sendurl("test.php?page=1");
</script>
test.php
<?php
mysql_connect('localhost','root','');
mysql_select_db('test');
$result=mysql_query('select *from a');
while($rst=mysql_fetch_assoc($result))
$rsts[]=$rst;
require './Page.class.php';
$total_rst=mysql_query("select count(*) from a");
$total=mysql_fetch_row($total_rst);
//var_dump($total);
$data['pagesize']=$pagesize=1;
$current = isset($_GET['page']) ? $_GET['page'] : 1;
$offset=($current-1)*$pagesize;
$res=mysql_query("select *from a limit $offset,$pagesize");
while($rst=mysql_fetch_assoc($res))
$data[]=$rst;
$page = new Page($total[0],$pagesize,$current,"test.php");
$data['pageinfo'] = $page->showPage();
//$data['pageinfo']=$pageinfo;
echo json_encode($data);
//require "./test.html";
Page.class.php
<?php
//分页类,完成分页信息的输出
//共有 3 条记录,每页显示 2 条记录, 当前为 1/2 [首页] [上一页] [下一页] [末页]
//index.php?p=admin&c=brand&a=index&page=2
class Page{
//属性
private $total; //总的记录数
private $pagesize; //每页显示的记录数
private $currernt; //当前页数
private $pagenum; //总的页数
private $first; //首页超链接
private $last; //末页超链接
private $prev; //上一页超链接
private $next; //下一页超链接
private $url; //超链接的地址
//构造方法
/**
*@param script string 超链接地址的文件名,不带任何参数
*@param params array 超链接地址的参数
*/
function __construct($total,$pagesize,$current,$script,$params=array()){
$this->total = $total;
$this->pagesize = $pagesize;
$this->current = $current;
$this->pagenum = ceil( $total / $pagesize );
//index.php?p=admin&c=brand&a=index&page=2
//new分页类 new Page(10,3,2,'index.php',array('p'=>'admin','c'=>'brand','a'=>'index'))
$temp = array();
foreach ($params as $k =>$v){
//首先要形成 p=admin c=brand a=index 的内容,以数组的形式保存
$temp[] = "$k=$v";
}
$str = implode("&", $temp); // p=admin&c=brand&a=index
$this->url = "$script?{$str}&page="; //由于上页,下页,首页,末页的page是不同的,不能指定
$this->first = $this->getFirst(); //得到首页超链接
$this->last = $this->getLast();
$this->prev = $this->getPrev();
$this->next = $this->getNext();
}
//获得首页超链接
private function getFirst(){
//判断当前页是否为第一页
if ($this->current == 1) {
// 当前页是第一页
return "[首页]";
}else {
//不是第一页
return "<a href='{$this->url}1' οnclick='cgpage(this);return false;'>[首页]</a>";
}
}
//获得末页超链接
private function getLast(){
//判断是否为最后一页
if ($this->current == $this->pagenum ){
return "[末页]";
} else {
return "<a href='{$this->url}{$this->pagenum}' οnclick='cgpage(this);return false;'>[末页]</a>";
}
}
//获得上一页超链接
private function getPrev(){
//判断是否为第一页
if ($this->current == 1){
return "[上一页]";
} else {
return "<a href='{$this->url}".($this->current - 1)."' οnclick='cgpage(this);return false;'>[上一页]</a>";
}
}
//获得下一页超链接
private function getNext(){
//判断是否为末页
if ($this->current == $this->pagenum){
return "[下一页]";
} else {
return "<a href='{$this->url}".($this->current + 1)."' οnclick='cgpage(this);return false;'>[下一页]</a>";
}
}
//主方法,输出分页信息
public function showPage(){
if ($this->pagenum >= 1){
return "共有 {$this->total} 条记录,每页显示 {$this->pagesize} 条记录,
当前为 {$this->current}/{$this->pagenum} {$this->first} {$this->prev}
{$this->next} {$this->last}";
} else {
return "";
}
}
}<?php
//分页类,完成分页信息的输出
//共有 3 条记录,每页显示 2 条记录, 当前为 1/2 [首页] [上一页] [下一页] [末页]
//index.php?p=admin&c=brand&a=index&page=2
class Page{
//属性
private $total; //总的记录数
private $pagesize; //每页显示的记录数
private $currernt; //当前页数
private $pagenum; //总的页数
private $first; //首页超链接
private $last; //末页超链接
private $prev; //上一页超链接
private $next; //下一页超链接
private $url; //超链接的地址
//构造方法
/**
*@param script string 超链接地址的文件名,不带任何参数
*@param params array 超链接地址的参数
*/
function __construct($total,$pagesize,$current,$script,$params=array()){
$this->total = $total;
$this->pagesize = $pagesize;
$this->current = $current;
$this->pagenum = ceil( $total / $pagesize );
//index.php?p=admin&c=brand&a=index&page=2
//new分页类 new Page(10,3,2,'index.php',array('p'=>'admin','c'=>'brand','a'=>'index'))
$temp = array();
foreach ($params as $k =>$v){
//首先要形成 p=admin c=brand a=index 的内容,以数组的形式保存
$temp[] = "$k=$v";
}
$str = implode("&", $temp); // p=admin&c=brand&a=index
$this->url = "$script?{$str}&page="; //由于上页,下页,首页,末页的page是不同的,不能指定
$this->first = $this->getFirst(); //得到首页超链接
$this->last = $this->getLast();
$this->prev = $this->getPrev();
$this->next = $this->getNext();
}
//获得首页超链接
private function getFirst(){
//判断当前页是否为第一页
if ($this->current == 1) {
// 当前页是第一页
return "[首页]";
}else {
//不是第一页
return "<a href='{$this->url}1' οnclick='cgpage(this);return false;'>[首页]</a>";
}
}
//获得末页超链接
private function getLast(){
//判断是否为最后一页
if ($this->current == $this->pagenum ){
return "[末页]";
} else {
return "<a href='{$this->url}{$this->pagenum}' οnclick='cgpage(this);return false;'>[末页]</a>";
}
}
//获得上一页超链接
private function getPrev(){
//判断是否为第一页
if ($this->current == 1){
return "[上一页]";
} else {
return "<a href='{$this->url}".($this->current - 1)."' οnclick='cgpage(this);return false;'>[上一页]</a>";
}
}
//获得下一页超链接
private function getNext(){
//判断是否为末页
if ($this->current == $this->pagenum){
return "[下一页]";
} else {
return "<a href='{$this->url}".($this->current + 1)."' οnclick='cgpage(this);return false;'>[下一页]</a>";
}
}
//主方法,输出分页信息
public function showPage(){
if ($this->pagenum >= 1){
return "共有 {$this->total} 条记录,每页显示 {$this->pagesize} 条记录,
当前为 {$this->current}/{$this->pagenum} {$this->first} {$this->prev}
{$this->next} {$this->last}";
} else {
return "";
}
}
}
数据库