PHP simple implementation of the previous page function example, the previous page

Source: Internet
Author: User

PHP simple implementation of the previous page function example, the previous page

The example in this article describes PHP's simple implementation of the previous page and next page function. We will share this with you for your reference. The details are as follows:

Train of Thought:

Many people now use id increment 1 and subtraction 1 to implement the previous and next articles, but won't the article ID be broken? So you need to know the last ID and ID is OK.

How can we solve this problem? It's very simple!

Example:

Assume that the ID200 of this Article

<A href = "? Action = up & id = 200 "> previous article </a> <a href = "? Action = down & id = 200 "> next </a>

If the previous article is implemented, write the function on the action = up page.

$ Id = $ _ GET ['id']; // previous: $ SQL = select * from article where id <'. $ id. 'order by id desc limit 0, 1 '; $ rs = mysql_query ($ SQL); $ row = mysql_fetch_array ($ rs); // next: $ SQL = select * from article where id <'. $ id. 'order by id asc limit 0, 1 '; $ rs = mysql_query ($ SQL); $ row = mysql_fetch_array ($ rs );

The query is smaller than the current ID (where id <'. $ id. 'The previous one) and greater than the current ID (where id> '. $ id. 'Next) One (limit) data, which is displayed in descending order (desc, previous) and ascending order (asc, next, the descending or ascending order can be omitted.

Specific implementation code: note that parameters need to be passed

The front end is called in the previous and next sections:

<? Php echo GetPreNext (pre, news, $ _ REQUEST [catid], $ _ REQUEST [id]);?> // Display the next function GetPreNext ($ gtype, $ table, $ catid, $ id) {$ preR = mysql_fetch_array (mysql_query ("select * from ". $ table. "where catid = ". $ catid. "and id <$ id order by id desc limit 0, 1"); // the last entry with a smaller id than the input id $ nextR = mysql_fetch_array (mysql_query ("select * from ". $ table. "where catid = ". $ catid. "and id> $ id order by id asc limit 0, 1"); // the last one with a higher id than the input id $ next = (is_array ($ nextR )? "Where id = {$ nextR ['id']}": 'Where 1> 2'); $ pre = (is_array ($ preR )? "Where id = {$ preR ['id']}": 'Where 1> 2'); $ query = "Select * from ". $ table. ""; $ nextRow = mysql_query ($ query. $ next); $ preRow = mysql_query ($ query. $ pre); if ($ PreNext = mysql_fetch_array ($ preRow) {echo $ PreNext ['pre'] = "previous: <a href = 'newsshow. php? Id = ". $ preR ['id']. "& catid = ". $ catid. "'> ". $ PreNext ['title']. "</a>" ;}else {echo $ PreNext ['pre'] = "previous: No";} if ($ PreNext = mysql_fetch_array ($ nextRow )) {echo $ PreNext ['Next'] = "next article: <a href = 'newsshow. php? Id = ". $ nextR ['id']. "& catid = ". $ catid. "'> ". $ PreNext ['title']. "</a>" ;}else {echo $ PreNext ['Next'] = "next: No ";}}

Code tested and available

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.