


A brief discussion on the fifth bullet of PHP---The Nine-Yang Magic of Pagination_PHP Tutorial
In the above article http://www.BkJia.com/kf/201204/128413.html I told you about the operating mechanism and use of recursive functions. I don’t know if you understand it. If you don’t understand, you can ask questions below. , I will help you answer it at any time.
In the past two days, many students have added questions to the paging when asking questions. So today I will tell you in detail the application of paging technology:
We often see beautiful pagination in some web applications,
Clicking the next page will display the content of the next page, which makes the page more beautiful.
Okay, after reading the explanation in this article, I believe that students can also write beautiful pagination
Now I have a data table named mytable with the following data:
id name pass age sex
1 Goudan wrw7921sqa5eb72c92a549dd5avfr112 20 1
2 Gao Fushuai vdf79218965eb72c92a549dd5a334534 20 0
3 Jie Ge gbj792saa65eb72c92a549dd5avfd321 22 1
4 Band of Brothers 45379218965eb72ccdd549dd5a330342 24 1
5 lampbrothers cdc79218965eb72c92a549dd5vsfdwsx 24 1
6 lamp ret79218965eb72c92a549dd5a33bgyt 25 0
7 php 123qwe18965eb72c92a549dd5a330875 26 1
8 mysql 76e79218965eb72c92a549ddvfdd0dsa 28 0
9 apache 54ebnm18965eb72c92a549dd5a330cxz 30 1
First, make a paging link
echo "Previous page
";
echo "Next page
";
echo "Total items";
?>
Let’s think about it, click on the previous page, the current page number will be reduced by one, and the data of the previous page will be displayed
Suppose we use a variable $page to control the current page number, then should the previous page be $page-1
The page number of the next page is $page+1
So our link above should be changed to:
echo "Previous page
";
echo "Next page
";
?>
This way everyone should understand clearly how to pass the page numbers of the previous and next pages
But in actual development, it is impossible for a web software to allow users to pass in page numbers. When users visit the page, they will only access index.php
So we will handle this situation accordingly:
$page = !empty($_GET['page'])?$_GET['page']:1
?>
If our $page is not given at this time, that is, $_GET['page'] is empty, we directly assign the value 1 to $page
Next step, let’s display the data separately, that is to say, the first page only displays the data of the first page, and the second page only displays the data of the second page.....Only the Nth page is displayed. Data on page N
In this case, we need to operate the sql statement. If we want to display the data in pages, we use the limit clause of the sql statement
Okay, then we think, if each page of data only displays 5 records, what should the SQL statement look like?
Then ours
First page:
Should the displayed data from the first to fifth items be "limit 0,5";
Second page:
The displayed data from the sixth to the tenth item should be "limit 5,5";
Page 3:
The displayed data from the 11th to 15th items should be “limit 10,5”;
............
Let’s look at a pattern,
Look carefully, should our nth page be
limit (n-1)*Number of items displayed on each page, number of items displayed on each page
In other words, the data we need include the current page number and the number of items displayed on each page
Okay, as we just said, we can get the current page number through $_GET['page'], then we can use a variable to define the number of items displayed on each page. Skilled students can define a constant and put it Go to the configuration file,
Here we use variables to define and set the page size to 5
$page = $_GET['page'];
$pagesize = 5;
The limit clause of the assembled sql statement should be "limit ($page-1)*$pagesize,$pagesize"
At this point, we have been able to paginate the data,
But if we want to get the total number of pages, we also need to count the total number of records from the database
$sql = "select count(*) from mytable";
$result = mysql_query($sql);
We can take out the execution result using the mysql_result() function.
$count = mysql_result($result,0);
Okay, then our current total number of records is $count,
Now our total number of data items is 9, and each page displays 5 items. Think about it, should our total number of pages be 2, that is, ceil(9/5)=2
The formula for calculating the total number of pages is ceil($count/$pagesize),
Okay, all the data we need has been obtained, finally assembled into a sql statement and displayed, it should be:
//Set how many records to display on each page
$pagesize = 5;
//Get the current page number
$page = !empty($_GET['page'])?$_GET['page']:1;
//Total number of records
$sql = "select count(*) from mytable";
$result = mysql_query($sql);
$count = mysql_result($result,0);
//How many pages are there in total
$page_count = ceil($count/$pagesize);
//Determine whether the current given page number is out of bounds
if($page<1){
$page=1;
}elseif($page>$page_count){
$page=$page_count;
}
//Start assembling sql statements
$sql = "select id,name,age,sex from mytable limit ".($page-1)*$pagesize.','.$pagesize;
//Send sql statement
$result = mysql_query($sql);
//Output form
echo "
Username | Age | Gender | ";
---|---|---|
".$row['name']." | ";".$row['age']." | ";".$sex[$row['sex']]." | ";
//Add pagination to display links
$up = (($page-1)<1)?1:($page-1);
$down = (($page+1)>$page_count)?$page_count:($page+1);
echo "Previous page ";
echo "Next page ";
echo "Total".$page_count."page";
echo "Current page ".$page." ";
echo "Total".$count."records";
?>
Okay, so we have added the paging function. How about it? Isn’t it very simple?
Let me summarize the specific steps of paging:
Step one: Set the page size (how many items to display on each page)
Step 2: Get the current page number
Step 3: Get the total number of records
Step 4: Get the total number of pages
Step 5: Determine whether the current given page number is out of bounds
Step 6: Assemble sql statement
Step 7: Send sql statement to mysql server
Step 8: Traverse the result set and output
Step 9: Add pagination links
As long as you remember the above martial arts secrets of Jie Ge’s nine-yang magic skill, I believe that students will be able to leisurely ride through the martial arts killing fields of PHP
Author zdrjlamp

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

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

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,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
