首页 后端开发 php教程 php 分页类(源码+实例)

php 分页类(源码+实例)

Jul 25, 2016 am 08:52 AM

分享一个php分页类,写的不错,提供了二个该分页类的调用实例,供大家学习参考。

1,php分页类代码

<?php
/**
* php分页类
* by bbs.it-home.org
*/

//定义错误级别
error_reporting(E_ALL); 

//分页类
class pagination{ 
public  $Start;  // 开始mysql查询
public  $End;  // 结束mysql查询
private $Number;  //分布数据的数量 
public  $Pages; // 页数
private $N_p_p;  //每页要显示的内容数量 
private $Page_number; //当前页数 
private $Buttons; //最大显示的按钮数,即每页面中显示出的页数

function __construct($number,$n_p_p=10,$page_number=1,$buttons=5) 
{ 
     
    //page start from 1
    $page_number    =    ($page_number<1)    ?    1    :    $page_number    ;
    $this->        Number        =    $number;
    $this->        N_p_p        =    $n_p_p; 
    $this->        Pages        =    ceil    (    $this->    Number    /    $this->    N_p_p    )    ; 
    $this->        Buttons        =    $buttons        ; 
    $page_number=    ($page_number>$this->Pages)    ?    $this->Pages    :    $page_number    ; 
    $this->        Page_number    =    $page_number    ; 
    $this->        Ret()    ; 
    } 

 public function Show_Pagination($link,$get='page',$div_class_name='pagination') 
    { 

    //if pages == 1 , no need to print pagination 
    if($this->Pages==1)return; 
    //$link is the addres of current page      
    //$get is name of get method 
    //echo pagination's div 
     
    echo'<div class="'.$div_class_name.'">'; 
    //echo pre button 
     
    if($this->Page_number>1)echo 'Page_number -1 ).'">Per '; 
    else echo 'Per '; 
     
    //print button
    $this->Buttons=(int)$this->Buttons;
    $start_counter    =    $this->Page_number-floor($this->Buttons/2);//for normal mode
    $end_conter        =    $this->Page_number+floor($this->Buttons/2);//for normal mode
    //try to buttons exactly equal to $Buttons    
    if($start_counter<1) $end_conter=$end_conter+abs($start_counter);
    if($end_conter>$this->Pages) $start_counter=$start_counter-($end_conter-$this->Pages);
    if(($this->Page_number-floor($this->Buttons/2))<1)$end_conter ++;     
     
    for ($i=$start_counter;$i<=$end_conter;$i++) 
{ 
     
if($i>$this->Pages || $i<1)continue;        //no print less than 1 value or grater than totall page 
     
if($i==$this->Page_number)echo ' '.$i.' ';         // change current page' class 
     
else echo ' '.$i.' ';      // normal pages 
     
} 
      
    //echo next button 
     
    if($this->Page_number<$this->Pages)echo 'Page_number +1 ) .'">Nex '; 
     
    else echo 'Nex ';         
     
    //close div tag 
     
    echo'</div>'; 
     
    } 


    //give the page number and return start and end of selection  

 private function  Ret() 
    { 

    $this->Start=(($this->Page_number-1)*$this->N_p_p); 

    $this->End=  $this->N_p_p ; 

    } 
}
登录后复制

2,分页类的示例一

<html> 
<head> 
<style type="text/css"> 
/* style for show*/ 
.pagination{direction:ltr} 
.pagination a{border-radius:3px;background-color:#eee;color:#555;border:1px solid #aaaaaa;padding-top:2px;padding-bottom:2px;
padding-right:5px;padding-left:5px;text-decoration:none;} 
.pagination a:hover ,.pagination .cur{border-color:#0C52CE;color:#0C52CE;background-color:#fff;} 
</style> 
<title>php分页类示例---bbs.it-home.org</title> 
</head> 

<?php  
//include class: 
require_once"pagination.php"; 

//get the numbet of current page 
//note! you should  safe it  
if(isset($_GET['page'])){ 
$page=$_GET['page']; 
} 
else $page=1; 

//connect to db ... 
mysql_connect("localhost","root","");//use your host,username and password to connect to the db 

mysql_select_db("dbname");//select your database 

//run query to get number of all records 
$query=mysql_query("select count(id) from table"); //raplace table with your table name 
$totall=mysql_result($query,0); 

//creat new object 
$pagination=new pagination($totall,3 /*number of content per page*/,$page,5 /*number of button to show*/); 

//get records of current page 
$SecondQuery=mysql_query("select id from table order by id desc limit $pagination->Start , $pagination->End"); 

//echo your result 
while($row=mysql_fetch_assoc($SecondQuery)) 
    { 
        echo 'id='.$row['id'].'<br>'; 
     
    }
//show pagination: 
$pagination->Show_Pagination("ExampleMysql.php?param1=value1",'page','pagination'); 
?>
登录后复制

3,分页类的调用示例二

<html> 
<head> 
<style type="text/css"> 
/* style for show*/ 
.pagination{direction:ltr} 
.pagination a{border-radius:3px;background-color:#eee;color:#555;border:1px solid #aaaaaa;padding-top:2px;padding-bottom:2px;
|padding-right:5px;padding-left:5px;text-decoration:none;} 
.pagination a:hover ,.pagination .cur{border-color:#0C52CE;color:#0C52CE;background-color:#fff;} 
</style> 
<title>php分页类的调用示例-bbs.it-home.org</title> 
</head> 
<?php  
//include分页类文件 
require_once "pagination.php"; 

//param of url to specify page_number 
$get_param='page'; 

//get current page from url 
$current_page=(isset($_GET[$get_param]) && is_numeric($_GET[$get_param]))?$_GET[$get_param]:1; 
//notice: when get param , youe should SAFE it  

//get totall available content   
// for example  you can get number of news from news table in database ( mysql_num_rows or count()) 
$totall_content=120; // for example 

//creat new cat object  
$cat=new pagination($totall_content,10 /*number of content per page*/,$current_page,5 /*number of button*/); 

//when you want to load content of page: for example in db queris : 

// select * from table where conditions order by key  limit $cat->Start , $cat->End 

//for show : 
$cat->Show_Pagination("example.php?",'page','pagination'); 
?> 
</html>
登录后复制


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

在Laravel中使用Flash会话数据 在Laravel中使用Flash会话数据 Mar 12, 2025 pm 05:08 PM

Laravel使用其直观的闪存方法简化了处理临时会话数据。这非常适合在您的应用程序中显示简短的消息,警报或通知。 默认情况下,数据仅针对后续请求: $请求 -

php中的卷曲:如何在REST API中使用PHP卷曲扩展 php中的卷曲:如何在REST API中使用PHP卷曲扩展 Mar 14, 2025 am 11:42 AM

PHP客户端URL(curl)扩展是开发人员的强大工具,可以与远程服务器和REST API无缝交互。通过利用Libcurl(备受尊敬的多协议文件传输库),PHP curl促进了有效的执行

简化的HTTP响应在Laravel测试中模拟了 简化的HTTP响应在Laravel测试中模拟了 Mar 12, 2025 pm 05:09 PM

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显着减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

在Codecanyon上的12个最佳PHP聊天脚本 在Codecanyon上的12个最佳PHP聊天脚本 Mar 13, 2025 pm 12:08 PM

您是否想为客户最紧迫的问题提供实时的即时解决方案? 实时聊天使您可以与客户进行实时对话,并立即解决他们的问题。它允许您为您的自定义提供更快的服务

解释PHP中晚期静态结合的概念。 解释PHP中晚期静态结合的概念。 Mar 21, 2025 pm 01:33 PM

文章讨论了PHP 5.3中引入的PHP中的晚期静态结合(LSB),从而允许静态方法的运行时分辨率调用以获得更灵活的继承。 LSB的实用应用和潜在的触摸

自定义/扩展框架:如何添加自定义功能。 自定义/扩展框架:如何添加自定义功能。 Mar 28, 2025 pm 05:12 PM

本文讨论了将自定义功能添加到框架上,专注于理解体系结构,识别扩展点以及集成和调试的最佳实践。

如何用PHP的cURL库发送包含JSON数据的POST请求? 如何用PHP的cURL库发送包含JSON数据的POST请求? Apr 01, 2025 pm 03:12 PM

使用PHP的cURL库发送JSON数据在PHP开发中,经常需要与外部API进行交互,其中一种常见的方式是使用cURL库发送POST�...

See all articles