詳細(xì)介紹:
記得以前總覺得數(shù)據(jù)庫分頁麻煩,而網(wǎng)上的一些分頁類又總感覺太繁瑣,于是這個(gè)類就誕生了。:d
當(dāng)初寫這個(gè)類的時(shí)候就是想把分頁簡單化。您在使用這個(gè)類的時(shí)候只要把記錄數(shù),頁數(shù)大小和要傳遞的變量傳過來,剩下的一切都會(huì)自動(dòng)搞定。有什么問題請到論壇提出。
<?php
/**
* 一個(gè)用于mysql數(shù)據(jù)庫的分頁類
*
* @author avenger <[email protected]>
* @version 1.0
* @lastupdate 2003-04-08 11:11:33
*
*
* 使用實(shí)例:
* $p = new show_page; //建立新對(duì)像
* $p->file="ttt.php"; //設(shè)置文件名,默認(rèn)為當(dāng)前頁
* $p->pvar="pagecount"; //設(shè)置頁面?zhèn)鬟f的參數(shù),默認(rèn)為p
* $p->setvar(array("a" => '1', "b" => '2')); //設(shè)置要傳遞的參數(shù),要注意的是此函數(shù)必須要在 set 前使用,否則變量傳不過去
* $p->set(20,2000,1); //設(shè)置相關(guān)參數(shù),共三個(gè),分別為'頁面大小'、'總記錄數(shù)'、'當(dāng)前頁(如果為空則自動(dòng)讀取get變量)'
* $p->output(0); //輸出,為0時(shí)直接輸出,否則返回一個(gè)字符串
* echo $p->limit(); //輸出limit子句。在sql語句中用法為 "select * from table limit {$p->limit()}";
*
*/
class show_page {
/**
* 頁面輸出結(jié)果
*
* @var string
*/
var $output;
/**
* 使用該類的文件,默認(rèn)為 php_self
*
* @var string
*/
var $file;
/**
* 頁數(shù)傳遞變量,默認(rèn)為 'p'
*
* @var string
*/
var $pvar = "p";
/**
* 頁面大小
*
* @var integer
*/
var $psize;
/**
* 當(dāng)前頁面
*
* @var ingeger
*/
var $curr;
/**
* 要傳遞的變量數(shù)組
*
* @var array
*/
var $varstr;
/**
* 總頁數(shù)
*
* @var integer
*/
var $tpage;
/**
* 分頁設(shè)置
*
* @access public
* @param int $pagesize 頁面大小
* @param int $total 總記錄數(shù)
* @param int $current 當(dāng)前頁數(shù),默認(rèn)會(huì)自動(dòng)讀取
* @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> '; //輸出當(dāng)前頁數(shù)
} else {
$this->output.='<a href="'.$this->file.'?'.$this->pvar.'='.$i.$this->varstr.'">['.$i.']</a> '; //輸出頁數(shù)
}
}
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>';
}
}
}
/**
* 要傳遞的變量設(shè)置
*
* @access public
* @param array $data 要傳遞的變量,用數(shù)組來表示,參見上面的例子
* @return void
*/
function setvar($data) {
foreach ($data as $k=>$v) {
$this->varstr.='&'.$k.'='.urlencode($v);
}
}
/**
* 分頁結(jié)果輸出
*
* @access public
* @param bool $return 為真時(shí)返回一個(gè)字符串,否則直接輸出,默認(rèn)直接輸出
* @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;
}
} //end class
?>
新聞熱點(diǎn)
疑難解答