php 上一篇,下一篇文章實現(xiàn)代碼與原理說明
更新時間:2010年05月09日 10:39:01 作者:
就是對id對進行order by id desc 或 order by id asc進行排序,然后再判斷比當前id> or小于當前文章id的相同欄目的文章。
實現(xiàn)原理:
就是對id對進行order by id desc 或 order by id asc進行排序,然后再判斷比當前id> or小于當前文章id的相同欄目的文章。
實例的sql語句如下:
$id就是當面文章的id
select * from news where id<$id order by id desc limit 0,1
select * from news where id>$id order by id desc limit 0,1
--
-- 表的結構 `string_find`
--
CREATE TABLE IF NOT EXISTS `string_find` (
`id` int(4) NOT NULL auto_increment,
`charList` varchar(100) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
--
-- 導出表中的數(shù)據(jù) `string_find`
--
INSERT INTO `string_find` (`id`, `charList`) VALUES
(1, '腳本之家'),
(2, 'baidu'),
(5, 'www.baidu.com'),
(6, 'chabaoo.cn');
好了萬事俱備了,下面來看一下操作方法
mysql_connect('localhost','root','root') or die(mysql_error());
mysql_select_db('cc');
mysql_query("set names 'gbk'");
$cid =5;//是你當前文章的編號
$sql ="select * from string_find where id>$cid order by id desc limit 0,1"; //上一篇文章
$sql1 ="select * from string_find where id<$cid order by id asc limit 0,1";//下一篇文章
$result = mysql_query( $sql );
if( mysql_num_rows( $result ) )
{
$rs = mysql_fetch_array( $result );
echo "上一篇".$rs[0];
}
else
{
echo "沒有了";
}
$result1 = mysql_query( $sql1 );
if( mysql_num_rows( $result1 ) )
{
$rs1 = mysql_fetch_array( $result1 );
echo "下一篇".$rs1[0];
}
else
{
echo "沒有了";
}
以下是別的網(wǎng)友寫的文章。
由于我希望訪客在瀏覽網(wǎng)頁的時候需要看到上一主題,下一主題的標題,所以必定是要在數(shù)據(jù)庫中查詢出來的了,可以通過limit限制來取,比如,我的博客是按照ID自動增量的,那么可以通過查找大于或者小于當前ID來取
$UpSQL="SELECT * FROM `blog` WHERE `ID`<$id ORDER BY `ID` DESC LIMIT 0,1";
$DownSQL="SELECT `ID`,`Title` FROM `blog` WHERE `ID`> $id ORDER BY `ID` ASC LIMIT 0,1";
再通過查詢,取出數(shù)據(jù)
如果只是單一的"上一篇","下一篇"那么就沒有必要查詢了,這樣是不必查詢了,但也許用戶點擊之后會看到,這已經(jīng)是首頁了或者這已經(jīng)是末頁了,呵呵
switch($act) {
case "Up":
$SQL="SELECT * FROM `blog` WHERE `ID`< $id ORDER BY `ID` DESC LIMIT 0,1";
break;
case 'Down':
$SQL="SELECT * FROM `blog` WHERE `ID`> $id ORDER BY `ID` ASC LIMIT 0,1";
break;
default :
$SQL="SELECT * FROM `blog` WHERE `ID`= $id LIMIT 0,1";
break;
}
通過傳遞一個動作來實現(xiàn)上一主題,下一主題
就是對id對進行order by id desc 或 order by id asc進行排序,然后再判斷比當前id> or小于當前文章id的相同欄目的文章。
實例的sql語句如下:
$id就是當面文章的id
select * from news where id<$id order by id desc limit 0,1
select * from news where id>$id order by id desc limit 0,1
--
-- 表的結構 `string_find`
--
CREATE TABLE IF NOT EXISTS `string_find` (
`id` int(4) NOT NULL auto_increment,
`charList` varchar(100) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
--
-- 導出表中的數(shù)據(jù) `string_find`
--
INSERT INTO `string_find` (`id`, `charList`) VALUES
(1, '腳本之家'),
(2, 'baidu'),
(5, 'www.baidu.com'),
(6, 'chabaoo.cn');
好了萬事俱備了,下面來看一下操作方法
復制代碼 代碼如下:
mysql_connect('localhost','root','root') or die(mysql_error());
mysql_select_db('cc');
mysql_query("set names 'gbk'");
$cid =5;//是你當前文章的編號
$sql ="select * from string_find where id>$cid order by id desc limit 0,1"; //上一篇文章
$sql1 ="select * from string_find where id<$cid order by id asc limit 0,1";//下一篇文章
$result = mysql_query( $sql );
if( mysql_num_rows( $result ) )
{
$rs = mysql_fetch_array( $result );
echo "上一篇".$rs[0];
}
else
{
echo "沒有了";
}
$result1 = mysql_query( $sql1 );
if( mysql_num_rows( $result1 ) )
{
$rs1 = mysql_fetch_array( $result1 );
echo "下一篇".$rs1[0];
}
else
{
echo "沒有了";
}
以下是別的網(wǎng)友寫的文章。
由于我希望訪客在瀏覽網(wǎng)頁的時候需要看到上一主題,下一主題的標題,所以必定是要在數(shù)據(jù)庫中查詢出來的了,可以通過limit限制來取,比如,我的博客是按照ID自動增量的,那么可以通過查找大于或者小于當前ID來取
$UpSQL="SELECT * FROM `blog` WHERE `ID`<$id ORDER BY `ID` DESC LIMIT 0,1";
$DownSQL="SELECT `ID`,`Title` FROM `blog` WHERE `ID`> $id ORDER BY `ID` ASC LIMIT 0,1";
再通過查詢,取出數(shù)據(jù)
如果只是單一的"上一篇","下一篇"那么就沒有必要查詢了,這樣是不必查詢了,但也許用戶點擊之后會看到,這已經(jīng)是首頁了或者這已經(jīng)是末頁了,呵呵
復制代碼 代碼如下:
switch($act) {
case "Up":
$SQL="SELECT * FROM `blog` WHERE `ID`< $id ORDER BY `ID` DESC LIMIT 0,1";
break;
case 'Down':
$SQL="SELECT * FROM `blog` WHERE `ID`> $id ORDER BY `ID` ASC LIMIT 0,1";
break;
default :
$SQL="SELECT * FROM `blog` WHERE `ID`= $id LIMIT 0,1";
break;
}
通過傳遞一個動作來實現(xiàn)上一主題,下一主題
相關文章
Laravel中擴展Memcached緩存驅動實現(xiàn)使用阿里云OCS緩存
這篇文章主要介紹了Laravel中擴展Memcached緩存驅動實現(xiàn)使用阿里云OCS緩存,本文擴展了一個支持SASL 認證模式的Memcached緩存驅動,需要的朋友可以參考下2015-02-02PHP pthreads v3下的Volatile簡介與使用方法示例
這篇文章主要介紹了PHP pthreads v3下的Volatile簡介與使用方法,結合實例形式較為詳細的分析了PHP pthreads v3下Volatile的功能、原理、使用方法及相關操作注意事項,需要的朋友可以參考下2020-02-02PHP+JS三級菜單聯(lián)動菜單實現(xiàn)方法
這篇文章主要介紹了PHP+JS三級菜單聯(lián)動菜單實現(xiàn)方法,涉及JavaScript動態(tài)控制頁面樣式及PHP數(shù)據(jù)庫操作相關技巧,需要的朋友可以參考下2016-02-02javascript+php實現(xiàn)根據(jù)用戶時區(qū)顯示當?shù)貢r間的方法
這篇文章主要介紹了javascript+php實現(xiàn)根據(jù)用戶時區(qū)顯示當?shù)貢r間的方法,實例分析javascript獲取客戶端時區(qū)及與服務器端php交互的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03一些需要禁用的PHP危險函數(shù)(disable_functions)
有時候為了安全我們需要禁掉一些PHP危險函數(shù),整理如下需要的朋友可以參考下2012-02-02