php数据库分页类,php数据库分页是怎么实现的-成都快上网建站

php数据库分页类,php数据库分页是怎么实现的

php分页功能怎么实现

php本身是没有分页概念的,分页是URL传参,然后通过mysql查询语句到数据库获取数据,然后实现的分页,url上的参数,通过PHP的$_GET都是可以获取到的。

10年积累的成都网站制作、成都网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先制作网站后付款的网站建设流程,更有类乌齐免费网站建设让你可以放心的选择与我们合作。

现在市面上的PHP框架基本都有PHP分页类,参照文档直接调用就好了,如果想看实现过程,可以去下载一个TP框架,然后打开里面的分页类查看里面的源代码。

ThinkPHP使用心得分享-分页类Page的用法

ThinkPHP中的Page类在ThinkPHP/Extend/Library/ORG/Util/Page.class.php中,所以使用前要引入Page类:

复制代码

代码如下:

import('ORG.Util.Page');

//Page类的引入

$db

=

M('abc');//实例化数据表abc

$where

=

array(

'id'='2';

);//条件语句$where,例表中字段id的值为2

$count

=

$db-where($where)-count();//获取符合条件的数据总数count

$page

=

new

Page($count,

10);//实例化page类,传入数据总数和每页显示10条内容

$limit

=

$page-firstRow

.

','

.

$page-listRows;//每页的数据数和内容$limit

$result

=$db-where($where))-limit($limit)-select();//分页查询结果

$this-result

=

$result;//赋值

$this-show

=

$page-show();//获取分页的底部信息

以上代码是分页类实现的基本语句,当然喜欢使用原生sql语句的朋友也可以配合原生sql语句实现查询分页:

复制代码

代码如下:

import('ORG.Util.Page');

//Page类的引入

$db

=

M('abc');//实例化数据表abc

$where

=

array(

'id'='2';

);//条件语句$where,例表中字段id的值为2

$count

=

$db-where($where)-count();//获取符合条件的数据总数count

$page

=

new

Page($count,

10);//实例化page类,传入数据总数和每页显示10条内容

$Modle

=

new

Model();//实例化新数据模型

$sql

=

'select

id,name

from

abc

where

'.$where.'

limit

'.$page-firstRow.','.$page-listRows;//sql语句

$result

=

$Modle-query($sql);//执行sql语句

$this-result

=

$result

$this-show=$page-show();

当然,分布查询获取的内容也可以先对查询完的数据进行处理再赋值,比如

复制代码

代码如下:

...

$result

=$db-where($where))-limit($limit)-select();//分页查询结果

$res

=

abc($result);//abc方法(自定义方法或php函数)对结果$result进行数据排序或重组处理等

$this-result

=

$res;//赋值

php 分页类怎么用啊?

?php

class Page {

private $total_rows;//数据库总条数

private $per_page_rows;//每页显示条数

private $limit;

private $uri;

private $total_pages;//总页数

private $config=array("header"="记录条数","prev"="上一页","next"="下一页","first"="首 页","last"="尾 页");

private $list_length=8;

public function __construct($total_rows,$per_page_rows=10,$url_args){

$this-total_rows=$total_rows;

$this-per_page_rows=$per_page_rows;

$this-uri=$this-get_uri($url_args);

$this-page = !empty($_GET['page']) ? $_GET['page'] : 1;

$this-total_pages=ceil($this-total_rows/$this-per_page_rows);

$this-limit=$this-set_limit();

}

private function set_limit() {

return "limit ".($this-page-1)*$this-per_page_rows.",{$this-per_page_rows}";

}

private function get_uri($url_args) {

$url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"],"?") ? "" : "?").$url_args;

$parse=parse_url($url);

if (isset($parse['query'])) {

parse_str($parse['query'],$params);//把url字符串解析为数组

unset($params['page']);//删除数组下标为page的值

$url=$parse['path'].'?'.http_build_query($params);//再次构建url

}

return $url;

}

public function __get($args) {

if ($args=="limit") {

return $this-limit;

}else{

return null;

}

}

private function start_page(){

if ($this-total_rows==0) {

return 0;

}else{

return (($this-page-1)*$this-per_page_rows)+1;

}

}

private function end_page(){

return min($this-page*$this-per_page_rows,$this-total_rows);

}

private function go_first() {

$html="";

if ($this-page==1) {

$html.="nbsp;{$this-config['first']}nbsp;";

}else{

$html.="nbsp;a href='{$this-uri}page=1'{$this-config['first']}/anbsp;";

}

return $html;

}

private function go_prev() {

$html="";

if ($this-page==1) {

$html.="nbsp;{$this-config['prev']}nbsp;";

}else{

$html.="nbsp;a href='{$this-uri}page={$this-page}-1'{$this-config['prev']}/anbsp;";

}

return $html;

}

private function go_next() {

$html="";

if ($this-page==$this-total_pages) {

$html.="nbsp;{$this-config['next']}nbsp;";

}else{

$html.="nbsp;a href='{$this-uri}page={$this-page}+1'{$this-config['next']}/anbsp;";

}

return $html;

}

private function go_last() {

$html="";

if ($this-page==$this-total_pages) {

$html.="nbsp;{$this-config['last']}nbsp;";

}else{

$html.="nbsp;a href='{$this-uri}page={$this-total_pages}'{$this-config['last']}/anbsp;";

}

return $html;

}

private function go_page() {

return 'nbsp;input type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value'.$this-total_pages.')?'.$this-total_pages.':this.value;location=\''.$this-uri.'page=\'+page+\'\'}" value="'.$this-page.'" style="width:25px;" /input type="button" onclick="javascript:var page=(this.previousSibling.value'.$this-total_pages.')?'.$this-total_pages.':this.previousSibling.value;location=\''.$this-uri.'page=\'+page+\'\'" value="GO" /nbsp;';

}

private function page_list() {

$link_page="";

$i_num=floor($this-list_length/2);

for ($i = $i_num; $i = 1; $i--) {

$page=$this-page-$i;

if ($page1) {

continue;

}else{

$link_page.="nbsp;a href='{$this-uri}page={$page}'{$page}/anbsp;";

}

}

$link_page.="nbsp;{$this-page}nbsp;";

for ($i = 1; $i  $i_num; $i++) {

$page=$this-page+$i;

if ($page=$this-total_pages) {

$link_page.="nbsp;a href='{$this-uri}page={$page}'{$page}/anbsp;";

}else{

break;

}

}

return $link_page;

}

public function out_page($display=array(0,1,2,3,4,5,6,7,8)) {

$display_html='';

$html[0]="nbsp;共有b{$this-total_rows}/b{$this-config['header']}nbsp;";

$html[1]="nbsp;每页显示b".($this-end_page()-$this-start_page()+1)."/b条,本页显示从b{$this-start_page()}/b--b{$this-end_page()}/b{$this-config['header']}nbsp;";

$html[2]="nbsp;b{$this-page}/b/b{$this-total_pages}/b页nbsp;";

$html[3]=$this-go_first();

$html[4]=$this-go_prev();

$html[5]=$this-page_list();

$html[6]=$this-go_next();

$html[7]=$this-go_last();

$html[8]=$this-go_page();

foreach ($display as $index){

$display_html.=$html[$index];

}

return $display_html;

}

}

?

这个简单的PHP分页类,该如何调用?感谢。~

0代表开始的下标,比如一页显示10条的话,那么第一页就是0,10,第二页就是(2-1)*10,10,第三页就是(3-1)*10,10

所以你那个0 就是写 limit($page-1)*$pagenum,$pagenum


网页标题:php数据库分页类,php数据库分页是怎么实现的
网站网址:http://kswjz.com/article/dsipcps.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流