Home > php教程 > php手册 > php实现分页功能的3种方法第1/3页

php实现分页功能的3种方法第1/3页

WBOY
Release: 2016-06-06 19:36:36
Original
1222 people have browsed it

这篇文章主要介绍了php实现分页功能的3种方法,感兴趣的小伙伴们可以参考一下

方法一:讲sql查询进行分页进行,需要调用几个函数,具体见脚本:
1.pager.class.php

_conn === false || !is_resource($this->_conn)) { warningLog(__METHOD__ . ': query sql with no connection', true); return false; } $this->_query_id = @mysql_query($query, $this->_conn); if ($this->_query_id === false) { $this->_errstr = @mysql_error(); $ret = false; } else { $this->_errstr = 'SUCCESS'; $ret = $this->_query_id; } } $msg = ($ret === false) ? 'false' : strval($ret); debugLog(__METHOD__.": [$msg] returned for sql query [$query]"); return $ret; } function __construct($sql,$page_size) { $result = mysql_query($sql); $datanum = mysql_num_rows($result); $this->sql=$sql; $this->datanum=$datanum; $this->page_size=$page_size; } //当前页数 public function page_id() { if($_SERVER['QUERY_STRING'] == ""){ return 1; }elseif(substr_count($_SERVER['QUERY_STRING'],"page_id=") == 0){ return 1; }else{ return intval(substr($_SERVER['QUERY_STRING'],8)); } } //剩余url值 public function url() { if($_SERVER['QUERY_STRING'] == ""){ return ""; }elseif(substr_count($_SERVER['QUERY_STRING'],"page_id=") == 0){ return "&".$_SERVER['QUERY_STRING']; }else{ return str_replace("page_id=".$this->page_id(),"",$_SERVER['QUERY_STRING']); } } //总页数 public function page_num() { if($this->datanum == 0){ return 1; }else{ return ceil($this->datanum/$this->page_size); } } //数据库查询的偏移量 public function start() { return ($this->page_id()-1)*$this->page_size; } //数据输出 public function sqlquery() { return $this->sql." limit ".$this->start().",".$this->page_size; } //获取当前文件名 private function php_self() { return $_SERVER['PHP_SELF']; } //上一页 private function pre_page() { if ($this->page_id() == 1) { //页数等于1 return "url().">上一页 "; }elseif ($this->page_id() != 1) { //页数不等于1 return "page_id()-1).$this->url().">上一页 "; } } //显示分页 private function display_page() { $display_page = ""; if($this->page_num() page_num();$i++) //循环显示出页面 $display_page .= "url().">".$i." "; return $display_page; }elseif($this->page_num() > 10){ //大于10页 if($this->page_id() php_self()."?page_id=".$i.$this->url().">".$i." "; return $display_page; }elseif(($this->page_id() > 6) && ($this->page_num()-$this->page_id() >= 4)){ for ($i=$this->page_id()-5;$ipage_id()+4;$i++) //循环显示出页面 $display_page .= "url().">".$i." "; return $display_page; }elseif(($this->page_id() > 6) && ($this->page_num()-$this->page_id() page_num()-9;$ipage_num();$i++) //循环显示出页面 $display_page .= "url().">".$i." "; return $display_page; } } } //下一页 private function next_page() { if ($this->page_id() page_num()) { //页数小于总页数 return "page_id()+1).$this->url().">下一页 "; }elseif ($this->page_id() == $this->page_num()) { //页数等于总页数 return "page_num().$this->url().">下一页 "; } } // 设置分页信息 public function set_page_info() { $page_info = "共".$this->datanum."条 "; $page_info .= "url().">首页 "; $page_info .= $this->pre_page(); $page_info .= $this->display_page(); $page_info .= $this->next_page(); $page_info .= "page_num().$this->url().">尾页 "; $page_info .= "第".$this->page_id()."http://www.jb51.net/".$this->page_num()."页"; return $page_info; } } ?>

2.脚本2:

sqlquery()); while($info = mysql_fetch_array($result,MYSQL_ASSOC)){ // while($info = mysql_fetch_array($res_1, MYSQL_ASSOC)){ echo $info["word"]."
"; } // 页码索引条 echo $page->set_page_info(); ?>

方法二:使用ajax的方法
1、首先了解SQL语句中的limit用法

SELECT * FROM table …… limit 开始位置 , 操作条数 (其中开始位置是从0开始的)

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template