php实现上一篇下一篇这个主要是通过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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
