1. 程式人生 > >上一篇文章、下一篇文章實現

上一篇文章、下一篇文章實現

上一篇程式碼

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);  上一頁

'