コピペで簡単にページネーションを!ページネーションクラス記載

このクラスの使い方はこちら=>必見! ページネーションクラスを使ったページネーションを実装!(コード記載してあります!)

<?php
/*

ページネーション実行クラス

*/

class Pagination {

/* 1ページに表示する最大件数 */
private $max_view = null;

/* 総件数 */
private $total = null;

/* ページ数 */
private $max_page = null;

/* 初期ページ */
private $index = null;

/* 前に表示するページの数 */
private $before_link_count = null;

/* 後に表示するページの数 */
private $after_link_count = null;

/* コンストラクタ */
function  __construct(){
	$this->set_max_view();
	$this->set_after_link_count();
	$this->set_before_link_count();
	$this->set_start_page_number();
}


/* 総件数を設定 */
public function set_total($total_number){
	$this->total = $total_number;
}

/* ページ数取得 */
public function set_max_page(){
	$this->max_page = ceil( $this->get_total() / $this->get_max_view() );
}

/* 後に表示するページのリンクの数を設定 */
public function set_after_link_count( $after_link_count = 1 ){
	$this->after_link_count = $after_link_count;
}

/* 前に表示するページのリンクの数を設定 */
public function set_before_link_count( $before_link_count = 1 ){
	$this->before_link_count = $before_link_count;
}

/* 初期のページ番号を設定 */
public function set_start_page_number($start_page_number = 1){
	$this->index = $start_page_number;
}

/* 1ページに表示する件数を設定 */
public function set_max_view($max_view_number = 5){
	$this->max_view = $max_view_number;
}


/*
	表示中の件数を取得  何件中 何件目~何件目
	@param  string  $current_page  	現在のページ番号
	@return array   $array			総件数、始まり位置、終わり位置 を返す
*/
public function get_count_display( $current_page ){
	$total = $this->get_total();
	$max_view = $this->get_max_view();
	$max_page = $this->get_max_page();
	$index = $this->get_start_page_number();
	$current_page = $this->isset_current_page( $current_page );

	/*現在のアクセスページが初期ページの場合*/
	if( $current_page == $index ){
		$start = $this->get_start_page_number();
		if( $total > $max_view ){
			$end = $max_view;
		}else if( $total <= $max_view ){ 
                   $end = $total; 
                } 
        }else 
        /*現在のアクセスページが最後のページの場合*/ 
        if( $current_page == $max_page ){ 
            $start = $max_view * ( $current_page - 1 ) +1; $end = $total; 
        } 
                    /*1現在のアクセスページが最初と最後以外の場合*/ 
        else{ 
            $start = $max_view * ( $current_page - 1 ) +1; 
            $end = $max_view * $current_page; 
        } 
        return array( 'total'=>$total , 'start'=>$start, 'end'=>$end );
}


/*
	ページ番号取得
	@param  string  $current_page	現在のページ番号
	@return  array   $span           現在のページと前後のページ番号の取得
*/

public function get_page_number( $current_page ){
	$before_link_count = $this->get_before_link_count();
	$after_link_count = $this->get_after_link_count();
	$index = $this->get_start_page_number();
	$max_page = $this->get_max_page();
	$current_page = $this->isset_current_page( $current_page );

	/* ページング処理 */
	 $span="";
	if( $max_page != $index ){
		/* 数字表示 */
		$i=0;
		$flag = true;
		while ( $flag ){
			$i++;
			if( $current_page == $i ){
				$span.=''.$i.'';
			}

			/* 現在のページが1~3 */
			if( $current_page <=  ( $index + $after_link_count ) ){
				if( $current_page != $i ){
					 $span.='<span><a href="?page='.$i.'">'.$i.'</a></span>';
				}
				if( $this->max_page < ( $before_link_count + $index + $after_link_count ) ){ $max_page = $this->get_max_page();
				}else{
					$max_page = ( $before_link_count + $index + $after_link_count );
				}
				if( $i == $max_page ){
					$flag = false;
					break;
				}
			/* 現在のページが 最後-2~最後 */
			}else if(  $current_page >= ( $max_page - $before_link_count )  ){
				if( $current_page != $i && $i >  $max_page- ( $before_link_count + $index + $after_link_count ) ){
					 $span.='<span><a href="?page='.$i.'">'.$i.'</a></span>';
				}
				if($max_page == $i){
					$flag = false;
					break;
				}
			/*それ以外*/
			}else{
				if( $current_page != $i && $current_page - $before_link_count <= $i ){
					 $span.='<span><a href="?page='.$i.'">'.$i.'</a></span>';
					if( $current_page +$after_link_count == $i ){
						$flag = false;
						break;
					}
				}
			}
		}
	}
	return $span;
}


/*
	次へ 前へ 取得
	@param  string  $current_page	現在のページ番号
	@return array   $array          次へ前へ 最前 最後を返す(リンク)
*/
public function get_shortcut_link( $current_page ){
	$pvre="";
	$first="";
	$last="";
	$next="";
	$index = $this->get_start_page_number();
	$max_page = $this->get_max_page();
	$current_page = $this->isset_current_page( $current_page );
	if( $max_page != $index ){
          /* 日本語表示 */
          if( $current_page == $index ){
             $current_page = $current_page +1;
             $next ='<span><a href="?page='.$current_page.'">次へ></a></span>';
             $last ='<span><a href="?page='.$max_page.'">最後</a></span>';
          }else if( $current_page == $max_page ){
             $current_page = $current_page -1;
             $pvre ='<span><a href="?page='.$current_page.'"><前へ</a></span>';
             $first ='<span><a href="?page='.$index.'">最前</a></span>';
          }else{
             $current_page_next = $current_page +1;
             $current_page_prev = $current_page -1;
             $next ='<span><a href="?page='.$current_page_next.'">次へ></a></span>';
             $pvre ='<span><a href="?page='.$current_page_prev.'"><前へ</a></span>';
             $last ='<span><a href="?page='.$max_page.'">最後</a></span>';
             $first ='<span><a href="?page='.$index.'">最前</a></span>';
         }
       }
	return array( 'pvre'=>$pvre, 'first'=>$first, 'last'=>$last, 'next'=>$next );
}


/*
	実データを取得 ページング機能
	セッションに入れて使うことを想定
	@param array $data   データ配列
	@param  string  $current_page	現在のページ番号
	@return array   $cell           ページングデータ
*/
public function get_pagination_data( $data , $current_page ){
	$cell = array();
	$max_view = $this->get_max_view();

	$current_page = $this->isset_current_page( $current_page );

	$start = ( $current_page - 1 ) * $max_view;
	$end = $current_page * $max_view;

	for ( $i = $start;  $i < $end; $i++ ){ if( isset( $data[$i] ) ){ $cell[] = $data[$i]; } } return $cell; 
} 

/*
ページング機能 sqlに入れて使うことを想定 
@param string $current_page 現在のページ番号
@return array $array limitを返す limit $limit['cut'] , $limit['view'] 
*/ 
public function get_db_limit( $current_page ){ $start = ( $current_page - 1 ) * $this->get_max_view() ;
	return array( 'cut'=>$start , 'view'=> $this->get_max_view() ) ;
}


/*
	現在のページがセットされているか
	@param  string  $current_page	現在のページ番号
*/
private function isset_current_page( $current_page ){
	if( ! isset( $current_page ) ){
		$current_page = 1;
	}
	return $current_page;
}


/* 後に表示するページのリンクの数を返す */
private function get_after_link_count(){
	return $this->after_link_count;
}

/* 前に表示するページのリンクの数を返す */
private function get_before_link_count(){
	return $this->before_link_count;
}

/* 初期のページ番号を返す */
private function get_start_page_number(){
	return $this->index;
}

/* 総件数を返す */
private  function get_total(){
	return $this->total;
}

/* 1ページに表示する件数を返す */
private function get_max_view(){
	return $this->max_view;
}

/* ページ数を返す */
private function get_max_page(){
	return $this->max_page;
}

}//end class 

シェアする

  • このエントリーをはてなブックマークに追加

フォローする