PHP to get the article previous page and next page method _php tips

Source: Internet
Author: User
Tags php programming

This article is an example of how PHP gets the previous and next pages of the article. Share to everyone for your reference. The specific methods are as follows:

Today found a station on the previous page with the next page problem, previous page no problem but the next page is directly to the latest release of the article, according to the principle should be the ID of the article itself before and after the ID is the next page, I will tell you in detail.

Let's look at an example: 1,2,3,4,5.

If the top 5 is an ID, I want to sort the previous page by using the ORDER by id DESC, if my current ID is 3 then the 2 is right, on the next page we want to use ORDER by ID ASC to the sort should be, 4, good principle is so simple.

SQL implementation method, the code is as follows:

Previous page:

Copy Code code as follows:
$sql = "Select field from table name where id<3 ORDER BY id desc limit 1";

Next page:

Copy Code code as follows:
$sql = "Select field from table name where ID>3 ORDER by ID ASC limit 1";

The results are the same as we think, okay. Finally, I share a function I wrote before, the code is as follows:

Copy Code code 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";
$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> ";
Return ' no ';

I hope this article will help you with your PHP programming.

Related Article

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