Recently, many people in the forum asked me how to display the query results on pages. I hope that the following code will help you improve your program. These codes are used for MYSQL, but can be easily transplanted to other SQL statements. Since each program is recently released, many people in the forum are asking how to display the query results by page. I hope that the following code will help you improve your program. These codes are used for MYSQL, but can be easily transplanted to other SQL statements.
Because of the special nature of each program, I applied 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 expect 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, the default value 0 is applied.
If (empty ($ offset ))
{
$ Offset = 0;
}
// Query results (20 entries per page, but you can change the results 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 'previous Page \ n ';
}
// Calculate the total number of pages required
$ Pages = ceil ($ nr/20); // $ pages variable now contains the required page number
For ($ I = 1; $ I <= $ pages; $ I)
{
$ Newoffset = 20 * $ I;
Print '$ I \ n ';
}
// Check whether it is the last page
If ($ pages! = 0 & ($ newoffset/20 )! = $ Pages)
{
Print 'next page \ n ';
}
?>
This just shows you how to display the query results by page. you can do other functions by yourself.
Pay attention to two points: $ PHP_SELF has only one offset parameter. you can participate in your own items as needed. this method is inefficient at querying tables that contain more than one million records.
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