上一篇代码
select top 1 Id from [table] where Id < @当前ID order by Id desc
下一篇代码
select top 1 Id from [table] where Id > @当前ID order by Id
以后下面不管是asp、php、jsp、asp.net等都可以实现,下面介绍asp和php做饭
在news.asp文件中添加如下代码
<%
set backrsd=server.CreateObject("Adodb.recordset")
sql="select top 1 * from news where id<"&id&" order by id desc"
set backrsd=conn.execute(sql)
if backrsd.eof then
NewTitle4="已经是第一篇"
else
id2=backrsd("id")
NewTitle2=backrsd("title")
end If
backrsd.close
set nextrsd=server.CreateObject("adodb.recordset")
sql="select top 1 * from news where id>"&id&" order by id"
set nextrsd=conn.execute(sql)
if nextrsd.eof then
NewTitle3="已经是最后一篇 "
else
id1=nextrsd("id")
NewTitle1=nextrsd("title")
end If
nextrsd.close
%>
在body中加入如下代码
<br>上一篇:<%=NewTitle4%><a href='news.asp?newid=<%=id2%>'><%=NewTitle2%></a>
<br>下一篇:<%=NewTitle3%><a href='news.asp?newid=<%=id1%>'><%=NewTitle1%></a>
php中用法
function nextpage($fid,$tag,$bid){
if('next' ==$tag){
$sql ="select * from gx_news where id<$fid and menu_id='$bid' order by id desc";
}elseif('prev' ==$tag){
$sql ="select * from gx_news where id>$fid and menu_id='$bid' order by id asc";
}
$result =mysql_query($sql);
if(!mysql_num_rows($result) ){
echo('己经没有了!');
}else{
$rs =mysql_fetch_array($result);
echo "<a href=news_page.php?id=".$rs[0].">".$rs['new_title']."</a>";
}
}
调用
nextpage($fid,'next',$bid); 下一页
nextpage($fid,'prev',$bid); 上一页
'