php文件分页类
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php文件分页类,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3512字,纯文字阅读大概需要6分钟。
内容图文
![php文件分页类](/upload/InfoBanner/zyjiaocheng/161/20fb60c6216c45879c08a8660675663a.jpg)
/**
* 文件分页类
*/
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教程有兴趣的朋友有所帮助。
内容总结
以上是互联网集市为您收集整理的php文件分页类全部内容,希望文章能够帮你解决php文件分页类所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。