PHP分頁類
<?php /**
- 分頁類
- author lynnluo
- addtime 2011-08-15 */
define( 'PAGE_NAME','page' );// url '....php?page=2'中的page名稱 define( 'CR',"\n" );// 換行符
class page { private $prev_label = ' 上一頁 ';// private $next_label = ' 下一頁 ';// private $first_label = ' 首頁 ';// private $last_label = ' 尾頁 ';//
private $per_page = 10;//每頁顯示多少條
private $adjacent_num = 2;//當前頁相鄰的顯示量
private $page_num; //總頁數
private $page; //第幾頁
private $html;//返回分頁的html源碼
private $display_str_flag = true;//首頁和尾頁用字符串or數字顯示 true:字符串 其它則顯示數字
/**
* 構造函數
* $per_page int 每頁顯示多少條
* $totle int 總數量
*/
public function __construct( $per_page, $totle )
{
$this->per_page = intval($per_page)<1 ? $this->per_page : intval($per_page);//每頁顯示條數小于1時 默認為10
$this->page_num = ceil( $totle / $this->per_page );
$this->page = isset($_GET[PAGE_NAME]) ? ( (intval($_GET[PAGE_NAME])>0) ? intval($_GET[PAGE_NAME]):1) : 1;//當前頁碼小于1時默認為第一頁
$this->page = $this->page > $this->page_num ? $this->page_num : $this->page;//當前頁碼超過最大頁碼時默認為最后一頁
$this->html = '';
}
/**
* 私有屬性賦值時的錯誤處理
*/
public function __set( $aa ,$bb){}
/**
* 配置類的私有屬性
* $ary array 鍵值對,鍵為該類的私有屬性
*/
public function set( $ary = array( 'display_str_flag'=>false,
'prev_label'=>'>>',
'next_label'=>'>>',
'first_label'=>'>>',
'last_label'=>'>>',
'adjacent_num'=>5))
{
foreach( $ary as $key=>$value )
{
$this->{$key} = $value;
}
}
/**
* 返回當前URL
*/
private function getURL()
{
$url = $_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"],'?')?'':"?");
$parse=parse_url($url);
if(isset($parse['query']))
{
parse_str($parse['query'],$params);
unset($params[PAGE_NAME]);
$url = $parse['path'].'?'.http_build_query($params);
}
$url .= strpos($url, '?') ? (strpos($url, '=')?'&':'') : '' ;
return $url;
}
/**
* 首頁
*/
private function first_page()
{
if( $this->page == 1 )
{
$this->html .= '<span>' . $this->first_label . '</span>'.CR;
}
else
{
$this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'=1">' . $this->first_label . '</a>'.CR;
}
}
/**
* 尾頁
*/
private function last_page()
{
if( $this->page == $this->page_num )
{
$this->html .= '<span>' . $this->last_label . '</span>'.CR;
}
else
{
$this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'='.$this->page_num.'">' . $this->last_label . '</a>'.CR;
}
}
/**
* 上一頁
*/
private function prev_page()
{
if( $this->page == 1 )
{
$this->html .= '<span>' . $this->prev_label . '</span>'.CR;
}
else
{
$this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'=' . ( $this->page - 1 ) . '">' . $this->prev_label . '</a>'.CR;
}
}
/**
* 下一頁
*/
private function next_page()
{
if( $this->page < $this->page_num)
{
$this->html .= '<a href="' . $this->getURL().PAGE_NAME.'=' . ( $this->page+1) . '">' . $this->next_label . '</a>'.CR;
}
else
{
$this->html .= '<span>' . $this->next_label . '</span>'.CR;
}
}
/**
* 第一塊 第一個省略號前的那塊
*/
private function first_block()
{
if( $this->page > ( $this->adjacent_num+1 ) )
{
$this->html.= '<a href="' . $this->getURL().PAGE_NAME.'=1">1</a>'.CR;
}
if( $this->page > ( $this->adjacent_num+2 ) )
{
$this->html.= '...'.CR;
}
}
/**
* 第二塊 兩個省略號中間的那塊
*/
private function middle_block()
{
$page_min = ( $this->page > $this->adjacent_num ) ? ( $this->page - $this->adjacent_num ) : 1;
$page_max = ( $this->page < ($this->page_num-$this->adjacent_num)) ? ($this->page+$this->adjacent_num) : $this->page_num ;
for( $i=$page_min; $i<=$page_max; $i++)
{
if( $i == $this->page )
{
$this->html .= '<span class="current">' . $i . '</span>'.CR;
}
else
{
$this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'=' . $i . '">' . $i . '</a>'.CR;
}
}
}
/**
* 最后一塊 最后一個省略號后的的那塊
*/
private function last_block()
{
if( $this->page < ($this->page_num - $this->adjacent_num - 1))
{
$this->html .= '...'.CR;
}
if( $this->page < ($this->page_num-$this->adjacent_num ))
{
$this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'=' . $this->page_num . '">' . $this->page_num . '</a>'.CR;
}
}
/**
* 顯示分頁
*
* $out_flag bool 輸出和反回分頁html的標志 true:直接輸出 false:返回分頁的html
*
*/
public function display( $out_flag = false )
{
$this->html = '<div class="pagin">'.CR;
if( $this->display_str_flag === true )
{//首頁和尾頁用字符串顯示
$this->first_page(); //顯示首頁
$this->prev_page(); //顯示上一頁
$this->middle_block(); //顯示中間塊
$this->next_page(); //顯示下一頁
$this->last_page(); //顯示最后一頁
}
else
{//首頁和尾頁用數字顯示
$this->prev_page(); //顯示上一頁
$this->first_block(); //顯示第一塊
$this->middle_block(); //顯示中間塊
$this->last_block(); //顯示最后一塊
$this->next_page(); //顯示下一頁
}
$this->html .= '</div>';
if($out_flag === false)
{//返回分頁 html碼
return $this->html;
}
else
{//輸出分頁 html碼
echo $html;
}
}
}
/*demo **/ $page = new page(3,55); $page->set( $ary = array( 'display_str_flag'=>false, 'prev_label'=>'上', 'next_label'=>'下一頁', 'last_label'=>'末',)); echo $page->display(); /*demo **/ ?>
分頁類 ~ 6KB 下載(39)
?<?php /**
- 分頁類
- author lynnluo
- addtime 2011-08-15 */
define( 'PAGE_NAME','page' );// url '....php?page=2'中的page名稱 define( 'CR',"\n" );// 換行符
class page { private $prev_label = ' 上一頁 ';// private $next_label = ' 下一頁 ';// private $first_label = ' 首頁 ';// private $last_label = ' 尾頁 ';//
private $per_page = 10;//每頁顯示多少條
private $adjacent_num = 2;//當前頁相鄰的顯示量
private $page_num; //總頁數
private $page; //第幾頁
private $html;//返回分頁的html源碼
private $display_str_flag = true;//首頁和尾頁用字符串or數字顯示 true:字符串 其它則顯示數字
/**
* 構造函數
* $per_page int 每頁顯示多少條
* $totle int 總數量
*/
public function __construct( $per_page, $totle )
{
$this->per_page = intval($per_page)<1 ? $this->per_page : intval($per_page);//每頁顯示條數小于1時 默認為10
$this->page_num = ceil( $totle / $this->per_page );
$this->page = isset($_GET[PAGE_NAME]) ? ( (intval($_GET[PAGE_NAME])>0) ? intval($_GET[PAGE_NAME]):1) : 1;//當前頁碼小于1時默認為第一頁
$this->page = $this->page > $this->page_num ? $this->page_num : $this->page;//當前頁碼超過最大頁碼時默認為最后一頁
$this->html = '';
}
/**
* 私有屬性賦值時的錯誤處理
*/
public function __set( $aa ,$bb){}
/**
* 配置類的私有屬性
* $ary array 鍵值對,鍵為該類的私有屬性
*/
public function set( $ary = array( 'display_str_flag'=>false,
'prev_label'=>'>>',
'next_label'=>'>>',
'first_label'=>'>>',
'last_label'=>'>>',
'adjacent_num'=>5))
{
foreach( $ary as $key=>$value )
{
$this->{$key} = $value;
}
}
/**
* 返回當前URL
*/
private function getURL()
{
$url = $_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"],'?')?'':"?");
$parse=parse_url($url);
if(isset($parse['query']))
{
parse_str($parse['query'],$params);
unset($params[PAGE_NAME]);
$url = $parse['path'].'?'.http_build_query($params);
}
$url .= strpos($url, '?') ? (strpos($url, '=')?'&':'') : '' ;
return $url;
}
/**
* 首頁
*/
private function first_page()
{
if( $this->page == 1 )
{
$this->html .= '<span>' . $this->first_label . '</span>'.CR;
}
else
{
$this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'=1">' . $this->first_label . '</a>'.CR;
}
}
/**
* 尾頁
*/
private function last_page()
{
if( $this->page == $this->page_num )
{
$this->html .= '<span>' . $this->last_label . '</span>'.CR;
}
else
{
$this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'='.$this->page_num.'">' . $this->last_label . '</a>'.CR;
}
}
/**
* 上一頁
*/
private function prev_page()
{
if( $this->page == 1 )
{
$this->html .= '<span>' . $this->prev_label . '</span>'.CR;
}
else
{
$this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'=' . ( $this->page - 1 ) . '">' . $this->prev_label . '</a>'.CR;
}
}
/**
* 下一頁
*/
private function next_page()
{
if( $this->page < $this->page_num)
{
$this->html .= '<a href="' . $this->getURL().PAGE_NAME.'=' . ( $this->page+1) . '">' . $this->next_label . '</a>'.CR;
}
else
{
$this->html .= '<span>' . $this->next_label . '</span>'.CR;
}
}
/**
* 第一塊 第一個省略號前的那塊
*/
private function first_block()
{
if( $this->page > ( $this->adjacent_num+1 ) )
{
$this->html.= '<a href="' . $this->getURL().PAGE_NAME.'=1">1</a>'.CR;
}
if( $this->page > ( $this->adjacent_num+2 ) )
{
$this->html.= '...'.CR;
}
}
/**
* 第二塊 兩個省略號中間的那塊
*/
private function middle_block()
{
$page_min = ( $this->page > $this->adjacent_num ) ? ( $this->page - $this->adjacent_num ) : 1;
$page_max = ( $this->page < ($this->page_num-$this->adjacent_num)) ? ($this->page+$this->adjacent_num) : $this->page_num ;
for( $i=$page_min; $i<=$page_max; $i++)
{
if( $i == $this->page )
{
$this->html .= '<span class="current">' . $i . '</span>'.CR;
}
else
{
$this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'=' . $i . '">' . $i . '</a>'.CR;
}
}
}
/**
* 最后一塊 最后一個省略號后的的那塊
*/
private function last_block()
{
if( $this->page < ($this->page_num - $this->adjacent_num - 1))
{
$this->html .= '...'.CR;
}
if( $this->page < ($this->page_num-$this->adjacent_num ))
{
$this->html .= '<a href="' . $this->getURL() .PAGE_NAME.'=' . $this->page_num . '">' . $this->page_num . '</a>'.CR;
}
}
/**
* 顯示分頁
*
* $out_flag bool 輸出和反回分頁html的標志 true:直接輸出 false:返回分頁的html
*
*/
public function display( $out_flag = false )
{
$this->html = '<div class="pagin">'.CR;
if( $this->display_str_flag === true )
{//首頁和尾頁用字符串顯示
$this->first_page(); //顯示首頁
$this->prev_page(); //顯示上一頁
$this->middle_block(); //顯示中間塊
$this->next_page(); //顯示下一頁
$this->last_page(); //顯示最后一頁
}
else
{//首頁和尾頁用數字顯示
$this->prev_page(); //顯示上一頁
$this->first_block(); //顯示第一塊
$this->middle_block(); //顯示中間塊
$this->last_block(); //顯示最后一塊
$this->next_page(); //顯示下一頁
}
$this->html .= '</div>';
if($out_flag === false)
{//返回分頁 html碼
return $this->html;
}
else
{//輸出分頁 html碼
echo $html;
}
}
}
/*demo **/
$page = new page(3,55);
$page->set( $ary = array( 'display_str_flag'=>false,
'prev_label'=>'上',
'next_label'=>'下一頁',
'last_label'=>'末',));
echo $page->display();
/*demo **/
?></pre>