class show_page {
/**
* 頁面輸出結果
*
* @var string
*/
var $output;
/**
* 使用該類的文件,默認為 PHP_SELF
*
* @var string
*/
var $file;
/**
* 頁數傳遞變量,默認為 'p'
*
* @var string
*/
var $pvar = "p";
/**
* 頁面大小
*
* @var integer
*/
var $psize;
/**
* 當前頁面
*
* @var ingeger
*/
var $curr;
/**
* 要傳遞的變量數組
*
* @var array
*/
var $varstr;
/**
* 總頁數
*
* @var integer
*/
var $tpage;
/**
* 分頁設置
*
* @access public
* @param int $pagesize 頁面大小
* @param int $total 總記錄數
* @param int $current 當前頁數,默認會自動讀取
* @return void
*/
function set($pagesize=20,$total,$current=false) {
global $HTTP_SERVER_VARS,$HTTP_GET_VARS;
$this->tpage = ceil($total/$pagesize);
if (!$current) {$current = $HTTP_GET_VARS[$this->pvar];}
if ($current>$this->tpage) {$current = $this->tpage;}
if ($current<1) {$current = 1;}
$this->curr = $current;
$this->psize = $pagesize;
if (!$this->file) {$this->file = $HTTP_SERVER_VARS['PHP_SELF'];}
if ($this->tpage > 1) {
if ($current>10) {
$this->output.='<a href='.$this->file.'?'.$this->pvar.'='.($current-10).($this->varstr).' title="前十頁"><<<</a> ';
}
if ($current>1) {
$this->output.='<a href='.$this->file.'?'.$this->pvar.'='.($current-1).($this->varstr).' title="前一頁"><<</a> ';
}
$start = floor($current/10)*10;
$end = $start+9;
if ($start<1) {$start=1;}
if ($end>$this->tpage) {$end=$this->tpage;}
for ($i=$start; $i<=$end; $i++) {
if ($current==$i) {
$this->output.='<font color="red">'.$i.'</font> '; //輸出當前頁數
} else {
$this->output.='<a href="'.$this->file.'?'.$this->pvar.'='.$i.$this->varstr.'">['.$i.']</a> '; //輸出頁數
}
}
if ($current<$this->tpage) {
$this->output.='<a href='.$this->file.'?'.$this->pvar.'='.($current+1).($this->varstr).' title="下一頁">>></a> ';
}
if ($this->tpage>10 && ($this->tpage-$current)>=10 ) {
$this->output.='<a href='.$this->file.'?'.$this->pvar.'='.($current+10).($this->varstr).' title="下十頁">>>></a>';
}
}
}
/**
* 要傳遞的變量設置
*
* @access public
* @param array $data 要傳遞的變量,用數組來表示,參見上面的例子
* @return void
*/
function setvar($data) {
foreach ($data as $k=>$v) {
$this->varstr.='&'.$k.'='.urlencode($v);
}
}
/**
* 分頁結果輸出
*
* @access public
* @param bool $return 為真時返回一個字符串,否則直接輸出,默認直接輸出
* @return string
*/
function output($return = false) {
if ($return) {
return $this->output;
} else {
echo $this->output;
}
}
/**
* 生成Limit語句
*
* @access public
* @return string
*/
function limit() {
return (($this->curr-1)*$this->psize).','.$this->psize;
}
}