The example of this article analyzes the method of implementing the previous article and the next article in PHP. Share it with everyone for your reference, the details are as follows:
php implements the previous article and the next article. This mainly uses 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 and next articles 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:
SELECT max(article_id) FROM article WHERE article_id < $article_id AND cat_id=$cat_id;
After executing this sql statement, we get $max_id, and then
SELECT article_id, title FROM article WHERE article_id = $max_id;
Simplify it and convert it into a subquery:
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:
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:
SELECT article_id, title FROM article WHERE article_id = $min_id;
Simplify it and turn it into a subquery:
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
select id from table where id10 limit 0,1;
(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);
$id = isset($_GET['id']) > 0 ? intval($_GET['id']) : "";
$query = mysql_query("SELECT id,title FROM article WHERE id>'$id' ORDER BY id ASC LIMIT 1"); $next = mysql_fetch_array($query);
$query = mysql_query("SELECT id,title FROM article WHERE id <'$id' ORDER BY id DESC LIMIT 1"); $prev = mysql_fetch_array($query);