PHP implementation of the next article on the method of the detailed

Source: Internet
Author: User
This article mainly introduces the PHP implementation of the next article of the method, combined with an example of the form of a summary of the next article on the PHP SQL operation related query skills, the need for friends can refer to the following

PHP implementation of the next article is mainly through the SQL to judge based on the current ID and then filter out the current ID before the data or the ID of the data after it is so simple, specific we take a look.

The implementation of the Web site article in the previous and next SQL statement writing.

The current article ID is $article _id, the current article corresponding to the category ID is $cat_id, then the previous article should be:

Copy the Code code as follows:

SELECT Max (article_id) from article WHERE article_id < $article _id and cat_id= $cat _id;

After executing this SQL statement, you get $max _id, and then

Copy the Code code as follows:

SELECT article_id, title from article WHERE article_id = $max _id;

To simplify, turn to subquery:

Copy the Code code as follows:

Select article_id, title from article where article_id = (SELECT max (article_id) from article where article_id < $artic le_id and cat_id= $cat _id);


For the next article, the code is as follows:

Copy the Code code as follows:

SELECT min (article_id) from article WHERE article_id > $article _id and cat_id= $cat _id;


After executing this SQL statement, you get $min _id, and then:

Copy the Code code as follows:

SELECT article_id, title from article WHERE article_id = $min _id;

To simplify, turn to subquery:

Copy the Code code as follows:

Select article_id, title from article where article_id = (SELECT min (article_id) from article where article_id > $artic le_id and cat_id= $cat _id);


Finally, a lot of friends like to use the following statement

In the previous article, the code is as follows:

Select ID from table where ID10 limit 0, 1;

This is certainly not a problem, but the performance is not feeling very much.

SQL statement Optimization:

You can use union ALL to implement a statement to take 3 rows of data, but only if 3 query fields to be the same, this query out the results of the first line is the previous article, the second line is the current article, the third line is the next article, the code is as follows:

Copy the Code code as follows:

(select ID from table where ID < ten ORDER BY ID ASC limit 1) UNION ALL (select ID from table where id = ten) UNION ALL ( Select ID from table where ID > The ORDER BY id DESC limit 1);


Now look at some examples of CMS PHPCMS implementation of the next article.

Get the current browse article ID:

$id = Isset ($_get[' id ') > 0? Intval ($_get[' id '): "";

Next post:

$query = mysql_query ("Select Id,title from article WHERE id> ' $id ' ORDER by ID ASC LIMIT 1"); $next = Mysql_fetch_array ($ query);

Previous post:

$query = mysql_query ("Select Id,title from article WHERE ID < ' $id ' ORDER by ID DESC LIMIT 1"); $prev = Mysql_fetch_array ($query);

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.