Home > Backend Development > PHP Tutorial > Summary of method examples of PHP implementation of the previous article and the next article

Summary of method examples of PHP implementation of the previous article and the next article

高洛峰
Release: 2023-03-05 13:12:01
Original
1650 people have browsed it

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;
Copy after login

After executing this sql statement, we get $max_id, and then

SELECT article_id, title FROM article WHERE article_id = $max_id;
Copy after login

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);
Copy after login

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;
Copy after login

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

SELECT article_id, title FROM article WHERE article_id = $min_id;
Copy after login

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);
Copy after login

Finally, many friends like to use the following statement


##Previous one Chapter, the code is as follows:

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

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

SQL statement optimization:

You can use Union all implements a statement to fetch 3 rows of data, but the premise is that the fields of the three queries must be the same. The first row of the result of this query is the previous article, the second row is the current article, and the third row is the next article. Article, the 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);
Copy after login

Now let’s look at some examples in cms phpcms implements the previous article and the next article.

Get the current browsed article id:

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

Next Previous article:

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

Previous article:

$query = mysql_query("SELECT id,title FROM article WHERE id <&#39;$id&#39; ORDER BY id DESC LIMIT 1");
$prev = mysql_fetch_array($query);
Copy after login
I hope this article will be helpful to everyone in PHP programming.

For more PHP implementation methods and examples of the previous and next articles, please pay attention to the PHP Chinese website for related articles!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template