当前位置:Gxlcms > PHP教程 > php文件分页类

php文件分页类

时间:2021-07-01 10:21:17 帮助过:33人阅读

/**
 * 文件分页类 
 */
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 "url."1'>首页";
 }
 //上一页
 private function pre(){
   if( $this->current > 1 )
    return "url.($this->current-1)."'>上一页";
 }
 //下一页
 private function next(){
   if( $this->current < $this->totalPage)
    return "url.($this->current+1)."'>下一页";
 }
 //最后一页
 private function end(){
   if( $this->current < $this->totalPage )
    return "url.$this->totalPage."'>末页";
 }
/**
 * [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.="".$i." ";
    }else{
      $pageListStr.="url.$i."'>".$i." ";
    }
   }
   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; 
 }
}

使用如下

$file_obj=new  FilePage('../test.txt',1);
echo $file_obj->getContents();
echo "
"; echo $file_obj->pageStyle();

以上就介绍了 php 文件分页类,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

人气教程排行