Create Backward/forward buttons for query results

Source: Internet
Author: User
Tags mysql query prev table name
Recently, there have been a lot of questions about support forums about how to make a link like "Back 1 2 3 4 5 forward" for a search result. I hope the following script will help you to add this functionality to your search results page. This example is written specifically for MySQL, but it can be easily adapted to other SQL engines.

Because each application is different, I use some common statements for MySQL query processing. The table name should be replaced with your actual table name. YOUR CONDITIONAL here should be replaced with your where condition, and whatever should be replaced with the fields you want to sort the results (if your application needs to be sorted in descending order, don't forget to add desc).

<?php

$limit = 20; The Returned row
$numresults =mysql_query ("select * from TABLE where YOUR CONDITIONAL to order by WHATEVER");
$numrows =mysql_num_rows ($numresults);

Then determine if offset has been passed to the script, if not set to 0
if (empty ($offset)) {
$offset = 0;
}

Take the result
$result =mysql_query ("select Id,name,phone").
"From TABLE where YOUR CONDITIONAL".
"ORDER by WHATEVER limit $offset, $limit");

Now we can show you the results returned.
while ($data =mysql_fetch_array ($result)) {
Here contains the display result code as you wish
}

Then we need to generate links to other results.

if ($offset ==1) {//If offset is 0, prev link is ignored
$prevoffset = $offset-20;
Print "<a href=" $PHP _self?offset= $prevoffset ">PREV</a> n";
}

Calculate the number of pages that need to be linked
$pages =intval ($numrows/$limit);

If there is no remainder, $pages now contains the integer value that requires the page
if ($numrows% $limit) {
Add a page if you have more than a few
$pages + +;
}

for ($i =1; $i <= $pages; $i + +) {//Loop
$newoffset = $limit * ($i-1);
Print "<a href=" $PHP _self?offset= $newoffset "> $i </a> n";
}

Check if the last page
if (!) ( ($offset/$limit) = = $pages) && $pages!=1) {
If it is not the next page, give a backward link
$newoffset = $offset + $limit;
Print "<a href=" $PHP _self?offset= $newoffset ">next</a><p>n";
}

?>

These may be of some use to you. Of course, you might want to make HTML output cleaner ...

Also, please note that the link after the $php_self contains only the $offset. If you need to pass parameters for the query's where condition, you also need to make these up.

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.