新聞中心
本文操作環(huán)境:windows7系統(tǒng)、php7.4版、DELL G3電腦

創(chuàng)新互聯(lián)技術(shù)團隊10多年來致力于為客戶提供成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計、營銷型網(wǎng)站建設(shè)、搜索引擎SEO優(yōu)化等服務(wù)。經(jīng)過多年發(fā)展,公司擁有經(jīng)驗豐富的技術(shù)團隊,先后服務(wù)、推廣了近1000家網(wǎng)站,包括各類中小企業(yè)、企事單位、高校等機構(gòu)單位。
php如何實現(xiàn)上一頁下一頁?
PHP簡單實現(xiàn)上一頁下一頁功能示例
本文實例講述了PHP簡單實現(xiàn)上一頁下一頁功能。
具體如下:
思路整理:
現(xiàn)在好多人用id的增1和減1實現(xiàn)上一篇和下一篇,但是難道文章ID不會斷了嗎?所以你要知道上個ID和個ID是多少就OK了。
那怎么解決這個問題呢,很簡單!
例子:
假如這篇文章的ID200
上一篇 下一篇
如果是實現(xiàn)上一篇就在action=up頁面寫函數(shù)
$id= $_GET['id']; //上一篇: $sql= select * from article where id < '.$id.' order by id desc limit 0,1'; $rs= mysql_query($sql); $row= mysql_fetch_array ($rs); //下一篇: $sql= select * from article where id < '.$id.' order by id asc limit 0,1'; $rs= mysql_query($sql); $row= mysql_fetch_array ($rs);
原理,查詢比當(dāng)前ID小(where id < '.$id.'上一篇)和比當(dāng)前ID大(where id > '.$id.'下一篇)的1條(limit 0,1)數(shù)據(jù),并按降序(desc,上一篇)和升序(asc,下一篇)顯示出來,當(dāng)只取一篇的時候,可以省略降序或升序。
具體實現(xiàn)代碼:注需要傳遞參數(shù)
前臺在上一篇,下一篇處調(diào)用:
//顯示上一篇下一篇
function GetPreNext($gtype,$table,$catid,$id){
$preR=mysql_fetch_array(mysql_query("select * from ".$table." where catid=".$catid." and id<$id order by id desc limit 0,1"));//id比傳入id小的最近一條
$nextR=mysql_fetch_array(mysql_query("select * from ".$table." where catid=".$catid." and id>$id order by id asc limit 0,1"));//id比傳入id大的最近一條
$next = (is_array($nextR) ? " where id={$nextR['id']} " : ' where 1>2 ');
$pre = (is_array($preR) ? " where id={$preR['id']} " : ' where 1>2 ');
$query = "Select * from ".$table." ";
$nextRow =mysql_query($query.$next);
$preRow = mysql_query($query.$pre);
if($PreNext=mysql_fetch_array($preRow))
{
echo $PreNext['pre'] = "上一篇:".$PreNext['title']." ";
}
else
{
echo $PreNext['pre'] = "上一篇:沒有了 ";
}
if($PreNext=mysql_fetch_array($nextRow))
{
echo $PreNext['next'] = "下一篇:".$PreNext['title']." ";
}
else
{
echo $PreNext['next'] = "下一篇:沒有了 ";
}
}
代碼經(jīng)測試可用
網(wǎng)站名稱:php如何實現(xiàn)上一頁下一頁
分享路徑:http://www.5511xx.com/article/coijdpo.html


咨詢
建站咨詢
