찾다
php教程php手册通用分页类

通用分页类

Jun 06, 2016 pm 07:33 PM
codeigniter쪽수 매기기시험만능인

通用分页类(以Codeigniter测试) CodeIgniter ?php if( ! defined('BASEPATH')) die('No Access');/** * 分页类 * * @author wangaibowangaibo@boqii.net * @version 1.0.0 * @link http://www.du52.com/text.php?id=551 */class Page_list { /** * 总数据 *

通用分页类(以Codeigniter测试) CodeIgniter
<?php if( ! defined('BASEPATH')) die('No Access');

/**
 * 分页类
 *
 * @author wangaibo<wangaibo@boqii.net>
 * @version 1.0.0
 * @link http://www.du52.com/text.php?id=551
 */
class Page_list {

    /**
     * 总数据
     * @var int
     */
    private $total;
    /**
     * 每页显示数据
     * @var int
     */
    private $size;
    /**
     * 当前页数
     * @var int
     */
    private $page;
    /**
     * 页数列表左右页数
     * @var int
     */
    private $len;

    /**
     * 总页数
     * @var int
     */
    private $page_total;
    /**
     * 页码列表
     * @var array
     */
    private $page_list;

    /**
     * 基准地址
     * @var string
     */
    private $base_url;
    /**
     * 替换标志
     * @var string
     */
    private $place;
    /**
     * 分页样式
     * @var string
     */
    private $style;


    /**
     * 构造函数
     *
     * @param array $config 配置数组
     */
    public function __construct($config = array()){
        // 初始化默认值
        $this->total = 0;
        $this->size = 20;
        $this->page = 1;
        $this->len = 4;
        $this->page_total = 1;
        $this->page_list = array();
        $this->base_url = '?page=-page-';
        $this->place = '-page-';
        $this->style = $this->get_default_style();
        $this->initialize($config);
    }

    /**
     * 初始化分页
     *
     * @param array $config 配置数组
     */
    public function initialize($config = array()){
        // 取得配置值
        if(is_array($config)){
            if(array_key_exists('total', $config)) $this->total = @intval($config['total']);
            if(array_key_exists('size', $config)) $this->size = @intval($config['size']);
            if(array_key_exists('page', $config)) $this->page = @intval($config['page']);
            if(array_key_exists('len', $config)) $this->len = @intval($config['len']);
            if(array_key_exists('base_url', $config)) $this->base_url = @strval($config['base_url']);
            if(array_key_exists('place', $config)) $this->place = @strval($config['place']);
            if(array_key_exists('style', $config)) $this->style = @strval($config['style']);
        }
        // 修正值
        if($this->total<0) $this->total = 0;
        if($this->size<=0) $this->size = 20;
        if($this->page<=0) $this->page = 1;
        if($this->len<=0) $this->len = 4;
        // 执行分页算法
        $this->page_total = ceil($this->total/$this->size);
        if($this->page_total<=0) $this->page_total = 1;
        if($this->page>$this->page_total) $this->page = $this->page_total;
        if($this->page-$this->len>=1){
            for($i=$this->len; $i>0; $i--){
                $this->page_list[] = $this->page - $i;
            }
        }else{
            for($i=1; $i<$this->page; $i++){
                $this->page_list[] = $i;
            }
        }
        $this->page_list[] = $this->page;
        if($this->page+$this->len<=$this->page_total){
            for($i=1; $i<=$this->len; $i++){
                $this->page_list[] = $this->page + $i;
            }
        }else{
            for($i=$this->page+1; $i<=$this->page_total; $i++){
                $this->page_list[] = $i;
            }
        }
    }

    /**
     * 默认分页样式
     *
     * @return string
     */
    public function get_default_style(){
        $style = '<style type="text/css">';
        $style .= '	div.page_list { margin:0;padding:0;overflow:hidden;zoom:1;}';
        $style .= ' div.page_list a {display:block;float:left;height:20px;line-height:21px; font-size:13px;font-weight:normal;font-style:normal;color:#133DB6;text-decoration:none;margin:0 3px;padding:0 7px;overflow;zoom:1;}';
        $style .= ' div.page_list a.page_list_act { font-size:13px;padding:0 6px;}';
        $style .= ' div.page_list a:link, div.page_list a:visited { background:#FFF;border:1px #EEE solid;text-decoration:none;}';
        $style .= ' div.page_list a:hover, div.page_list a:active { background:#EEE;text-decoration:none;}';
        $style .= ' div.page_list strong { display:block;float:left;height:20px;line-height:21px;font-size:13px;font-weight:bold;font-style:normal;color:#000;margin:0 3px;padding:0 8px;overflow:hidden;zoom:1;}';
        $style .= ' </style>';
        return $style;
    }

    /**
     * 是否是第一页
     *
     * @return bool
     */
    public function is_first_page(){
        return $this->page == 1;
    }

    /**
     * 获取第一页页码
     *
     * @return int
     */
    public function get_first_page(){
        return 1;
    }

    /**
     * 是否是最后一页
     *
     * @return bool
     */
    public function is_last_page(){
        return $this->page == $this->page_total;
    }

    /**
     * 获取最后一页页码
     *
     * @return int
     */
    public function get_last_page(){
        return $this->page_total;
    }

    /**
     * 是否存在上一页
     *
     * @return bool
     */
    public function has_prev_page(){
        return $this->page > 1;
    }

    /**
     * 是否存在下一页
     *
     * @return bool
     */
    public function has_next_page(){
        return $this->page < $this->page_total;
    }

    /**
     * 获取上一页页码
     *
     * @return int
     */
    public function get_prev_page(){
        return $this->has_prev_page() ? $this->page - 1 : $this->page;
    }

    /**
     * 获取下一页页码
     *
     * @return int
     */
    public function get_next_page(){
        return $this->has_next_page() ? $this->page + 1 : $this->page;
    }

    /**
     * 获取当前页页码
     *
     * @return int
     */
    public function get_curr_page(){
        return $this->page;
    }

    /**
     * 获取总数据数
     *
     * @return int
     */
    public function get_total(){
        return $this->total;
    }

    /**
     * 获取每页显示数据数
     *
     * @return int
     */
    public function get_size(){
        return $this->size;
    }

    /**
     * 获取总页数
     *
     * @return int
     */
    public function get_total_page(){
        return $this->page_total;
    }

    /**
     * 构建并返回分页代码
     *
     * @param string $base_url 基准地址
     * @param string $place 替换标志
     * @param string $style 分页样式
     * @return string 分页HTML代码
     */
    public function display($base_url = '', $place = '', $style = ''){
        if($base_url==='') $base_url = $this->base_url;
        if($place==='') $place = $this->place;
        if($style==='') $style = $this->style;
        $str = $style.'<div class="page_list">';
        if( ! $this->is_first_page()){
            $str .= '<a class="page_list_act" href="'.str_replace($place, $this->get_first_page(), $base_url).'">首页</a>';
        }
        if($this->has_prev_page()){
            $str .= '<a class="page_list_act" href="'.str_replace($place, $this->get_prev_page(), $base_url).'">上一页</a>';
        }
        foreach($this->page_list as $v){
            if($v==$this->page){
                $str .= '<strong>' . $v . '</strong>';
            }else{
                $str .= '<a href="'.str_replace($place, $v, $base_url).'">'.$v.'</a>';
            }
        }
        if($this->has_next_page()){
            $str .= '<a class="page_list_act" href="'.str_replace($place, $this->get_next_page(), $base_url).'">下一页</a>';
        }
        if( ! $this->is_last_page()){
            $str .= '<a class="page_list_act" href="'.str_replace($place, $this->get_last_page(), $base_url).'">尾页</a>';
        }
        $str .= '</div>';
        return $str;
    }

}
?>
<?php if( ! defined('BASEPATH')) die('No Access');

    class Pagelist extends CI_Controller {

        public function page(){
            $this->load->helper('url');
            $page = $this->input->get('page');
            $page = @intval($page);
            if($page<=0) $page = 1;
            $this->load->library('page_list',array('total'=>10000,'size'=>16,'page'=>$page));
            $pl = $this->page_list->display(site_url('pagelist/page/page/-page-'));
            $this->load->view('pagelist', array('pl' => $pl));
        }

    }
?>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <title>分页测试</title>
</head>
<body>
<?php echo $pl; ?>
</body>
</html>
通用分页类
성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

SublimeText3 Linux 새 버전

SublimeText3 Linux 새 버전

SublimeText3 Linux 최신 버전

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

ZendStudio 13.5.1 맥

ZendStudio 13.5.1 맥

강력한 PHP 통합 개발 환경

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse용 SAP NetWeaver 서버 어댑터

Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

에디트플러스 중국어 크랙 버전

에디트플러스 중국어 크랙 버전

작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음