Home > Backend Development > PHP Tutorial > Create backward/forward buttons for query results_PHP tutorial

Create backward/forward buttons for query results_PHP tutorial

WBOY
Release: 2016-07-21 16:02:36
Original
919 people have browsed it

There have been a lot of questions on the Support Forums recently about how to create a link that looks like "backward 1 2 3 4 5 forward" for a search result. I hope the script below helps you add this functionality to your search results pages. This example was written specifically for MySQL, but can be easily adapted for other SQL engines.

Because every 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 field by which you wish to sort the results (don't forget to add DESC if your application requires descending order).


$limit=20; // Returned rows
$numresults=mysql_query("select * from TABLE where CONDITIONAL HERE order by YOUR WHATEVER");
$numrows=mysql_num_rows($numresults);

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

// Get the result
$result=mysql_query("select id,name,phone ".
"from TABLE where YOUR CONDITIONAL HERE ".
"order by WHATEVER limit $offset,$limit");

// Now the returned results can be displayed
while ($data=mysql_fetch_array($result)) {
// Here Contains the code to display the results as you wish
}

// Then we need to generate links to other results

if ($offset==1) { // If offset is 0 , then ignore the PREV link
$prevoffset=$offset-20;
print "PREV n";
}

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

// If there is no remainder after division, $pages now contains the integer of the pages needed Value
if ($numrows%$limit) {
// If there is a remainder, add a page
$pages++;
}

for ($i=1;$i< ;=$pages;$i++) { // Loop
$newoffset=$limit*($i-1);
print "$ i n";
}

// Check if it is the last page
if (!(($offset/$limit)==$pages) && $pages!= 1) {
// If it is not the next page, give a backward link
$newoffset=$offset+$limit;
print "

n";
}

?>

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

Also, please note that the link after $PHP_SELF only contains $offset. If you need to pass parameters for the where condition of the query, you also need to add these above.​

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/316509.htmlTechArticleThere have been a lot of questions on the Support Forums recently about how to create a search result like backward 1 2 3 4 5 forward looking links. I hope the following script can help you in your search...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template