


Practical simple PHP paging collection including usage methods_PHP tutorial
Method 1:
/*
The paging class is used to display multiple pieces of data in pages
version: 1.0
Date: 2013-10-20
*/
/*
The call is very convenient. First connect to the database and directly pass the sql string to be queried. You can also specify the number of data displayed on each page
For example $pages = new Page( 'SELECT * FROM `zy_common_member`');
or $pages = new Page('SELECT * FROM `zy_common_member`', 10);
*/
class Page{
private $curPage;
private $totalPages;//How many pages of data are displayed in total
private $dispNum;//Number of data items displayed on each page
private $queryStr;//SQL statement for query
private $limitStr ; // Query the Limit control statement behind the sentence
/ *
The constructor
$ Querystr query data SQL statement
$ Dispnum The number of data displayed by each page
*/ */
public function __construct($queryStr='',$dispNum=10){
$result = mysql_query($queryStr);
$totalNum = mysql_num_rows($result);
$this-> ;dispNum = $dispNum;
$this->totalPages = ceil($totalNum / $dispNum);
$this->queryStr = $queryStr;
$temp = (isset( $_GET["curPage"]) ? $_GET["curPage"] : 1);
$this->setCurPage($temp);
$this->showCurPage();
$this->showFoot();
}
/*Display the data content of the current page*/
private function showCurPage(){
$this ->limitStr = ' LIMIT '.(($this->curPage - 1)* $this->dispNum).','.$this->dispNum;
->limitStr;
$result = mysql_query($this->queryStr.$this->limitStr); 🎜> if ($ this->totalPages > 0)
else
{
'
';
$cols = mysql_num_fields($result);
echo '
'; echo mysql_field_name($result, $i); echo ' | ';
---|
'; echo $value; echo ' | ';
}
private function setCurPage($curPage){
if($curPage {
$curPage = 1;
}
else if($curPage > $this->totalPages)
{
$curPage = $this->totalPages;
}
$this->curPage = $curPage;
}
/*
显示分页页脚的信息
如首页,上一页,下一页,尾页等信息
*/
private function showFoot(){
echo '首页';
echo '上一页';
echo '下一页';
echo '尾页';
}
}
?>
方法二:
class mysqlPager{
var $pagePerNum=5;//Number of data items displayed on each page
var $pagePerGroup=5;//Number of pages in each paging group
var $curPage=0;//Current page, Defualt first page
var $totalPage=0;//Total number of pages
var $totalNum=0;//Total number of data items
var $curPageGroup=0;//Current paging group
var $curPageUrl="";//Currently used paging URL
var $custom;//Custom style
var $pageQuerySql= "";
function mysqlPager(){//Constructor PHP4
}
/**
* Initialize all variables
*/
function InitAllVar($totalNum,$pagePerGroup,$curPageUrl,$curPage=1 ,$curPageGroup=1)
{
$this->totalNum=$totalNum;
$this->pagePerGroup=$pagePerGroup;
$this->curPageUrl=$curPageUrl;
$this->curPage=$curPage;
$this->curPageGroup=$curPageGroup;
}
/**
* Set the current page variable
*
* @param number $curPage
*/
function setCurPage($curPage)
{
$this->curPage=$curPage;
}
/**
* Set the current paging group variable
*
* @param mixed $curPageGroup
*/
function setCurPageGroup($curPageGroup)
{
$this- >curPageGroup=$curPageGroup;
}
/**
* Set the URL of the currently used distribution class
* $curPageUrl string
*/
function setCurPageUrl($curPageUrl)
{
$this->curPageUrl=$curPageUrl;
}
/**
* Get all
*
* @param number $totalNum
* @param number $curPage
* @return float
*/
function getTotalPage($totalNum,$curPage=0)
{
return $this->totalPage=ceil($totalNum/$this- >pagePerNum);
}
/**
* Set user-defined style
*
* @param mixed $customStyle
*/
function setCustomStyle($customStyle)
{
$this->customStyle=$customStyle;
}
/**
* Set user-defined style return string
*
*
* @param mixed $pagerString
*/
function setCustomStyleString($pagerString)
{
return $styleString="".$pagerString. "";
}
/**
* You can output navigation page information without parameters, but you must set the corresponding variables before use
*
* @param mixed $curPageGroup
* @param mixed $curPage
* @param mixed $curPageUrl
*/
function showNavPager($curPageGroup=0,$curPage=0,$curPageUrl=0)
{
if($curPageGroup)
{
$this->curPageGroup=$curPageGroup;
}
if($curPage)
{
$this->curPage=$ curPage;
}
if($curPageUrl)
{
$this->curPageUrl=$curPageUrl;
}
$rtnString="";
//Judgement Whether the variable is initialized
if($this->curPageGroup && $this->curPageUrl && $this->totalNum && $this->curPage)
{
$this-> totalPage=$this->getTotalPage($this->totalNum);
if($this->curPage==1)
$this->curPage=($this->curPageGroup- 1)*$this->pagePerGroup+1;
if($this->curPageGroup!=1)
{
$prePageGroup=$this->curPageGroup-1;
$ rtnString.="".$this->setCustomStyleString(" " ;
}
for($i=1;$ipagePerGroup;$i++)
{
$curPageNum=($this->curPageGroup-1)* $this->pagePerGroup+$i;
if($curPageNumtotalPage){
if($curPageNum==$this->curPage)
{
$ rtnString.=" ".$this->setCustomStyleString($curPageNum);
}else
{
$rtnString.=" curPageUrl?cpg={$ this->curPageGroup}&cp=$curPageNum >";
$rtnString.=$this->setCustomStyleString($curPageNum)."";
}
}
}
if($this->curPageGroup
{
$nextPageGroup=$this->curPageGroup+ 1;
$rtnString.=" curPageUrl?cpg=$nextPageGroup >".$this->setCustomStyleString(">>")."> ;";
}
$this->pageQuerySql=" limit ".(($this->curPage-1)*$this->pagePerNum).",".$this-> pagePerNum;
}
else
{
$rtnString="Error: Variable not initialized! ";
}
return $rtnString;
}
/**
* Get the complete Sql statement for querying MYSQL
*
* @param mixed $sql
*/
function getQuerySqlStr($sql)
{
$allsql=$sql. $this->pageQuerySql;
return $allsql;
}
/**
* Set how many data items there are on each page
*
* @param INT $num
*/
function setPagePerNum($num)
{
$this-> pagePerNum=$num;
}
}
?>
Usage:
$curPage=$_GET['cp'];
$curPageGroup=$_GET['cpg']
if($curPage=="")
$curPage =1;
if($curPageGroup=="")
$curPageGroup=1;
//They all start from 1. The incoming data must be verified before to prevent injection
/ /. . .
$pager=new MysqlPager();
$pager->initAllVar(...)
$pager->showNavPager();
//The following SQL can be any output
$sql="select id form dbname ";
$querysql=$pager->getQuerySqlStr($sql)
//You can use $querysql to query the database in the future to get the corresponding result set
Method three:
PHP paging function:
//In order to avoid duplication Including files causes errors,
adds a condition to determine whether the function exists:
if(!function_exists(pageft)){
//Define the function pageft(), the meaning of the three parameters is:
//$totle: the total number of information;
//$displaypg: the number of information displayed on each page, the default setting here is 20;
//$url: the link in the paging navigation, in addition to adding different query information
The parts outside "page" are the same as this URL.
//The default value should be set to the URL of this page (i.e. $_SERVER["REQUEST_URI"])
, but the right side of the default value can only be a constant, so the default value is set to an empty string
, and then set it to the URL of this page inside the function.
function pageft($totle,$displaypg=20,$url=”){
//Define several global variables:
//$page: current page number;
//$firstcount: (Database) starting item of query;
//$pagenav: page navigation bar code, it is not output inside the function;
//$_SERVER: Read the URL of this page "$_SERVER["REQUEST_URI" ]" required.
global $page,$firstcount,$pagenav,$_SERVER;
//In order to make the "$displaypg" here accessible from outside the function,
set it as a global variable. Note that after a variable is redefined
as a global variable, the original value is overwritten, so reassign it here
$GLOBALS["displaypg"]=$displaypg;
if(!$page) $. page=1;
//If $url uses the default value, which is an empty value, the value is assigned to the URL of this page:
if(!$url){ $url=$_SERVER["REQUEST_URI"];}
//URL analysis:
$parse_urlparse_url=parse_url($url);
$url_query=$parse_url["query"];
//Take out the query string of the URL separately
if($ url_query){
//Because the URL may contain page number information, we need to remove it
in order to add new page number information.
//Regular expressions are used here, please refer to "PHP. Regular expression "
$url_query=ereg_replace("(^|&)page=$page","",$url_query);
//Replace the query string of the processed URL The query string of the original URL:
$url=str_replace($parse_url["query"],$url_query,$url);
//Add page query information after the URL, but wait for assignment :
if($url_query) $url.=”&page”; else $url.=”page”;
}else {
$url.=”?page”;
}
$lastpg=ceil($totle/$displaypg);
//The last page is also the total number of pages
$page=min($lastpg,$page);
$prepg=$page-1 ;
//Previous page
$nextpg=($page==$lastpg ? 0 : $page+1);
//Next page
$firstcount=($page-1 )*$displaypg;
//Start paging navigation bar code:
$pagenav=”Display the ”.($totle?($firstcount+1):0).”
< ; /B>-”.min($firstcount+$displaypg,$totle).”
records, total $totle records”;
// If there is only one page, jump out of the function:
if($lastpg$pagenav.=” Homepage “ ;
if($prepg) $pagenav.=”
Previous page “; else $pagenav.=” Previous page”;
if($nextpg) $pagenav.=”
Next page “; else $pagenav.=”Next page”;
$pagenav.=” Last page “;
//Drop-down jump list, loop through all page numbers:
$ pagenav.=”Go to pagesize=’1′ onchange=’window.location=”
$url=”+this.value'>n”;
for($i=1;$iif($i==$page) $pagenav.=” selected>$ in”;
else $pagenav.=”$in”; ; /select> page of $lastpg";
}
}
?>
The pageft() function is called during paging. However, it does not output anything, but generates several global variables for use: $firstcount, $displaypg, $pagenav.
The following is an example of how to use the PHP paging function:
//(The previous procedure is omitted)
include("pageft.php");
//Include the "pageft.php" file
//Get the total number of information
$result=mysql_query("select
* from mytable”);
$total=mysql_num_rows($result);
//Call pageft() to display 10 pieces of information per page
(When using the default 20, you can omit this parameters),
use this page URL (default, so omitted).
pageft($total,10);
//The global variables generated now come in handy:
$result=mysql_query(”select *
from mytable limit $firstcount,
$displaypg “);
while($row=mysql_fetch_array($result)){
//(list content omitted)
}
//Output paging navigation bar code:
echo $ pagenav;
//(The following procedures are omitted)
?>

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft