溫馨提示×

溫馨提示×

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

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

如何在PHP中實現(xiàn)分頁顯示

發(fā)布時間:2021-06-07 16:10:59 來源:億速云 閱讀:103 作者:Leah 欄目:開發(fā)技術(shù)

如何在PHP中實現(xiàn)分頁顯示?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

具體如下:

<?php
header("content-type:text/html;charset=utf-8");
$currentpage = 1;
if(isset($_GET['page']))
  $currentpage = $_GET['page'];
//連接數(shù)據(jù)庫
$link = mysql_connect("localhost","root","") or die('連接失敗');
mysql_select_db('myschool');
mysql_query('set names utf8');
$sql ="SELECT count(*) as 'count' from student";//查詢記錄的sql語句
$result = mysql_query($sql);
$arr = mysql_fetch_array($result);
$count = $arr['count'];
$pagesize = 3;
$pages = ceil($count/$pagesize);//共多少頁
$prepage = $currentpage -1;
if($prepage<=0)
  $prepage=1;
$nextpage = $currentpage+1;
if($nextpage >= $pages){
 $nextpage = $pages;
}
$start =($currentpage-1) * $pagesize;//起始位置
$sql = "SELECT * from student limit $start,$pagesize";
echo $sql;
// $sql = "select * from student";
$result = mysql_query($sql);
?>
<!-- html部分 -->
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
</head>
<body>
<table border="1">
 <tr>
 <td>學號</td>
 <td>姓名</td>
 <td>性別</td>
 <td>年齡</td>
 </tr>
<?php while($arr=mysql_fetch_array($result)){ ?>
 <td><?php echo $arr['number']; ?></td>
 <td><?php echo $arr['name']; ?></td>
 <td><?php echo $arr['sex']; ?></td>
 <td><?php echo $arr['age']; ?></td>
 </tr>
<?php } ?>
 </table>
 <a href="<?php echo $_SERVER['PHP_SELF'].'?page='.$prepage; ?>" rel="external nofollow" >上一頁</a>&nbsp;&nbsp;<a href="<?php echo $_SERVER['PHP_SELF'].'?page='.$nextpage; ?>" rel="external nofollow" >下一頁</a>
</body>
</html>

注:當一個文件中有php和html兩種時,php文件必須有結(jié)束標記

附:php通用分頁類與用法:

Page.class.php文件:

<?php
/**
 * 分頁類
 *
 * 調(diào)用方式:
 * $p=new Page(總條數(shù),顯示頁數(shù),當前頁碼,每頁顯示條數(shù),[鏈接]);
 * print_r($p->getPages()); //生成一個頁碼數(shù)組(鍵為頁碼,值為鏈接)
 * echo $p->showPages(1);  //生成一個頁碼樣式(可添加自定義樣式)
 *
 */
/*
總條數(shù),需要顯示的頁數(shù),當前頁,每頁顯示的條數(shù),連接
生成一個一維數(shù)組,鍵為頁碼 值為連接
返回一個生成好樣式的頁碼(并且可以根據(jù)自己需要添加樣式)
默認樣式 共45條記錄,每頁顯示10條,當前第1/4頁 [首頁] [上頁] [1] [2] [3] .. [下頁] [尾頁]
*/
class Page{
  protected $count;    //總條數(shù)
  protected $showPages;  //需要顯示的頁數(shù)
  protected $countPages; //總頁數(shù)
  protected $currPage;  //當前頁
  protected $subPages;  //每頁顯示條數(shù)
  protected $href;    //連接
  protected $page_arr=array();  //保存生成的頁碼 鍵頁碼 值為連接
  /**
   * __construct 構(gòu)造函數(shù)(獲取分頁所需參數(shù))
   * @param int $count   總條數(shù)
   * @param int $showPages 顯示頁數(shù)
   * @param int $currPage 當前頁數(shù)
   * @param int $subPages 每頁顯示數(shù)量
   * @param string $href  連接(不設(shè)置則獲取當前URL)
   */
  public function __construct($count,$showPages,$currPage,$subPages,$href=''){
    $this->count=$count;
    $this->showPages=$showPages;
    $this->currPage=$currPage;
    $this->subPages=$subPages;
    //如果鏈接沒有設(shè)置則獲取當前連接
    if(empty($href)){
      $this->href=htmlentities($_SERVER['PHP_SELF']);
    }else{
      $this->href=$href;
    }
    $this->construct_Pages();
  }
  /**
   * getPages 返回頁碼數(shù)組
   * @return array 一維數(shù)組 鍵為頁碼 值為鏈接
   */
  public function getPages(){
    return $this->page_arr;
  }
  /**
   * showPages 返回生成好的頁碼
   * @param int $style 樣式
   * @return string   生成好的頁碼
   */
  public function showPages($style=1){
    $func='pageStyle'.$style;
    return $this->$func();
  }
  /**
   * pageStyle1 分頁樣式(可參照這個添加自定義樣式 例如pageStyle2())
   * 樣式 共45條記錄,每頁顯示10條,當前第1/4頁 [首頁] [上頁] [1] [2] [3] .. [下頁] [尾頁]
   * @return string
   */
  protected function pageStyle1(){
    /* 構(gòu)造普通模式的分頁
    共4523條記錄,每頁顯示10條,當前第1/453頁 [首頁] [上頁] [1] [2] [3] .. [下頁] [尾頁]
    */
    $pageStr='共'.$this->count.'條記錄,每頁顯示'.$this->subPages.'條';
    $pageStr.='當前第'.$this->currPage.'/'.$this->countPages.'頁 ';
    $_GET['page'] = 1;
    $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a>] </span>';
    //如果當前頁不是第一頁就顯示上頁
    if($this->currPage>1){
      $_GET['page'] = $this->currPage-1;
      $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上頁</a>] </span>';
    }
    foreach ($this->page_arr as $k => $v) {
      $_GET['page'] = $k;
      $pageStr.='<span>[<a href="'.$v.'" rel="external nofollow" >'.$k.'</a>] </span>';
    }
    //如果當前頁小于總頁數(shù)就顯示下一頁
    if($this->currPage<$this->countPages){
      $_GET['page'] = $this->currPage+1;
      $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下頁</a>] </span>';
    }
    $_GET['page'] = $this->countPages;
    $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁</a>] </span>';
    return $pageStr;
  }
  /**
   * construct_Pages 生成頁碼數(shù)組
   * 鍵為頁碼,值為鏈接
   * $this->page_arr=Array(
   *         [1] => index.php?page=1
   *         [2] => index.php?page=2
   *         [3] => index.php?page=3
   *         ......)
   */
  protected function construct_Pages(){
    //計算總頁數(shù)
    $this->countPages=ceil($this->count/$this->subPages);
    //根據(jù)當前頁計算前后頁數(shù)
    $leftPage_num=floor($this->showPages/2);
    $rightPage_num=$this->showPages-$leftPage_num;
    //左邊顯示數(shù)為當前頁減左邊該顯示的數(shù) 例如總顯示7頁 當前頁是5 左邊最小為5-3 右邊為5+3
    $left=$this->currPage-$leftPage_num;
    $left=max($left,1); //左邊最小不能小于1
    $right=$left+$this->showPages-1; //左邊加顯示頁數(shù)減1就是右邊顯示數(shù)
    $right=min($right,$this->countPages); //右邊最大不能大于總頁數(shù)
    $left=max($right-$this->showPages+1,1); //確定右邊再計算左邊,必須二次計算
    for ($i=$left; $i <= $right; $i++) {
      $_GET['page'] = $i;
      $this->page_arr[$i]=$this->href.'?'.http_build_query($_GET);
    }
  }
}
?>

用法示例demo.php:

<?php
/**
 * demo
 */
header("content-type:text/html;charset=utf8");
include('Page.class.php');  //引入類
//$p=new Page(總條數(shù),顯示頁數(shù),當前頁碼,每頁顯示條數(shù),[鏈接]);
//連接不設(shè)置則為當前鏈接
$page=isset($_GET['page']) ? $_GET['page'] : 1;
$p=new Page(100,4,$page,8);
//生成一個頁碼數(shù)組(鍵為頁碼,值為鏈接)
echo "<pre>";
print_r($p->getPages());
//樣式 共45條記錄,每頁顯示10條,當前第1/4頁 [首頁] [上頁] [1] [2] [3] .. [下頁] [尾頁]
echo $p->showPages(1);

關(guān)于如何在PHP中實現(xiàn)分頁顯示問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細節(jié)

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

php
AI