PHP實現(xiàn)適用于文件內(nèi)容操作的分頁類
更新時間:2016年06月15日 10:33:34 作者:D_aneil
這篇文章主要為大家詳細介紹了PHP實現(xiàn)適用于文件內(nèi)容操作的分頁類,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了PHP實現(xiàn)文件內(nèi)容操作的分頁類,強調(diào)一下只針對文件的操作,供大家參考,具體內(nèi)容如下
<?php
class StrPage {
private $current; //當前頁
private $file; //操作文件
private $totalPage; //總的頁數(shù)
private $url; //傳遞的參數(shù)
private $pageLen; //每頁顯示的長度
function __construct( $file,$len = 200 ){
$this->file = file_get_contents($file);
$this->pageLen = $len;
$this->current = isset($_GET['page'])?$_GET['page']:1;
$this->totalPage = $this->getTotalPage();
$this->url = $this->getUrl();
}
//獲取到這個文件總的長度
private function getTotalPage(){
return ceil(strlen($this->file)/$this->pageLen);
}
//獲取當前傳遞的參數(shù),保留參數(shù),page參數(shù)動態(tài)變化
private function getUrl(){
$url =parse_url($_SERVER['REQUEST_URI']);
parse_str($url['query'],$queryArr);
unset($queryArr['page']);
$queryStr = http_build_query($queryArr);
return $url['path'].'?'.$queryStr.'&page=';
}
//首頁
private function first(){
if($this->current>1)
return "<a href='".$this->url."1'>首頁</a>";
}
//上一頁
private function pre(){
if( $this->current > 1 )
return "<a href='".$this->url.($this->current-1)."'>上一頁</a>";
}
//下一頁
private function next(){
if( $this->current < $this->totalPage)
return "<a href='".$this->url.($this->current+1)."'>下一頁</a>";
}
//最后一頁
private function end(){
if( $this->current < $this->totalPage )
return "<a href='".$this->url.$this->totalPage."'>末頁</a>";
}
public function pageList(){
$pageListStr = '';
for ($i=1;$i<=$this->totalPage;$i++){
if($i==$this->current){
$pageListStr.="<span style='font-size:20px;color:#f00'>".$i."</span> ";
}else{
$pageListStr.="<a href='".$this->url.$i."'>".$i." </a>";
}
}
return $pageListStr;
}
public function pageStyle($style=1){
switch ($style){
case 1:
return "共有".$this->totalPage."頁".$this->first().$this->pre().$this->pageList().$this->next().$this->end();
break;
case 2;
return $this->pageList();
break;
}
}
public function getContents(){
$prePageLen = strlen($this->subStrs($this->current-1));
$currentPageLen = strlen($this->subStrs($this->current));
return substr($this->file, $prePageLen,$currentPageLen-$prePageLen);
}
public function subStrs($page){
$string = '';
$len= $page*$this->pageLen;
for( $i=0; $i<$len; $i++ ){
if( ord(substr($this->file,$i,1))>0xa0 ){
$string .= substr($this->file,$i,3);
$i = $i+2;
}else{
$string .= substr($this->file, $i,1);
}
}
return $string;
}
}
精彩專題分享:php分頁功能操作
以上就是本文的全部內(nèi)容,希望對大家學習PHP程序設計有所幫助。
相關文章
PHP實現(xiàn)使用DOM將XML數(shù)據(jù)存入數(shù)組的方法示例
這篇文章主要介紹了PHP實現(xiàn)使用DOM將XML數(shù)據(jù)存入數(shù)組的方法,結(jié)合具體實例形式分析了php基于DOM實現(xiàn)xml數(shù)據(jù)讀取與解析相關操作技巧,需要的朋友可以參考下2017-09-09
解析PHP函數(shù)array_flip()在重復數(shù)組元素刪除中的作用
本篇文章是對PHP函數(shù)array_flip()在重復數(shù)組元素刪除中的作用進行了詳細的分析介紹,需要的朋友參考下2013-06-06
完美解決:Apache啟動問題—(OS 10022)提供了一個無效的參數(shù)
本篇文章是對Apache啟動問題—(OS 10022)提供了一個無效參數(shù)的解決方法進行了詳細的分析介紹,需要的朋友參考下2013-06-06
PHP使用http_build_query()構(gòu)造URL字符串的方法
這篇文章主要介紹了PHP使用http_build_query()構(gòu)造URL字符串的方法,結(jié)合實例形式較為詳細的分析了http_build_query函數(shù)的功能,使用技巧與相關注意事項,需要的朋友可以參考下2016-04-04

