


Silently talk about PHP&MYSQL paging principle and implementation_PHP tutorial
Before reading this article, please make sure you have mastered some knowledge of PHP and the basics of MYSQL query operations.
As a web program, it often has to deal with countless data, such as member data and article data. If there are only a few dozen members, it is easy to handle. It can be displayed on one page. But what if If your website has thousands or even hundreds of thousands of members, if they are all opened on one page, it will be a torture for both the browser and the viewer.
I believe that every novice learning PHP will have a headache with pagination, but with Momo’s post, you will definitely pat your head and say, hey, it turns out that pagination is so simple? Indeed, please take a deep breath of fresh air now and listen carefully as Silence explains it to you bit by bit.
Suppose we want to process 1000 pieces of data and display 10 pieces on each page. In this case, it will be displayed in 100 pages. Let's first take a look at how to extract 10 pieces of information in mysql.
Select * from table limit 0,10
The above is a very simple mysql query statement. Its function is to extract 10 pieces of data from a table named table and put all The values of the fields are obtained.
The key part is in this section "limit 0,10". The 0 in it is 0 as the starting point, and the following 10 is to display 10 pieces of data, so we need to use 10 as the starting point. How to write the 20th piece of data displayed?
Maybe many people will say “limit 10,20” outright! Oh, this is wrong. The correct way to write it is "limit 10,10". The parameter after it is not the end point but the number to be extracted. Remember.
Now that you know how to extract 10 pieces of data, extracting 1,000 pieces means doing this kind of query 100 times, which means doing the following query:
Limit 0,10 Page
Limit 10,10 . Already? Yes, the first parameter increases by 10 every time the page is turned, but the second parameter remains unchanged.
That is to say, if we try to change the value of the first parameter according to the number of pages, we can display the data in pages. How about it? Is the principle very simple?
But how to change the value of the first parameter according to the number of pages? First, we need to have a page number value, which can be obtained using the GET method of the URL.
For example, index.php?page=18
I believe most of you are familiar with this thing. This kind of URL address can be found everywhere. The function of the page parameter is to pass in the number of pages to be displayed.
Let’s take a look at how it is implemented through a piece of code:
Copy code
/*
Author: silently
Date :2006-12-03
*/
$page =isset($_GET['page'])?intval($_GET['page']):1; //This sentence is to get the value of page in page=18. If page does not exist, then the number of pages is 1 .
$ num = 10; // Show 10 data per page
$ db = mysql_connect ("host", "name", "pass"); // Create a database connection
$ select = MySQL_SELECT_DB ("DB", $ DB); // Select the database you want to operate
/*
First of all, how much data we have to get in the database can judge how many pages of the specific pages, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones, specific ones. The formula is
The total number of data divided by the number of items displayed on each page, and the remainder is rounded to one.
In other words, 10/3=3.3333=4 If there is a remainder, we must round it up by one.
*/<<>
$ Total = MySQL_NUM_ROWS (mysql_query ("Select*from Table"); // The total number of data
$ Pagenum = Ceil ($ Total/$ Num); // Get the total number of pages
//If the page number parameter passed in is greater than the total number of pages, an error message will be displayed
If($page>$pagenum || $page == 0){
Echo "Error: Can Not Found The page.";
Exit;
}
$offset=($page-1)*$num; //Get the first parameter of limit Value, if the first page is (1-1)*10=0, the second page is (2-1)*10=10.
$info=mysql_query("select * from table limit $offset,$num"); //Get the data to be displayed for the corresponding page number
While($it=mysql_fetch_array($info)) {
Echo $it['name']."
";
} 🎜>For($i=1;$i<=$pagenum; $i++){
$show=($i!=$page)?"$i ":"$i";
Echo $show." ";
}
/*Display paging information, if it is the current page, it will be displayed in bold Number, and the remaining page numbers are hyperlinks. If the current page is the third page, it will be displayed as follows
1 2 3 4 5 6
*/
?>
Isn’t it very simple? Just use your brain to make it display more personalized. Let me give you a little question: how to implement the format of "Home Page, Previous Page, Next Page, Last Page" What about paging?
OK, finish filling the water post and call it a day. ^_^
Copy code
/*
Author: Silently
Date :2006-12-03
*/
$page=isset($_GET['page'])?intval($_GET['page']):1; //This sentence is to get the value of page in page=18. If page does not exist, then page The number is 1.
$num=10; // Display 10 pieces of data per page >mysql_select_db( "cr_download"); //Select the database to be operated
/*
First of all, we need to get how much data there is in the database to determine how many pages to divide into. The specific formula is
Total Divide the database by the number of items displayed on each page, and the remainder is one.
That is to say, 10/3=3.3333=4. If there is a remainder, we must round it up by one.
*/
$result=mysql_query("select * from cr_userinfo");
$total=mysql_num_rows($result); //Query all data
$url ='test.php';//Get the URL of this page
//Calculate the page number
$pagenum=ceil($total/$num); 🎜>$page=min($pagenum,$page);//Get the home page
$prepg=$page-1;//Previous page
$nextpg=($page==$pagenum ? 0 : $page+1);//Next page
$offset=($page-1)*$num; *10=0, the second page is (2-1)*10=10.
//Start paging navigation bar code:
$pagenav="Display page ".($total?($offset+1):0)."- ".min($offset+10,$total)." records, total $total records ";
//If there is only one page, jump out Function:
if($pagenum<=1) 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 loop through all page numbers:
$pagenav.="Go to page page , A total of $ pagenum pages ";
// If the number of pages passed in is greater than the total page number, the error message
if ($ page & gt; $ pagenum) {
Echo" error: Can Not Found The page ".$page;
Exit;
}
$info=mysql_query("select * from cr_userinfo limit $offset,$num"); //Get the corresponding page number The data to be displayed
While($it=mysql_fetch_array($info)){
Echo $it['username'];
echo "
";
} //Display Data
echo "
";
echo $pagenav;//Output paging navigation
?>
By the way, let’s dig deeper. In practical applications, paging is almost always used when it comes to lists. You can try to make a general paging function, so that this function can be called whenever paging is needed. Haha~~

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

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



In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

To fill in the MySQL username and password: 1. Determine the username and password; 2. Connect to the database; 3. Use the username and password to execute queries and commands.

1. Use the correct index to speed up data retrieval by reducing the amount of data scanned select*frommployeeswherelast_name='smith'; if you look up a column of a table multiple times, create an index for that column. If you or your app needs data from multiple columns according to the criteria, create a composite index 2. Avoid select * only those required columns, if you select all unwanted columns, this will only consume more server memory and cause the server to slow down at high load or frequency times For example, your table contains columns such as created_at and updated_at and timestamps, and then avoid selecting * because they do not require inefficient query se

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

Copy and paste in MySQL includes the following steps: select the data, copy with Ctrl C (Windows) or Cmd C (Mac); right-click at the target location, select Paste or use Ctrl V (Windows) or Cmd V (Mac); the copied data is inserted into the target location, or replace existing data (depending on whether the data already exists at the target location).

Detailed explanation of database ACID attributes ACID attributes are a set of rules to ensure the reliability and consistency of database transactions. They define how database systems handle transactions, and ensure data integrity and accuracy even in case of system crashes, power interruptions, or multiple users concurrent access. ACID Attribute Overview Atomicity: A transaction is regarded as an indivisible unit. Any part fails, the entire transaction is rolled back, and the database does not retain any changes. For example, if a bank transfer is deducted from one account but not increased to another, the entire operation is revoked. begintransaction; updateaccountssetbalance=balance-100wh

View the MySQL database with the following command: Connect to the server: mysql -u Username -p Password Run SHOW DATABASES; Command to get all existing databases Select database: USE database name; View table: SHOW TABLES; View table structure: DESCRIBE table name; View data: SELECT * FROM table name;
