溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

在php項(xiàng)目中實(shí)現(xiàn)分頁(yè)功能的方法有哪些

發(fā)布時(shí)間:2020-12-21 15:54:36 來(lái)源:億速云 閱讀:125 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章給大家介紹在php項(xiàng)目中實(shí)現(xiàn)分頁(yè)功能的方法有哪些,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

方法一:講sql查詢進(jìn)行分頁(yè)進(jìn)行,需要調(diào)用幾個(gè)函數(shù),具體見腳本:

1.pager.class.php

<?php
  
  class pager {
    public $sql; //SQL查詢語(yǔ)句
    public $datanum; //查詢所有的數(shù)據(jù)總記錄數(shù)
    public $page_size; //每頁(yè)顯示記錄的條數(shù)
    protected $_errstr;
    protected $_conn;
    protected $_query_id;

    public function query($query)///這個(gè)函數(shù)有問(wèn)題,暫時(shí)可以不用
    {
    $ret = false;
    if (!empty($query)) {
      if ($this->_conn === false || !is_resource($this->_conn)) {
       warningLog(__METHOD__ . ': query sql with no connection', true);
      return false;
      }
    $this->_query_id = @mysql_query($query, $this->_conn);
    if ($this->_query_id === false) {
    $this->_errstr = @mysql_error();
    $ret = false;
     } else {
    $this->_errstr = 'SUCCESS';
    $ret = $this->_query_id;
      }
    }
     $msg = ($ret === false) ? 'false' : strval($ret);
     debugLog(__METHOD__.": [$msg] returned for sql query [$query]");
    return $ret;
    }
function __construct($sql,$page_size) {
      $result = mysql_query($sql);
      $datanum = mysql_num_rows($result);
      $this->sql=$sql;
      $this->datanum=$datanum;
      $this->page_size=$page_size;
    }

    //當(dāng)前頁(yè)數(shù)
    public function page_id() {
      if($_SERVER['QUERY_STRING'] == ""){
        return 1;
      }elseif(substr_count($_SERVER['QUERY_STRING'],"page_id=") == 0){
        return 1;
      }else{
        return intval(substr($_SERVER['QUERY_STRING'],8));
      }
    }

    //剩余url值
    public function url() {
      if($_SERVER['QUERY_STRING'] == ""){
        return "";
      }elseif(substr_count($_SERVER['QUERY_STRING'],"page_id=") == 0){
        return "&".$_SERVER['QUERY_STRING'];
      }else{
        return str_replace("page_id=".$this->page_id(),"",$_SERVER['QUERY_STRING']);
      }
    }

    //總頁(yè)數(shù)
    public function page_num() {
      if($this->datanum == 0){
        return 1;
      }else{
        return ceil($this->datanum/$this->page_size);
      }
    }
//數(shù)據(jù)庫(kù)查詢的偏移量
    public function start() {
      return ($this->page_id()-1)*$this->page_size;
    }

    //數(shù)據(jù)輸出
    public function sqlquery() {
      return $this->sql." limit ".$this->start().",".$this->page_size;
    }

    //獲取當(dāng)前文件名
    private function php_self() {
      return $_SERVER['PHP_SELF'];
    }

    //上一頁(yè)
    private function pre_page() {
      if ($this->page_id() == 1) { //頁(yè)數(shù)等于1
        return "<a href=".$this->php_self()."?page_id=1".$this->url().">上一頁(yè)</a> ";
      }elseif ($this->page_id() != 1) { //頁(yè)數(shù)不等于1
        return "<a href=".$this->php_self()."?page_id=".($this->page_id()-1).$this->url().">上一頁(yè)</a> ";
      }
    }

    //顯示分頁(yè)
    private function display_page() {
      $display_page = "";
      if($this->page_num() <= 10){ //小于10頁(yè)
        for ($i=1;$i<=$this->page_num();$i++) //循環(huán)顯示出頁(yè)面
          $display_page .= "<a href=".$this->php_self()."?page_id=".$i.$this->url().">".$i."</a> ";
          return $display_page;
      }elseif($this->page_num() > 10){ //大于10頁(yè)
        if($this->page_id() <= 6){
          for ($i=1;$i<=10;$i++) //循環(huán)顯示出頁(yè)面
            $display_page .= "<a href=".$this->php_self()."?page_id=".$i.$this->url().">".$i."</a> ";
            return $display_page;
        }elseif(($this->page_id() > 6) && ($this->page_num()-$this->page_id() >= 4)){
          for ($i=$this->page_id()-5;$i<=$this->page_id()+4;$i++) //循環(huán)顯示出頁(yè)面
            $display_page .= "<a href=".$this->php_self()."?page_id=".$i.$this->url().">".$i."</a> ";
 return $display_page;
        }elseif(($this->page_id() > 6) && ($this->page_num()-$this->page_id() < 4)){
          for ($i=$this->page_num()-9;$i<=$this->page_num();$i++) //循環(huán)顯示出頁(yè)面
            $display_page .= "<a href=".$this->php_self()."?page_id=".$i.$this->url().">".$i."</a> ";
            return $display_page;
        }
      }
    }

    //下一頁(yè)
    private function next_page() {
      if ($this->page_id() < $this->page_num()) { //頁(yè)數(shù)小于總頁(yè)數(shù)
        return "<a href=".$this->php_self()."?page_id=".($this->page_id()+1).$this->url().">下一頁(yè)</a> ";
      }elseif ($this->page_id() == $this->page_num()) { //頁(yè)數(shù)等于總頁(yè)數(shù)
        return "<a href=".$this->php_self()."?page_id=".$this->page_num().$this->url().">下一頁(yè)</a> ";
      }
    }

    // 設(shè)置分頁(yè)信息
    public function set_page_info() {
      $page_info = "共".$this->datanum."條 ";
      $page_info .= "<a href=".$this->php_self()."?page_id=1".$this->url().">首頁(yè)</a> ";
      $page_info .= $this->pre_page();
      $page_info .= $this->display_page();
      $page_info .= $this->next_page();
      $page_info .= "<a href=".$this->php_self()."?page_id=".$this->page_num().$this->url().">尾頁(yè)</a> ";
      $page_info .= "第".$this->page_id()."/".$this->page_num()."頁(yè)";
      return $page_info;
    }

  }
?>

2.腳本2:

<?php
  //類的用法
  // 讀取分頁(yè)類
  include("pager.class.php");
  // 數(shù)據(jù)庫(kù)連接初始化
//  $db = new mysql();
  $impeach_host = '10.81.43.139';
  $impeach_usr = 'vmtest15';
  $impeach_passwd = 'vmtest15';
  $impeach_name = 'ufeature';
  $impeach_con = mysql_connect($impeach_host, $impeach_usr, $impeach_passwd) or
    die("Can't connect ".mysql_error());
  mysql_select_db($impeach_name, $impeach_con);
  // 這是一個(gè)sql查詢語(yǔ)句,并得到查詢結(jié)果
  $sql = "select word from ufeature.spam_accuse_word_list where flag='0'";
  // 分頁(yè)初始化
  $page = new pager($sql,20);
  // 20是每頁(yè)顯示的數(shù)量
  // $res_1 = mysql_query($sql) or
  //    die("Can't get result ".mysql_error());

   $result=mysql_query($page->sqlquery());
while($info = mysql_fetch_array($result,MYSQL_ASSOC)){

  // while($info = mysql_fetch_array($res_1, MYSQL_ASSOC)){
  echo $info["word"]."<br/>";
  }
  // 頁(yè)碼索引條
  echo $page->set_page_info();


?>

方法二:使用ajax的方法
1、首先了解SQL語(yǔ)句中的limit用法

SELECT * FROM table …… limit 開始位置 , 操作條數(shù) (其中開始位置是從0開始的)

例子
取前20條記錄:SELECT * FROM table …… limit   0 , 20
從第11條開始取20條記錄:SELECT * FROM table …… limit   10 , 20 
LIMIT n 等價(jià)于 LIMIT 0,n。
select * from table LIMIT 5; //返回前5行, select * from table LIMIT 0,5一樣 
2、分頁(yè)原理

 所謂分頁(yè)顯示,也就是講數(shù)據(jù)庫(kù)中的結(jié)果集,一段一段顯示出來(lái)
怎么分段,當(dāng)前在第幾段 (每頁(yè)有幾條,當(dāng)前再第幾頁(yè))
前10條記錄:select * from table limit 0,10
第11至20條記錄:select * from table limit 10,10
第21至30條記錄:select * from table limit 20,10
分頁(yè)公式:
(當(dāng)前頁(yè)數(shù) - 1 )X 每頁(yè)條數(shù) , 每頁(yè)條數(shù)

Select * from table limit ($Page- 1) * $PageSize, $PageSize

3、$_SERVER["REQUEST_URI"]函數(shù)
預(yù)定義服務(wù)器變量的一種,所有$_SERVER開頭的都叫做預(yù)定于服務(wù)器變量。
REQUEST_URI的作用是取得當(dāng)前URI,也就除域名外后面的完整的地址路徑。
例子:
當(dāng)前頁(yè)為:http://www.test.com/home.php?id=23&cid=22
echo $_SERVER["REQUEST_URI"]
結(jié)果為:/home.php?id=23&cid=22 
4、parse_url()解析URL函數(shù)
 parse_url() 是講URL解析成有固定鍵值的數(shù)組的函數(shù) 
例子

$ua=parse_url("http://username:password@hostname/path?arg=value#anchor");
print_r($ua);

結(jié)果:

Array
(
 [scheme] => http  ;協(xié)議
 [host] => hostname  ;主機(jī)域名
 [user] => username  ;用戶
 [pass] => password  ;密碼
 [path] => /path   ;路徑
 [query] => arg=value  ;取參數(shù)
 [fragment] => anchor  ;
)

5、代碼實(shí)例
 這個(gè)一個(gè)留言的分頁(yè),分為3個(gè)部分,一個(gè)是數(shù)據(jù)庫(kù)設(shè)計(jì),一個(gè)是連接頁(yè)面,一個(gè)是顯示頁(yè)面。
(1)設(shè)計(jì)數(shù)據(jù)庫(kù)
 設(shè)計(jì)數(shù)據(jù)庫(kù)名為bbs,有一個(gè)數(shù)據(jù)表為message,里面包含title,lastdate,user,content等字段,分別表示留言標(biāo)題,留言日前,留言人,留言的內(nèi)容
(2)連接頁(yè)面

<?php
$conn = @ mysql_connect("localhost", "root", "123456") or die("數(shù)據(jù)庫(kù)鏈接錯(cuò)誤");
mysql_select_db("bbs", $conn);
mysql_query("set names 'GBK'"); //使用GBK中文編碼;
//將空格,換行轉(zhuǎn)換為HTML可解析
function htmtocode($content) {
 $content = str_replace("\n", "<br>", str_replace(" ", "&nbsp;", $content)); //兩個(gè)str_replace嵌套
 return $content;
}
//$content=str_replace("'","‘",$content);
 //htmlspecialchars();
 
?>

(3)顯示頁(yè)面

<?php
 include("conn.php");
$pagesize=2; //設(shè)置每頁(yè)顯示2個(gè)記錄
$url=$_SERVER["REQUEST_URI"]; 
$url=parse_url($url);
$url=$url[path];

$numq=mysql_query("SELECT * FROM `message`");
$num = mysql_num_rows($numq);
if($_GET123下一頁(yè)閱讀全文

關(guān)于在php項(xiàng)目中實(shí)現(xiàn)分頁(yè)功能的方法有哪些就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI