Php retrieves the previous and next pages of an article,
This example describes how php retrieves the previous and next pages of an article. Share it with you for your reference. The specific method is as follows:
Today, I found that there was a problem with the previous and next pages of a website. The previous page was okay, but on the next page, I went straight to the latest article published on this channel, the principle should be that the previous and subsequent IDs of the article itself are the top and bottom pages. I will introduce them to you in detail below.
Let's take a look at the example below: 1, 2, 3, 4, 5.
If the five above are IDs, I want to sort the previous pages directly using order by ID desc. If my current id is 3, then the result is 2, for the next page, the order we use order by id asc should be 4. The principle is so simple.
The Code is as follows:
Previous Page:Copy codeThe Code is as follows: $ SQL = "Select field from table name where id <3 order by id desc limit 1 ";
Next page:Copy codeThe Code is as follows: $ SQL = "Select field from table name where id> 3 order by id asc limit 1 ";
The result is the same as what we thought. Finally, I will share a previously written function with you. The Code is as follows:
Copy codeThe Code is as follows :/*
Previous Page, next page
Int $ tag 0 Previous Page, 1 next page
Int $ fid
*/
Function nextPre ($ tag = 0, $ zid, $ fid)
{
If ($ tag)
{
$ SQL = "Select field from table name where id <$ fid order by id desc limit 1 ";
}
Else
{
$ SQL = "Select field from table name where id> $ fid order by id asc limit 1 ";
}
// $ SQL = "Select * from table name where order by id desc limit 0, 10 ";
$ Result = mysql_query ($ SQL) or die ('query error ');
If (mysql_num_rows ($ result ))
{
$ Rs = mysql_fetch_array ($ result );
Return "<a href =" ". $ rs ['s _ url']." ">". $ rs ['title']. "</a> ";
}
Else
{
Return 'no longer ';
}
}
I hope this article will help you with PHP programming.