Data and segmentation on the PHP/MySQL selection page

Source: Internet
Author: User
This tutorial will show you how to select data from a MySQL database, split it on multiple pages, and display the page number it uses. We have a MySQL table named student 90 record and the following fields: name-the data type is varchar (250) ******-varchar (250), but it is done... syntaxHighlighter. a

This tutorial will show you how to select data from a MySQL database, split it on multiple pages, and display the page number it uses. We have a MySQL table named "student" 90 record and the following fields: name-data type: varchar (250) ******-varchar (250 ), instead, we make a single SELECT query and display all 90 records. we can have 5 pages and 20 records on one page, each containing up. To do this, we need to use the SELECT command of the LIMIT clause, so we can LIMIT the query to display only 20 records. The LIMIT clause also allows you to specify the start record. For example, this query

$ SQL = "SELECT * FROM students ORDER BY name ASC LIMIT 0, 20 ";
20 records are returned, sorted by name from the first record. Query
$ SQL = "SELECT * FROM students ORDER BY name ASC LIMIT 50, 20 ";
Sorts the name again, but at this time, it records 20 records from 50th.
Therefore, this clause (restriction start, count) specifies the starting record and count to specify how many records are displayed. What we will do next is to make a php file called pagination. php to display the table 20th from our records. Select the code below and print the data in the table.

If (isset ($ _ GET ["page"]) {$ page =$ _ GET ["page"] ;}else {$ page = 1 ;};
$ Start_from = ($ page-1) * 20;
$ SQL = "SELECT * FROM students ORDER BY name ASC LIMIT $ start_from, 20 ";
$ Rs_result = mysql_query ($ SQL, $ connection );
?>











While ($ row = mysql_fetch_assoc ($ rs_result )){?> };?>
Name Phone

Now, when you open pagination. php in your web browser, you will see more than 20th rows of code in the 2nd records table displayed from your "student" table
If (isset ($ _ GET ["page"]) {$ page =$ _ GET ["page"] ;}else {$ page = 1 ;};
$ Start_from = ($ page-1) * 20;
It is used to create a variable $ start_from, depending on the page we want to view. Later, you will see that we will use different page URLs (such as pagination. php? = 2 pages ). Next, we need to find a record of the total quantity we need on our table and page. To do this, run another query using the COUNT () function. Webmaster Encyclopedia

$ SQL = "SELECT COUNT (Name) FROM students ";
$ Rs_result = mysql_query ($ SQL, $ connection );
$ Row = mysql_fetch_row ($ rs_result );
$ Total_records = $ row [0];
USD total_records is now equal, we have 90 records in our database in our example. We have 20 records per page so that the total number of required pages is 5 (4 pages, 20 records and the last page will have 10 records ). Calculate the number of webpages that require PHP to use the CEIL () function.

$ Total_pages = ceil ($ total_records/20 );
Divide the total number of records by the records on each page, and then the CEIL () function rounds up the result. Now we have two new variables-$ total_records equals $90 total_pages equals 5
The page number to be printed and the website address of the associated company. each number will be recycled.

For ($ I = 1; $ I <= $ total_pages; $ I ++ ){
Echo "". $ I ."";
};
?>
The code above will print numbers from 1 to 5, and each number will be created differently. you can see each link. this is the transfer of different page values in the SELECT query above. Finally, you should have a file like this (remember to add the MySQL connection string ):

If (isset ($ _ GET ["page"]) {$ page =$ _ GET ["page"] ;}else {$ page = 1 ;};
$ Start_from = ($ page-1) * 20;
$ SQL = "SELECT * FROM students ORDER BY name ASC LIMIT $ start_from, 20 ";
$ Rs_result = mysql_query ($ SQL, $ connection );
?>











While ($ row = mysql_fetch_assoc ($ rs_result )){?> };?>
Name Phone

$ SQL = "SELECT COUNT (Name) FROM students ";
$ Rs_result = mysql_query ($ SQL, $ connection );
$ Row = mysql_fetch_row ($ rs_result );
$ Total_records = $ row [0];
$ Total_pages = ceil ($ total_records/20 );
 
For ($ I = 1; $ I <= $ total_pages; $ I ++ ){
Echo "". $ I ."";
};
?>
This pagination. php file will print up to 20 records per page and 5 pages at the bottom point to a table with 20 different records displayed on a page. Don't forget a small fee. I can add all your php files by page. Let me know that if you need help, I will give you a quote.

 


Author: newcnzz

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.