search
HomeBackend DevelopmentPHP TutorialPHP paging program implementation code for PHP development_PHP tutorial

PHP paging program implementation code for PHP development_PHP tutorial

Jul 13, 2016 pm 04:55 PM
phpphp+mysqlcodePaginationexistaccomplishopenrelativelyprogramSimpleObtainwant

It is relatively simple to implement paging in php+mysql. Just get the page and then For reference.

Project structure:

Operation effect:

Database connection code

The code is as follows Copy code
 代码如下 复制代码

$conn = @ mysql_connect("localhost", "root", "") or die("数据库链接错误"); mysql_select_db("form", $conn);
mysql_query("set names 'GBK'"); //使用GBK中文编码;
//替换空格,回车键
function htmtocode($content)
{
$content = str_replace("n", "
", str_replace(" ", " ", $content));
return $content;
}
?>

$conn = @ mysql_connect("localhost", "root", "") or die("Database link error"); mysql_select_db("form", $conn);
mysql_query("set names 'GBK'"); //Use GBK Chinese encoding;
//Replace spaces and enter key
function htmtocode($content)
{
$content = str_replace("n", "
", str_replace(" ", " ", $content));
return $content;
}
?>

Here is a more important sharing core function

The code is as follows Copy code


Function _PAGEFT($totle, $displaypg = 20, $url = '') {

global $page, $firstcount, $pagenav, $_SERVER;

            $GLOBALS["displaypg"] = $displaypg;

          if (!$page)
$page = 1;
             if (!$url) {
               $url = $_SERVER["REQUEST_URI"];
         }

//URL analysis:
            $parse_url = parse_url($url);
          $url_query = $parse_url["query"]; //Get the query string of the URL separately
             if ($url_query) {
                $url_query = ereg_replace("(^|&)page=$page", "", $url_query);
                $url = str_replace($parse_url["query"], $url_query, $url);
                 if ($url_query)
                           $url .= "&page";
            else
                       $url .= "page";
          } else {
                  $url .= "?page";
         }
​​​​​ $lastpg = ceil($totle / $displaypg); //The last page, 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 number " . ($totle ? ($firstcount +1) : 0) . "-" . min($firstcount + $displaypg, $totle) . " records, $totle records in total";

//If there is only one page, jump out of the function:
If ($lastpg               return false;

               $pagenav .= " Homepage ";
            if ($prepg)
                 $pagenav .= " Previous page ";
        else
                $pagenav .= "Previous page";
            if ($nextpg)
                  $pagenav .= " Next page ";
        else
                 $pagenav .= "Next page";
             $pagenav .= " Last page ";

//Pull down the jump list and list all page numbers in a loop:
             $pagenav .= "Go to page page of $lastpg";
}


include("conn.php");

$result=mysql_query("SELECT * FROM `test`");
$total=mysql_num_rows($result);
//Call pageft() to display 10 pieces of information per page (this parameter can be omitted when using the default 20), and use the URL of this page (default, so omit it).
_PAGEFT($total,5);
echo $pagenav;

$result=mysql_query("SELECT * FROM `test` limit $firstcount,$displaypg ");
while($row=mysql_fetch_array($result)){

echo "


".$row[name]." | ".$row[sex];

}
?>

list.php

Database query records and generate sql query statements

The code is as follows
 代码如下 复制代码
include("conn.php");
$pagesize=5; 5 $url=$_SERVER["REQUEST_URI"];
$url=parse_url($url);
$url=$url[path];
$numq=mysql_query("SELECT * FROM `test`");
$num = mysql_num_rows($numq);
if($_GET[page]){
$pageval=$_GET[page];
$page=($pageval-1)*$pagesize;
$page.=',';
}
if($num > $pagesize){
if($pageval echo "共 $num 条". 21 " 上一页 下一页";
}
$SQL="SELECT * FROM `test` limit $page $pagesize ";
$query=mysql_query($SQL);
while($row=mysql_fetch_array($query)){
echo "
".$row[name]." | ".$row[sex];
}
 ?>
Copy code
include("conn.php"); $pagesize=5; 5 $url=$_SERVER["REQUEST_URI"]; $url=parse_url($url); $url=$url[path]; $numq=mysql_query("SELECT * FROM `test`"); $num = mysql_num_rows($numq); if($_GET[page]){ $pageval=$_GET[page]; $page=($pageval-1)*$pagesize; $page.=','; } if($num > $pagesize){ if($pageval echo "Total $num items". 21 " Previous page Next page"; } $SQL="SELECT * FROM `test` limit $page $pagesize "; $query=mysql_query($SQL); while($row=mysql_fetch_array($query)){ echo "
".$row[name]." | ".$row[sex]; } ?>

Paging formula: (current page number - 1) * number of items per page, number of items per page

 代码如下 复制代码
sql语句:select * from test_table limit ($page-1)*$pageSize,$pageSize;

Summary:

No matter what program is developed, it is divided into one. The original method is to take N items starting from Take 5 out of 1.

Let’s introduce the core code. Here we get the paging number, and the Xpagesize code is as follows

 代码如下 复制代码
if($_GET[page]){
$pageval=$_GET[page];
$page=($pageval-1)*$pagesize;
$page.=',';
}
if($num > $pagesize){
if($pageval


It is relatively easy to divide in mysql+php because of limit

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631629.htmlTechArticleIt is relatively simple to implement paging in php+mysql. Just get the page and then Paging can be perfectly realized by using limit n, M. This example clearly explains the need...
Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

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 and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

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.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

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 and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

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.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

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

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

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.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Safe Exam Browser

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.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Atom editor mac version download

Atom editor mac version download

The most popular open source editor