Home Backend Development PHP Tutorial PHP paging class (source code + example)

PHP paging class (source code + example)

Jul 25, 2016 am 08:52 AM

Sharing a PHP paging class, it is well written and provides two examples of calling this paging class for your reference.

1, php paging code

<?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 ; 

    } 
}
Copy after login

2, Example 1 of paging class

<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'); 
?>
Copy after login

3, Example 2 of calling paging class

<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>
Copy after login


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles