php实现上一篇下一篇的方法总结
Jun 08, 2016 pm 05:19 PMphp实现上一篇下一篇这个主要是通过sql来根据当前的id来进行判断然后筛选出当前ID之前的数据或ID之后的数据了就这么简单,具体的我们来看看。
实现网站文章里面上一篇和下一篇的sql语句的写法。
当前文章的id为 $article_id,当前文章对应分类的id是$cat_id,那么上一篇就应该是:
代码如下
SELECT max(article_id) FROM article WHERE article_id
执行这段sql语句后得到 $max_id,然后
SELECT article_id, title FROM article WHERE article_id = $max_id;
简化一下,转为子查询即:
SELECT article_id, title FROM article WHERE article_id = (SELECT max(article_id) FROM article WHERE article_id
下一篇为:
代码如下
SELECT min(article_id) FROM article WHERE article_id > $article_id AND cat_id=$cat_id;
执行这段sql语句后得到 $min_id,然后
SELECT article_id, title FROM article WHERE article_id = $min_id;
简化一下,转为子查询即:
代码如下
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);
最后讲一下有很多朋友喜欢使用下面语句
上一篇:
代码如下
select id from table where id10 limit 0,1;
这样肯定没有问题,但是是性能感觉不怎么地。
sql语句优化
你可以使用union all来实现一条语句取3行数据,但是前提是3个查询的字段要相同
这个查询出来的结果第一行就是上一篇文章,第二行是当前文章,第三行是下一篇文章
代码如下
(select id from table where id 10 order by id desc limit 1);
现在来看一些cms中的例子phpcms 实现上一篇下一篇
获取当前浏览文章id
$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 $prev = mysql_fetch_array($query);
">?php>
">?php>

Heißer Artikel

Hot-Tools-Tags

Heißer Artikel

Heiße Artikel -Tags

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

Beheben Sie den Festplattenfehler der Ereignis-ID 55, 50, 98, 140 in der Ereignisanzeige

Diese Apple-ID wird im iTunes Store noch nicht verwendet: Fix

Wo Sie die Tencent-Video-ID überprüfen können
![Ereignis-ID 4660: Objekt gelöscht [Fix]](https://img.php.cn/upload/article/000/887/227/168834320512143.png?x-oss-process=image/resize,m_fill,h_207,w_330)
Ereignis-ID 4660: Objekt gelöscht [Fix]

Asynchrone Verarbeitungsmethode von Select Channels Go zur gleichzeitigen Programmierung mit Golang

So verbergen Sie das ausgewählte Element in jquery

Welche Lösungen gibt es für die verteilte Redis-ID?
