Php retrieves the previous and next pages of an article,

Source: Internet
Author: User

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.

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.