Detailed explanation of how to implement the previous article and next article in PHP

墨辰丷
Release: 2023-03-28 22:18:01
Original
3193 people have browsed it

This article mainly introduces the method of realizing the previous and next article in PHP. It summarizes and analyzes the query skills related to SQL operations of PHP to obtain the previous and next article in the form of examples. Friends in need can refer to the following

php implements the previous article and the next article mainly through sql to judge based on the current ID and then filter out the data before the current ID or the data after the ID. It’s that simple. Let’s take a look at the details.

Realize how to write the sql statements of the previous article and the next article in the website article.

The id of the current article is $article_id, and the id of the corresponding category of the current article is $cat_id, then the previous article should be:

Copy codeCode As follows:

SELECT max(article_id) FROM article WHERE article_id < $article_id AND cat_id=$cat_id;

After executing this sql statement, $max_id is obtained, and then

Copy codeThe code is as follows:

SELECT article_id, title FROM article WHERE article_id = $max_id;

Simplify it and convert it into a 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 next article is, the code 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;


After executing this sql statement, $min_id is obtained, and then:

Copy codeThe code is as follows:

SELECT article_id, title FROM article WHERE article_id = $min_id;

Simplify it and convert it into a 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 friends like to use the following statement

Previous article, the code is as follows:

select id from table where id10 limit 0,1;
Copy after login

There is definitely no problem in this way , but the performance is not very good.

SQL statement optimization:

You can use union all to fetch 3 rows of data in one statement, but the prerequisite is 3 The fields of the query must be the same. The first line 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 look at some examples in cms phpcms implements the previous article and the next article.

Get the current browsing article id:

$id = isset($_GET['id']) > 0 ? intval($_GET['id']) : "";
Copy after login

Next post:

$query = mysql_query("SELECT id,title FROM article WHERE id>'$id' ORDER BY id ASC LIMIT 1"); $next = mysql_fetch_array($query);
Copy after login

Previous post:

$query = mysql_query("SELECT id,title FROM article WHERE id <'$id' ORDER BY id DESC LIMIT 1"); $prev = mysql_fetch_array($query);
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Example of PHP production of abnormal login IP detection function

Example of PHP implementation of thief program

phpExport csv file, leading 0 instances can be exported

The above is the detailed content of Detailed explanation of how to implement the previous article and next article in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!