Create backward/forward buttons for query results

Source: Internet
Author: User
Recently, SupportForums has many questions about how to create a link like & quot; backward 12345 forward & quot; for a search result. I hope the following script can help you to add this feature to your search results page. This example is designed for MySQL, but it can be easily adapted to other SQL engines. Because each application is different, in this way, I use some general statements for My recent Support Forums. There are many questions about how to create a link for a search result like "backward 1 2 3 4 5 forward. I hope the following script can help you to add this feature to your search results page. This example is designed for MySQL, but it can be easily adapted to other SQL engines.

Because each application is different, I use some general statements for MySQL Query. Replace the TABLE name with your actual TABLE name. Your conditional here should be replaced with YOUR where condition, and WHATEVER should be replaced with the field you want to sort the result (if YOUR application needs to be sorted in descending order, do not forget to add DESC ).


$ Limit = 20; // The Returned row.
$ Numresults = mysql_query ("select * from TABLE where your conditional here order by WHATEVER ");
$ Numrows = mysql_num_rows ($ numresults );

// Determine whether the offset has been passed to the script. if it is not set to 0
If (empty ($ offset )){
$ Offset = 0;
}

// Obtain 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 are displayed.
While ($ data = mysql_fetch_array ($ result )){
// Contains the result code as you wish.
}

// Next we need to generate links to other results

If ($ offset = 1) {// if offset is 0, the PREV link is ignored.
$ Prevoffset = $ offset-20;
Print "PREV n ";
}

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

// If there is no remainder after division, $ pages now contains the integer that requires the page
If ($ numrows % $ limit ){
// Add a page if there is a remainder
$ Pages ++;
}

For ($ I = 1; $ I <= $ pages; $ I ++) {// Loop
$ Newoffset = $ limit * ($ i-1 );
Print "$ I n ";
}

// Check whether it is the last page
If (! ($ Offset/$ limit) ==$ pages) & $ pages! = 1 ){
// If it is not the next page, a backward link is provided.
$ Newoffset = $ offset + $ limit;
Print "NEXT

N ";
}

?>

These may be useful to you. Of course, you may want to make HTML output cleaner...

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 parameters.

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.