设为首页 收藏本站
查看: 685|回复: 0

[经验分享] PHP分页过程化及分页类实现

[复制链接]

尚未签到

发表于 2017-4-8 06:50:58 | 显示全部楼层 |阅读模式
  过程化分页

<?php
header("Content-Type:text/html;charset=utf-8");
mysql_connect('localhost','root','root');
mysql_select_db('test');
$sql="select * from test where 1";
$result=mysql_query($sql);
$total_num=mysql_num_rows($result);//查出一共有多少条记录
$show_num=3;//显示多少条
$total_pages=ceil($total_num/$show_num);//获取总的页数,ceil向上去整,floor向下
$current=isset($_GET['page'])?$_GET['page']:1;//当前页号
$next=($current==$total_pages)?false:$current+1;
$prev=($current==1)?false:$current-1;
$offset=($current-1)*$show_num;
$sql="select * from goods limit $offset,3";//offset为偏移量,代表查询时候,数据库起始位置
$result=mysql_query($sql);
mysql_close();
?>
<table>
<tr><th>id</th><th>name</th><th>price</th><th>mprice</th></tr>
<?php while($arr=mysql_fetch_assoc($result)){
$id=$arr['id'];
$name=$arr['name'];
$price=$arr['price'];
$mprice=$arr['mprice'];
?>
<tr><td><?php echo $id ?></td><td><?php echo $name ?></td><td><?php echo $price ?></td><td><?php echo $mprice ?></td></tr>
<?php } ?>
<tr><td colspan="4">
<?php
echo "一共有{$total_num}条记录,显示{$show_num}条,{$current}/{$total_pages}";
echo "首页";
if(!$prev){
echo "上一页";
}else{
echo "<a href='fenye.php?page={$prev}'>上一页</a>";
}
if(!$next){
echo "下一页";
}else{
echo "<a href='fenye.php?page={$next}'>下一页</a>";
}
echo "尾页";
unset($result);
?>
</td></tr>
</table>
下面是分页类:
<?php
class page{
//总页数
protected $count;
//当前页码
protected $page;
//总记录数,总条数
protected $total;
//上一页
protected $prev='上一页';
//下一页
protected $next='下一页';
//首页
protected $first='首页';
//尾页
protected $last='尾页';
//开始记录数
protected $start;
//结束记录数
protected $end;
//每页显示条数
protected $num;
//URL
protected $url;
//上一页数
protected $prevNum;
//下一页数
protected $nextNum;

//初使化成员属性
public function __construct($url,$total,$num=5){
$this->url=$url;
$this->total=$total;
$this->num=$num;
$this->count=$this->getCount();
$this->page=empty($_GET['page'])?1:(int)$_GET['page'];
$this->prevNum=$this->getPrev();
$this->nextNum=$this->getNext();
$this->start=$this->getStart();
$this->end=$this->getEnd();
}
protected function getStart(){
return ($this->page-1)*$this->num+1;
}
protected function getEnd(){
return min($this->page*$this->num,$this->total);
}
protected function getNext(){
if($this->page>=$this->count){
return false;
}else{
return $this->page+1;
}
}
protected function getPrev(){
if($this->page<=1){
return false;
}else{
return $this->page-1;
}
}
protected function getCount(){
return ceil($this->total/$this->num);
}
//
//
//得到偏移量  limit 偏移量,数量   limit 0,5   5,5  10,5  
public function getOffset(){
return ($this->page-1)*$this->num;
}

//得到分页效果
//
//search.php?keywords=手机&page=1
//
//第x页   从第n条记录到第n条记录  共x页 首页  上一页  下一页  尾页
public function getPage(){
$string='第'.$this->page.'页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;从第'.$this->start.'条记录到第'.$this->end.'条记录&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;共'.$this->count.'页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="'.$this->url.'page=1">'.$this->first.'</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
if($this->prevNum){
$string.='<a href="'.$this->url.'page='.$this->prevNum.'">'.$this->prev.'</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
}else{
$string.=$this->prev.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
}

if($this->nextNum){
$string.='<a href="'.$this->url.'page='.$this->nextNum.'">'.$this->next.'</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
}else{
$string.=$this->next.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
}
$string.='<a href="'.$this->url.'page='.$this->count.'">'.$this->last.'</a>';

return $string;
}
}


?>

 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-361662-1-1.html 上篇帖子: 《PHP编程网络大讲堂》--摘录11/4 下篇帖子: PHP的json_encode中文被转码的问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表