This article mainly introduces the PHP implementation of the next article of the method, combined with an example of the form of a summary of the next article on the PHP SQL operation related query skills, the need for friends can refer to the following
PHP implementation of the next article is mainly through the SQL to judge based on the current ID and then filter out the current ID before the data or the ID of the data after it is so simple, specific we take a look.
The implementation of the Web site article in the previous and next SQL statement writing.
The current article ID is $article _id, the current article corresponding to the category ID is $cat_id, then the previous article should be:
Copy the Code code as follows:
SELECT Max (article_id) from article WHERE article_id < $article _id and cat_id= $cat _id;
After executing this SQL statement, you get $max _id, and then
Copy the Code code as follows:
SELECT article_id, title from article WHERE article_id = $max _id;
To simplify, turn to subquery:
Copy the Code code as follows:
Select article_id, title from article where article_id = (SELECT max (article_id) from article where article_id < $artic le_id and cat_id= $cat _id);
For the next article, the code is as follows:
Copy the Code code as follows:
SELECT min (article_id) from article WHERE article_id > $article _id and cat_id= $cat _id;
After executing this SQL statement, you get $min _id, and then:
Copy the Code code as follows:
SELECT article_id, title from article WHERE article_id = $min _id;
To simplify, turn to subquery:
Copy the Code code as follows:
Select article_id, title from article where article_id = (SELECT min (article_id) from article where article_id > $artic le_id and cat_id= $cat _id);
Finally, a lot of friends like to use the following statement
In the previous article, the code is as follows:
Select ID from table where ID10 limit 0, 1;
This is certainly not a problem, but the performance is not feeling very much.
SQL statement Optimization:
You can use union ALL to implement a statement to take 3 rows of data, but only if 3 query fields to be the same, this query out the results of the first line is the previous article, the second line is the current article, the third line is the next article, the code is as follows:
Copy the Code code as follows:
(select ID from table where ID < ten ORDER BY ID ASC limit 1) UNION ALL (select ID from table where id = ten) UNION ALL ( Select ID from table where ID > The ORDER BY id DESC limit 1);
Now look at some examples of CMS PHPCMS implementation of the next article.
Get the current browse 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 to be able to help you learn.