In this paper, we analyze the method of PHP implementation of the next article. Share to everyone for your reference, specific as follows:
PHP implementation of the next article this is mainly through the SQL to judge according to the current ID and then filter out the current ID before the data or ID data is so simple, specific we look.
The implementation of the Web site article on the previous and the next one of the SQL statement writing.
The ID of the current article is $article _id, and the ID of the current article corresponds to the $cat_id, so the previous article should be:
Copy 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, get $max _id, and then
Copy Code code as follows:
SELECT article_id, title from article WHERE article_id = $max _id;
Simplify, and turn to subqueries namely:
Copy 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 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 Code code as follows:
SELECT article_id, title from article WHERE article_id = $min _id;
Simplify, and turn to subqueries namely:
Copy 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
Previous, the code is as follows:
Select ID from table where ID10 limit 0, 1;
This is certainly not a problem, but it is not a performance feeling.
SQL statement Optimization:
You can use union ALL to implement a statement to take 3 rows of data, but the premise is that 3 query fields to be the same, the result of this query is 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 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 > Ten ORDER BY id DESC limit 1);
Now look at some examples of CMS PHPCMS implementation of the next article.
Get current Browse Article ID:
$id = Isset ($_get[' id ') > 0? Intval ($_get[' id ']): "";
Next article:
$query = mysql_query ("Select Id,title from article WHERE id> ' $id ' ORDER by ID ASC LIMIT 1");
$next = Mysql_fetch_array ($query);
Previous article:
$query = mysql_query ("Select Id,title from article WHERE ID < ' $id ' ORDER by ID DESC LIMIT 1");
$prev = Mysql_fetch_array ($query);
More interested in PHP related content readers can view the site topics: "Php+mysql Database Operation Introductory Course", "PHP+MYSQLI Database Design Skills Summary", "PHP object-oriented Programming Program", "PHP Array" Operation Techniques Encyclopedia, " Introduction to PHP Basic Grammar, PHP operations and Operator Usage Summary, PHP Network Programming skills Summary, PHP string (String) Usage summary and "PHP Common database Operation Skills Summary"
I hope this article will help you with the PHP program design.