2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ページャークラス

Posted at
Pager.class.php
<?php
require_once HOMEPATH. 'models/PagerService.class.php';
require_once HOMEPATH. 'models/PagerFactory.class.php';
//  現在のページの情報をまとめたクラス
class pager {
    private $start; //現在のページの最初の要素番号
    private $item; //1ページの要素数
    private $count; //全要素数
    private $pagenum; //現在のページ番号
    private $lastpage; //最後のページ
    private $nextpage; //次のページがあるかどうか
    private $prevpage; //前のページがあるかどうか
    public function setStart($start){
        $this->start = $start;
    }
    public function setItem($item){
        $this->item = $item;
    }
    public function setCount($count){
        $this->count = $count;
    }
    public function setPagenum($pagenum){
        $this->pagenum = $pagenum;
    }
    public function setLastpage($lastpage){
        $this->lastpage = $lastpage;
    }
    public function setNextpage($nextpage){
        $this->nextpage = $nextpage;
    }
    public function setPrevpage($prevpage){
        $this->prevpage = $prevpage;
    }
    public function setAll($list,$item,$pagenum){
        //開始要素
        $start = ($pagenum - 1) * $item;
        $this->setStart($start);
        //1ページの要素数
        $this->setItem($item);        
        //全要素数
        $count = count($list);
        $this->setCount($count);
        //現在のページ番号
        $this->setPagenum($pagenum);
        //最後のページ
        $lastpage = ceil($count / $item);
        $this->setLastpage($lastpage);
        //次のページがあるかどうか
        if($pagenum == $lastpage){ $this->setNextpage(false); }
        else{ $this->setNextpage(true); }
        //前のページがあるかどうか
        if($pagenum == 1){ $this->setPrevpage(false); }
        else{ $this->setPrevpage(true); }
        //このクラスを返す
        return $this;
    }
    public function getStart(){
        return $this->start;
    }
    public function getItem(){
        return $this->item;
    }
    public function getCount(){
        return $this->count;
    }
    public function getPagenum(){
        return $this->pagenum;
    }
    public function getLastpage(){
        return $this->lastpage;
    }
    public function getNextpage(){
        return $this->nextpage;
    }
    public function getPrevpage(){
        return $this->prevpage;
    }
}
?>
PagerFactory.class.php
<?php
class PagerFactory {
	function __construct() { }
	/*
	 * 常にページャークラスを返す
	 */
	public function Create($list,$item,$pagenum){           
            $pager = new pager;
            $pager->setAll($list,$item,$pagenum);
            return $pager;
	}
}
?>
PagerService.class.php
<?php
class PagerService {
    public function filter($list,$start,$item){
        $n = 0;
        $end = $start + $item;
        $newlist = array();
        foreach($list as $i){
            if($start <= $n && $n < $end){
                $newlist[$n] = $i;
            }
            $n++;
        }
        return $newlist;
    }
}
?>
2
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?