PHP簡單實現上一頁下一頁功能樣本_php技巧

來源:互聯網
上載者:User

本文執行個體講述了PHP簡單實現上一頁下一頁功能。分享給大家供大家參考,具體如下:

思路整理:

現在好多人用id的增1和減1實現上一篇和下一篇,但是難道文章ID不會斷了嗎?所以你要知道上個ID和個ID是多少就OK了。

那怎麼解決這個問題呢,很簡單!

例子:

假如這篇文章的ID200

<a href="?action=up&id=200">上一篇</a><a href="?action=down&id=200">下一篇</a>

如果是實現上一篇就在action=up頁面寫函數

$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);

原理,查詢比當前ID小(where id < '.$id.'上一篇)和比當前ID大(where id > '.$id.'下一篇)的1條(limit 0,1)資料,並按降序(desc,上一篇)和升序(asc,下一篇)顯示出來,當只取一篇的時候,可以省略降序或升序。

具體實現代碼:注需要傳遞參數

前台在上一篇,下一篇處調用:

<?php echo GetPreNext(pre,news,$_REQUEST[catid],$_REQUEST[id]);?>//顯示上一篇下一篇 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'] = "上一篇:<a href='newsshow.php?id=".$preR['id']."&&catid=".$catid."'>".$PreNext['title']."</a> ";      }      else      {       echo $PreNext['pre'] = "上一篇:沒有了 ";      }      if($PreNext=mysql_fetch_array($nextRow))      {       echo $PreNext['next'] = "下一篇:<a href='newsshow.php?id=".$nextR['id']."&&catid=".$catid."'>".$PreNext['title']."</a> ";      }      else      {        echo $PreNext['next'] = "下一篇:沒有了 ";      }}

代碼經測試可用

更多關於PHP相關內容感興趣的讀者可查看本站專題:《php常見資料庫操作技巧匯總》、《PHP數組(Array)操作技巧大全》、《php排序演算法總結》、《PHP常用遍曆演算法與技巧總結》、《PHP資料結構與演算法教程》、《php程式設計演算法總結》、《PHP數學運算技巧總結》、《phpRegex用法總結》、《PHP運算與運算子用法總結》及《php字串(string)用法總結》

希望本文所述對大家PHP程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.