<?php if (!defined( 'BASEPATH' )) exit ( 'No direct script access allowed' );
class Mypage_class {
public $part =2;
public $totalpage =0;
public $url = '' ;
public $total =0;
public $perpage =5;
public $nowindex =1;
public $seg =3;
public $next_page = '下一页' ;
public $pre_page = '上一页' ;
public $first_page = '首页' ;
public $last_page = '末页' ;
public function __construct( $params = array ())
{
if ( count ( $params ) > 0)
{
$this ->initialize( $params );
}
log_message( 'debug' , "MyPage Class Initialized" );
}
function initialize( $params )
{
if ( count ( $params ) > 0)
{
$this ->total=isset( $params [ 'total' ]) ? intval ( $params [ 'total' ]) : 0;
$this ->perpage=isset( $params [ 'perpage' ]) ? intval ( $params [ 'perpage' ]) : 5;
$this ->nowindex=isset( $params [ 'nowindex' ]) ? intval ( $params [ 'nowindex' ]) : 1;
$this ->url=isset( $params [ 'url' ]) ? $params [ 'url' ] : '' ;
$this ->part=isset( $params [ 'part' ]) ? $params [ 'part' ] : 2;
$this ->seg=isset( $params [ 'seg' ]) ? $params [ 'seg' ] : 3;
$this ->next_page=isset( $params [ 'next_page' ]) ? $params [ 'next_page' ] : '下一页' ;
$this ->pre_page=isset( $params [ 'pre_page' ]) ? $params [ 'pre_page' ] : '上一页' ;
$this ->first_page=isset( $params [ 'first_page' ]) ? $params [ 'first_page' ] : '首页' ;
$this ->last_page=isset( $params [ 'last_page' ]) ? $params [ 'last_page' ] : '末页' ;
}
$this ->totalpage= ceil ( $this ->total / $this ->perpage);
$this ->_myset_url( $this ->url);
}
function next_page( $style = 'pg_next' )
{
if ( $this ->nowindex < $this ->totalpage){
return $this ->_get_link( $this ->_get_url( $this ->nowindex + 1), $this ->next_page, $style );
}
return '<span class="' . $style . '">' . $this ->next_page. '</span>' ;
}
function pre_page( $style = 'pg_pre' )
{
if ( $this ->nowindex > 1){
return $this ->_get_link( $this ->_get_url( $this ->nowindex-1), $this ->pre_page, $style );
}
return '<span class="' . $style . '">' . $this ->pre_page. '</span>' ;
}
function first_page( $style = 'pg_first' )
{
if ( $this ->nowindex == 1){
return '<span class="' . $style . '">' . $this ->first_page. '</span>' ;
}
return $this ->_get_link( $this ->_get_url(1), $this ->first_page, $style );
}
function last_page( $style = 'pg_last' )
{
if ( $this ->nowindex == $this ->totalpage){
return '<span class="' . $style . '">' . $this ->last_page. '</span>' ;
}
return $this ->_get_link( $this ->_get_url( $this ->totalpage), $this ->last_page, $style );
}
function nowbar( $style = 'pg_link' , $nowindex_style = 'pg_curr' )
{
$plus = $this ->part;
$begin =1;
$end = $this ->totalpage;
if ( $this ->nowindex > $plus ) {
$begin = $this ->nowindex- $plus ;
$end = $this ->nowindex + $plus ;
if ( $end > $this ->totalpage) {
$begin = ( $begin - $end + $this ->totalpage>0) ? ( $begin - $end + $this ->totalpage) : 1;
$end = $this ->totalpage;
}
} else {
$begin =1;
$end = $begin + 2* $plus ;
$end = $end > $this ->totalpage ? $this ->totalpage : $end ;
}
$out = '' ;
for ( $i = $begin ; $i <= $end ; $i ++)
{
if ( $i != $this ->nowindex){
$out .= $this ->_get_link( $this ->_get_url( $i ), $i , $style );
} else {
$out .= '<span class="' . $nowindex_style . '">' . $i . '</span>' ;
}
}
return $out ;
}
function select()
{
$out = '<select name="pagelect" class="pg_select">' ;
for ( $i =1; $i <= $this ->totalpage; $i ++)
{
if ( $i == $this ->nowindex){
$out .= '<option value="' . $i . '" selected>' . $i . '</option>' ;
} else {
$out .= '<option value="' . $i . '">' . $i . '</option>' ;
}
}
$out .= '</select>' ;
return $out ;
}
function show( $mode =1)
{
switch ( $mode )
{
case 1:
return $this ->pre_page(). $this ->nowbar(). $this ->next_page();
break ;
case 2:
return $this ->first_page(). $this ->pre_page(). $this ->nowbar(). $this ->next_page(). $this ->last_page(). ' 第' . $this ->select(). '页' ;
break ;
case 3:
return $this ->pre_page(). $this ->nowbar(). $this ->next_page();
break ;
default :
return $this ->pre_page(). $this ->nowbar(). $this ->next_page(). ' 第' . $this ->select(). '页' ;
break ;
}
}
public function _myset_url( $url )
{
$CI =&get_instance();
$CI ->load->helper( 'url' );
if ( empty ( $url )) {
$cururl = '' ;
$cururl =uri_string();
$segementarray = explode ( "/" , $cururl );
$c =0;
for ( $i =0; $i < sizeof( $segementarray ); $i ++) {
if ( $segementarray [ $i ] && $c < $this ->seg-1) {
$url = $url . '/' . $segementarray [ $i ];
$c ++;
}
}
}
$this ->url=site_url( $url );
}
function _get_url( $pagenum =1)
{
return $this ->url. '/' . $pagenum ;
}
function _get_link( $url , $text , $style = '' ){
$style = $style ? 'class="' . $style . '"' : '' ;
return '<a ' . $style . ' href="' . $url . '">' . $text . '</a>' ;
}
}
?>
|