首頁 >後端開發 >php教程 >php 檔案分頁類

php 檔案分頁類

WBOY
WBOY原創
2016-07-28 08:28:44796瀏覽
/**
 * 文件分页类 
 */
class FilePage {
 private $current;  //当前页 
 private $file;  //文件内容
 private $totalPage;  //总的页数
 private $url;  //传递的参数
 private $pageLen;  //每页显示的长度
/**
 * [__construct description] 构造方法 数据初始化
 * @author lianggc 2016-06-28
 * @param  string  $file_path [description] 文件路径
 * @param  integer $len       [description] 每页显示多少数据
 */
 function __construct( $file_path='',$len = 200 ){
   if(!file_exists($file_path)){
    die("$file_path not exists");
   }
   $this->file = file_get_contents($file_path);
   $this->pageLen = $len;
   $this->current = isset($_GET['page'])?$_GET['page']:1;
   $this->totalPage = $this->getTotalPage();
   $this->pageOverflow();
   $this->url = $this->getUrl();
 }
/**
 * [pageOverflow description] 页数溢出
 * @author lianggc 2016-06-28
 * @return [type] [description]
 */
private function pageOverflow(){

   if($this->current>$this->totalPage){ //当页大于最大页
     $this->current=$this->totalPage;
   }
   if($this->current<1){ //当页大于最大页
     $this->current=1;
   }
}  
/**
 * [getTotalPage description] 计算最大页数
 * @author lianggc 2016-06-28
 * @return [type] [description]
 */
 private function getTotalPage(){
  return ceil(strlen($this->file)/$this->pageLen);
 }
  
/**
 * [getUrl description]  url拼接
 * @author lianggc 2016-06-28
 * @return [type] [description]
 */
 private function getUrl(){
   $url =parse_url($_SERVER['REQUEST_URI']);
   parse_str(isset($url['query'])?$url['query']:'',$queryArr);
   unset($queryArr['page']);
   $query_str = http_build_query($queryArr);

   if(empty($query_str)){
    return $url['path'].'?page='; 
   }else{
    return $url['path'].'?'.$query_str.'&page='; 
   }
   
 }
  
  
 //首页
 private function first(){
   if($this->current>1)
    return "<a href=&#39;".$this->url."1'>首页</a>";
 }
 //上一页
 private function pre(){
   if( $this->current > 1 )
    return "<a href=&#39;".$this->url.($this->current-1)."'>上一页</a>";
 }
 //下一页
 private function next(){
   if( $this->current < $this->totalPage)
    return "<a href=&#39;".$this->url.($this->current+1)."'>下一页</a>";
 }
 //最后一页
 private function end(){
   if( $this->current < $this->totalPage )
    return "<a href=&#39;".$this->url.$this->totalPage."'>末页</a>";
 }
/**
 * [pageList description] 拼接 分页数  1 2 3 4 5 6
 * @author lianggc 2016-06-28
 * @return [type] [description]
 */
 public function pageList(){
   $pageListStr = '';
   for ($i=1;$i<=$this->totalPage;$i++){
    if($i==$this->current){
      $pageListStr.="<span style=&#39;font-size:20px;color:#f00&#39;>".$i."</span> ";
    }else{
      $pageListStr.="<a href=&#39;".$this->url.$i."'>".$i." </a>";
    }
   }
   return $pageListStr;
 }
/**
 * [pageStyle description] 分页显示格式
 * @author lianggc 2016-06-28
 * @param  integer $style [description] 分页格式
 * @return [type]         [description]
 */
 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;
  }
 }
/**
 * [getContents description] 获取每页内容。 暂不支持中文
 * @author lianggc 2016-06-28
 * @return [type] [description]
 */
 public function getContents(){
   $prePageLen = strlen($this->subStrs($this->current-1));
   $currentPageLen = strlen($this->subStrs($this->current));
   return substr($this->file, $prePageLen,$currentPageLen-$prePageLen);
 }
/**
 * [subStrs description] 文件内容截取
 * @author lianggc 2016-06-28
 * @param  [type] $page [description] 页数
 * @return [type]       [description]
 */
 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; 
 }
}

使用如下

<span style="font-family: Arial, Helvetica, sans-serif;">$file_obj=new  FilePage('../test.txt',1);</span>
echo $file_obj->getContents();
echo "<br>";
echo $file_obj->pageStyle();

以上就介紹了 php 檔案分頁類,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn