Ajax无刷新分页的必要性
具体实现:
商品总记录条数,每页显示多少条
mysql数据库关键字limit
limit 偏移量 长度
偏移量:(当前页码-1)*每页显示条数。
limit 0,7
limit 7,7
limit 14,7
ajax无刷新分页对传统分页效果的封装
制作传统分页效果:
1.建表
create table sw_goods(
goods_id int auto_increment primary key,
goods_name varchar(255) not null default '' comment '商品的名称',
goods_weight int unsigned not null default 0 comment '商品的重量',
goods_price int unsigned not null default 0 comment '商品的价格',
goods_introduce varchar(255) not null default '' comment '商品的品牌',
goods_big_img varchar(255) not null default '' comment '商品大图片',
goods_small_img varchar(255) not null default '' comment '商品小图片',
goods_create_time int unsigned not null default 0 comment '创建时间',
goods_last_time int unsigned not null default 0 comment '最后时间'
) charset=utf8,
engine=MyIsam;
写入php代码:
header("content-type:text/html;charset=utf-8");
$link=mysql_connect('localhost','root','root');
mysql_select_db('php39',$link);
mysql_query('set names utf8');
$sql="select*from sw_goods order by goods_id desc";
$qry=mysql_query($sql);
while($rst=mysql_fetch_assoc($qry)){
print_r($rst);
echo "
";
}
?>
输出结果如图所示:
例3 根据波利亚《怎样解题》的第四阶段的建议:"你能不能把这一结果或方法用于其他的问题? "修改上例代码
header("content-type:text/html;charset=utf-8");
$link=mysql_connect('localhost','root','root');
mysql_select_db('php39',$link);
mysql_query('set names utf8');
$sql="select*from sw_goods order by goods_id desc";
$qry=mysql_query($sql);
echo<< table{width:700px; border:1px solid black; margin:auto; } td{ border:1px solid black; margin:auto; border-collapse:collapse; } eof; while($rst=mysql_fetch_assoc($qry)){ echo " echo " echo " echo " echo " echo " echo " } echo "
";
序号
名称
价格
数量
重量
";
";
".$rst['goods_id']." ";
".$rst['goods_name']." ";
".$rst['goods_price']." ";
".$rst['goods_number']." ";
".$rst['goods_weight']." ";
?>
增加代码
ALTER TABLE `sw_goods`
ADD `goods_number` int unsigned not null default 0 comment '商品的数量'
如图所示:
例4 根据波利亚《怎样解题》的第四阶段的建议:"你能不能把这一结果或方法用于其他的问题? "修改上例代码运用到分页中去
提示:你需要引用一个封装好代码:
class Page {
private $total; //数据表中总记录数
private $listRows; //每页显示行数
private $limit;
private $uri;
private $pageNum; //页数
private $config=array('header'=>"个记录", "prev"=>"上一页", "next"=>"下一页", "first"=>"首 页", "last"=>"尾 页");
private $listNum=8;
/*
* $total
* $listRows
*/
public function __construct($total, $listRows=10, $pa=""){
$this->total=$total;
$this->listRows=$listRows;
$this->uri=$this->getUri($pa);
$this->page=!empty($_GET["page"]) ? $_GET["page"] : 1;
$this->pageNum=ceil($this->total/$this->listRows);
$this->limit=$this->setLimit();
}
private function setLimit(){
return "Limit ".($this->page-1)*$this->listRows.", {$this->listRows}";
}
private function getUri($pa){
$url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"], '?')?'':"?").$pa;
$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;
}
function __get($args){
if($args=="limit")
return $this->limit;
else
return null;
}
private function start(){
if($this->total==0)
return 0;
else
return ($this->page-1)*$this->listRows+1;
}
private function end(){
return min($this->page*$this->listRows,$this->total);
}
private function first(){
$html = "";
if($this->page==1)
$html.='';
else
$html.=" {$this->config["first"]} ";
return $html;
}
private function prev(){
$html = "";
if($this->page==1)
$html.='';
else
$html.=" {$this->config["prev"]} ";
return $html;
}
private function pageList(){
$linkPage="";
$inum=floor($this->listNum/2);
for($i=$inum; $i>=1; $i--){
$page=$this->page-$i;
if($page<1)
continue;
//$linkPage.=" {$page} ";
$linkPage.=" {$page} ";
}
$linkPage.=" {$this->page} ";
for($i=1; $i<=$inum; $i++){
$page=$this->page+$i;
if($page<=$this->pageNum){
//$linkPage.=" {$page} ";
$linkPage.=" {$page} ";
}else{
break;
}
}
return $linkPage;
}
private function next(){
$html = "";
if($this->page==$this->pageNum)
$html.='';
else
$html.=" {$this->config["next"]} ";
return $html;
}
private function last(){
$html = "";
if($this->page==$this->pageNum)
$html.='';
else
$html.=" {$this->config["last"]} ";
return $html;
}
private function goPage(){
return ' ';
//return ' ';
}
function fpage($display=array(0,1,2,3,4,5,6,7,8)){
$html[0]=" 共有{$this->total}{$this->config["header"]} ";
$html[1]=" 每页显示".($this->end()-$this->start()+1)."条,本页{$this->start()}-{$this->end()}条 ";
$html[2]=" {$this->page}/{$this->pageNum}页 ";
$html[3]=$this->first();
$html[4]=$this->prev();
$html[5]=$this->pageList();
$html[6]=$this->next();
$html[7]=$this->last();
$html[8]=$this->goPage();
$fpage='';
foreach($display as $index){
$fpage.=$html[$index];
}
return $fpage;
}
}
?>
修改代码如下:
header("content-type:text/html;charset=utf-8");
$link=mysql_connect('localhost','root','root');
mysql_select_db('php39',$link);
mysql_query('set names utf8');
//引入分页的类文件
include("./page.class.php");
$sql="select*from sw_goods order by goods_id desc";
$qry=mysql_query($sql);
//获取商品总条数
$total=mysql_num_rows($qry);//总条数
$per=2;//每页显示2条数据
//实例化分页类的对象
$page_obj=new Page($total,$per);
//拼装一条sql语句获得每页信息
$sql3="select goods_id,goods_name,goods_price,goods_number,goods_weight from sw_goods limit 0,2";
$qry3=mysql_query($sql3);
echo<< table{width:700px; border:1px solid black; margin:auto; } td{ border:1px solid black; margin:auto; border-collapse:collapse; } eof; echo " echo " echo " echo " echo " echo " echo " } echo "
";
序号
名称
价格
数量
重量
while($rst=mysql_fetch_assoc($qry3)){
";
";
".$rst['goods_id']." ";
".$rst['goods_name']." ";
".$rst['goods_price']." ";
".$rst['goods_number']." ";
".$rst['goods_weight']." ";
?>
header("content-type:text/html;charset=utf-8");
//传统方式实现分页效果
$link = mysql_connect('localhost','root','root');
mysql_select_db('php39',$link);
mysql_query('set names utf8');
//实现数据分页
//① 引入分页的类文件
include("./page.class.php");
//② 获得商品的总条数
$sql = "select * from sw_goods order by goods_id desc";
$qry = mysql_query($sql);
$total = mysql_num_rows($qry); //总条数
$per = 3;//每页显示3条数据
//③ 实例化分页类对象
$page_obj = new Page($total, $per);
//④ 拼装一条sql语句获得每页信息
$sql3 = "select goods_id,goods_name,goods_price,goods_number,goods_weight from sw_goods ".$page_obj->limit;
$qry3 = mysql_query($sql3);
//⑤ 获得页码列表
$pagelist = $page_obj -> fpage(array(3,4,5,6,7,8));
echo << table{width:700px; border:1px solid black; margin:auto; border-collapse:collapse;} td {border:1px solid black;} eof; while($rst3 = mysql_fetch_assoc($qry3)){ //$rst 代表每条记录的一维数组信息 echo " echo " echo " echo " echo " echo " echo " } echo " echo "
";序号 名称 价格 数量 重量 ";
";
".$rst3['goods_id']." ";
".$rst3['goods_name']." ";
".$rst3['goods_price']." ";
".$rst3['goods_number']." ";
".$rst3['goods_weight']." ";
";
$pagelist
?>