Home > Article > Backend Development > Detailed explanation of how to implement the previous article and next article in PHP
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 code Code As follows:
SELECT max(article_id) FROM article WHERE article_id 562e80fe49597a3051646622822661cf $article_id AND cat_id=$cat_id;
After executing this sql statement, $min_id is obtained, and then:
Copy code The code is as follows:
SELECT article_id, title FROM article WHERE article_id = $min_id;
Simplify it and convert it into a subquery:
Copy code The 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;
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 code The code is as follows:
(select id from table where id 19215b0c3444c5a44837d39c76413a04 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']) : "";
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 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!