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