PHP implementation of the next article on the method of summing up _php skills

Source: Internet
Author: User

In this paper, we analyze the method of PHP implementation of the next article. Share to everyone for your reference, specific as follows:

PHP implementation of the next article this is mainly through the SQL to judge according to the current ID and then filter out the current ID before the data or ID data is so simple, specific we look.

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

The ID of the current article is $article _id, and the ID of the current article corresponds to the $cat_id, so the previous article should be:

Copy 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, get $max _id, and then

Copy Code code as follows:
SELECT article_id, title from article WHERE article_id = $max _id;

Simplify, and turn to subqueries namely:

Copy 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 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 Code code as follows:
SELECT article_id, title from article WHERE article_id = $min _id;

Simplify, and turn to subqueries namely:

Copy 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

Previous, the code is as follows:

Select ID from table where ID10 limit 0, 1;

This is certainly not a problem, but it is not a performance feeling.

SQL statement Optimization:

You can use union ALL to implement a statement to take 3 rows of data, but the premise is that 3 query fields to be the same, the result of this query is 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 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 > Ten ORDER BY id DESC limit 1);

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

Get current Browse Article ID:

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

Next article:

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

Previous article:

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

More interested in PHP related content readers can view the site topics: "Php+mysql Database Operation Introductory Course", "PHP+MYSQLI Database Design Skills Summary", "PHP object-oriented Programming Program", "PHP Array" Operation Techniques Encyclopedia, " Introduction to PHP Basic Grammar, PHP operations and Operator Usage Summary, PHP Network Programming skills Summary, PHP string (String) Usage summary and "PHP Common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

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.