PHP面向對象(OOP)----分頁類


同驗證碼類,分頁也是在個人博客,論壇等網站中不可缺少的方式,通過分頁可以在一個界面展示固定條數的數據,而不至於將所有數據全部羅列到一起,實現分頁的原理其實就是對數據庫查詢輸出加了一個limit限制,接下來我們就開始准備今天分頁類的邏輯

邏輯准備

實現分頁,我們需要獲取准備以下屬性和方法

屬性{
  	數據總條數
    每一頁顯示的條數
    計算出總頁數
    獲取當前是第幾頁
    顯示上一頁
    顯示下一頁
    顯示首頁
    顯示尾頁
    每一頁的url
    數據限制limit
}
方法{
  構造函數
    計算總頁數
    獲取當前頁
    獲取上一頁
    獲取下一頁
    獲取首頁
    獲取尾頁
    獲取當前頁面url
    獲取上一頁url
    獲取下一頁url
    獲取首頁url
    獲取尾頁irl
    生成limit記錄
    重新生成url地址
    顯示分頁鏈接,顯示分頁情況
}

根據上面的邏輯,下一步將文字轉換為代碼

首先,我們先聲明一個Page類,按照邏輯進行屬性聲明,並且進行初始化

class Page
{
 	//記錄總條數
	protected $total;
	//每頁顯示幾條
	protected $nums;
	//總頁數
	protected $totalPages;
	//當前頁碼
	protected $currentPage;
	//上一頁頁碼
	protected $prevPage;
	//下一頁頁碼
	protected $nextPage;
	//首頁頁碼
	protected $firstPage;
	//尾頁頁碼
	protected $endPage;
	//url
	protected $url;
	//limit,傳到數據庫的limit
	protected $limit;
  
  	//構造函數,初始化
	public function __construct($total, $nums)
	{
		$this->total = $total;
		$this->nums = $nums;

		$this->totalPages = $this->getTotalPages();
		$this->currentPage = $this->getCurrentPage();

		$this->getPrevPage();
		$this->getNextPage();
		$this->getFirstPage();
		$this->getEndPage();
		$this->getUrl();

	}
}

接下來我們開始完善方法

  • 計算總頁數
protected funciton getTotalPages()
  {
    return ceil($this->total / $this->$nums);
  }
  • 獲取當前頁碼
protected function getCurentPage()
{
  //判斷如果存在page參數並且page大於0,返回實際值,否則返回1
  if(isset($_GET['page']) && intval($_GET['page']) > 0)
    {
      $this->currentPage = intval($_GET['page']);
    } else {
      $this->currentPage = 1;
    }
  return $this->currentPage;
}
  • 獲取上一頁
protected function getPrevPage()
  {
    $this->prevPage = $this->currentPage - 1;
  	if($this->prevPage < 1)
      {
        $this->prevPage = 1;
      }
  	return $this->prevPage;
  }
  • 獲取下一頁
protected function getNextPage()
  {
    $this->nextPage = $this->currentPage + 1;
 	return $this->nextPage;
  }
  • 獲取首頁
protected function getFirstPage()
  {
    $this->firstPage = 1;
  	return $this->firstPage;
  }
  • 獲取尾頁
protected function getEndPage()
  {
    $this->endPage = $this->totalPages;
  	return $this->endPage;
  }

接下來開始拼接每個頁碼的url

  • 獲取當前頁的url
protected function getCurrentUrl()
  {
    return $this->url.'$page='.$this->currentPage;
  }
  • 獲取前一頁的url
protected function getPrevUrl()
  {
    return $this->url.'&page='.$this->prevPage;
  }
  • 獲取下一頁的url
protected function getNextUrl()
  {
    return $this->url.'&page='.$this->nextPage;
  }
  • 獲取首頁的url
protected function getFirstUrl()
  {
    return $this->url.'&page='.$this->firstPage;
  }
  • 獲取尾頁的url
protected function getEndUrl()
  {
    return $this->url.'&page='.$this->endPage;
  }

生成limit記錄

public function limit()
  {
    return ($this->currentPage - 1) * $this->nums.','.$this->nums;
  }

生成url地址

public function getUrl()
  {
  	//獲取當前頁面的文件位置
    $url = $_SERVER['REQUEST_URI'];
  	//將url參數解析成數組
  	$parse = parse_url($url);
  	//獲得域名地址
  	$path  = $parse['path'];
  	//獲取參數
  	$query = isset($parse['query']) ? $parse['query'] : false;
  	//如果有參數,把page這個參數先給干掉,因為我們要重新拼接
  	if($query)
      {
        parse_str($query,$query);
      	//干掉page參數,保留其他參數
        unset($query['page']);
      	//http_build_query拼將參數拼接成請求
        $uri = $parse['path'].'?'.http_build_query($query);
      } else {
        $uri = rtrim($parse['path'],'?').'?';
      }
  
  	//智能識別https和http協議和端口號
  $protocal = (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
  switch ($_SERVER['SERVER_PORT']) {
    case 80:
    case 443:
      $uri = $protocal.$_SERVER['SERVER_NAME'].$uri;
      break;
    default:
      $uri = $protocal.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$uri;
      break;
  }
  $this->url = $uri;
}

到此所有的邏輯方面都已經處理完啦,接下來的render()函數用來顯示分頁的鏈接

public function render()
  {
    return array(
    		['first' => $this->getFirstUrl()],
      		['prev'  => $this->getPrevUrl()],
      		['current' => $this->getCurrentUrl()],
      		['next'  => $this->getNextUrl()],
      		['end'   => $this->getEndUrl()]
    );
  }

哦也,就這么愉快的結束啦

使用方法如下

//new一個對象
$page = new Page(102,10);
//打印出來上頁/下頁/首頁/尾頁……的url
var_dump($page->render());

后記


$url = http://www.zhyunfe.com/OOP/Page.class.php?page=1
parse_url($url):將url的文件路徑和參數分開並保存到數組中
  
 ..........................................
 .  array (size=2)
 .	'path' => string '/OOP/Page.class.php' (length=36)
 .	'query' => string 'page=1' (length=6)
 ..........................................
  					...
  
$query = "page=1&num=2&sex=男"
parse_str($query,$query):將帶參數的字符串解析成數組
 ..........................................
 . array
 . 	'page' => 1
 .	'num'  => 2
 .	'sex'  => '男'
 ..........................................
  					...
  
 $query = ['num'=>1,'sex'=>'男']
  
 http_build_query($query):使用給出的關聯(或下標)數組生成一個經過 URL-encode 的請求字符串。 
 
 .........................................
 . string
 .	"num=1&&sex='男'"
 .........................................
  
  					...
  
  $_SERVER['SERVER_PORT'] 獲取端口號
  $_SERVER['SERVER_NAME'] 當前運行腳本所在的服務器的主機名。如果腳本運行於虛擬主機中,該名稱是由那個虛擬主機所設置的值決定
  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM