Implementation of pagination display under Php+mysql

Source: Internet
Author: User
Tags mysql mysql query
mysql| page | Show recently, there are people in the forum who are asking how to implement the paging display of query results. I hope the following code will help you improve your program. The code is for MySQL, but it's easy to migrate to other SQL.

Because of the specificity of each program, I used some very common statements in the MySQL query. Replace the table with your form name, replace Your_condition_here with your conditional statement, and replace whatever with the field name you want to sort by (and, of course, if you want to order the reverse, don't forget to add the DESC clause).

<?php

$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 if the offset parameter is passed to the script, and if not, use the default value 0

if (empty ($offset))

{

$offset=0;

}

Query results (Here are 20 per page, but you can completely change it)

$result=mysql_query ("Select Id,name,phone from TABLE WHERE your_condition_here order by WHATEVER LIMIT $offset, 20");



Show 20 Records returned

while ($data=mysql_fetch_array ($result))

{

For the code you used to display the return record.

}

Next, write a 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 </a> \ n";

}

Calculate the total number of pages required

$pages=ceil ($NR/20); The $pages variable now contains the required number of pages

for ($i=1; $i <= $pages; $i++)

{

$newoffset=20*$i;

Print "<a href=\" $php_self?offset=$newoffset\ ">$i</a> \ n";

}

Check if it's the last page

if ($pages!=0 && ($NEWOFFSET/20)!=$pages)

{

Print "<a href=" $php_self?offset=$newoffset "> next page </a> \ n";

}

?>

This is just to give you a general introduction to the implementation of the Query results page display method, the other features you do yourself.

Note two: $php_self only an offset parameter, you can add your own things as needed; This approach is inefficient for queries that contain tables above millions records.

Related Article

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.