Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
Recently, many people in the forum asked me how to display the query results on pages. I hope the following code will help you improve your program. These codes are used for MYSQL, but can be easily transplanted to other SQL statements.
Due to the special nature of each program, I used some common statements in MYSQL queries. Replace TABLE with your TABLE name; replace YOUR_CONDITION_HERE with your condition statement; replace WHATEVER with the field name you want to sort by it (if you want to sort it in reverse order, do not forget to add the DESC clause ).
$ Qh = mysql_query ("select count (*) AS rcnt from table where YOUR_CONDITION_HERE order by whatever ");
$ Data = mysql_fetch_array ($ qh );
$ Nr = $ data ["rcnt"];
// Determine whether the offset parameter is passed to the script. If not, use the default value 0.
If (empty ($ offset ))
$ Offset = 0;
// Query result (20 entries per page, but you can change it by yourself)
$ Result = mysql_query ("SELECT id, name, phone from table where YOUR_CONDITION_HERE order by whatever limit $ offset, 20 ");
// Display the 20 returned Records
While ($ data = mysql_fetch_array ($ result ))
// Use the code you used to display the returned record
// Next, write the link to another page
If (! $ Offset) // if the offset is 0, the link to the previous page is not displayed.
$ Preoffset = $ offset-20;
Print "<a href =" $ PHP_SELF? Offset = $ preoffset "> Previous Page </a> & nbsp ;";
// Calculate the total number of required pages
$ Pages = ceil ($ nr/20); // $ pages variable now contains the required page number
For ($ I = 1; $ I <= $ pages; $ I)
$ Newoffset = 20 * $ I;
Print "<a href =" $ PHP_SELF? Offset = $ newoffset "> $ I </a> & nbsp ;";
// Check whether it is the last page
If ($ pages! = 0 & ($ newoffset/20 )! = $ Pages)
Print "<a href =" $ PHP_SELF? Offset = $ newoffset "> Next page </a> & nbsp ;";
This just gives you an overview of how to display the query results by page. You can complete other functions by yourself.
Note: $ PHP_SELF has only one offset parameter. You can add your own items as needed. This method is inefficient at querying tables with more than one million records.
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to firstname.lastname@example.org. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
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:
and provide relevant evidence. A staff member will contact you within 5 working days.