PHP implementation: summary of the method example in the previous article, and the previous article in the previous article

Source: Internet
Author: User

PHP implementation: summary of the method example in the previous article, and the previous article in the previous article

This article describes how to implement PHP in the previous article. We will share this with you for your reference. The details are as follows:

Php implements the next article in the previous article. This article mainly uses SQL to judge based on the current id and then filter out the data before or after the current ID, let's take a look at the specifics.

Implements the writing of SQL statements in the previous and next articles on the website.

The id of the current article is $ article_id, And the id of the corresponding category of the current article is $ cat_id, so the previous article should be:
Copy codeThe Code is as follows: SELECT max (article_id) FROM article WHERE article_id <$ article_id AND cat_id = $ cat_id;

Run this SQL statement and get $ max_id. Then
Copy codeThe Code is as follows: SELECT article_id, title FROM article WHERE article_id = $ max_id;

To simplify the process, convert it to subquery:
Copy codeThe Code is as follows: SELECT article_id, title FROM article WHERE article_id = (SELECT max (article_id) FROM article WHERE article_id <$ article_id AND cat_id = $ cat_id );
The code below is as follows:
Copy codeThe Code is as follows: SELECT min (article_id) FROM article WHERE article_id> $ article_id AND cat_id = $ cat_id;
Run this SQL statement and get $ min_id. Then:
Copy codeThe Code is as follows: SELECT article_id, title FROM article WHERE article_id = $ min_id;

To simplify the process, convert it to subquery:
Copy codeThe Code is as follows: SELECT article_id, title FROM article WHERE article_id = (SELECT min (article_id) FROM article WHERE article_id> $ article_id AND cat_id = $ cat_id );
Finally, many of my friends prefer to use the following statements:

In the previous article, the Code is as follows:

select id from table where id10 limit 0,1;

This is certainly no problem, but the performance is not very good.

SQL statement optimization:

You can use union all to obtain three rows of data in a statement, provided that the Three query fields are the same. The first row of the query result is the previous article, the second line is the current article, and the third line is the next article. The Code is as follows:
Copy codeThe Code is as follows: (select id from table where id <10 order by id asc limit 1) union all (select id from table where id = 10) union all (select id from table where id> 10 order by id desc limit 1 );
Now let's take a look at some examples in cms to implement phpcms in the previous article.

Obtain the id of the current browser article:

$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);

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.