另一个PHP分页类(Pagination)

Posted by linyupark :: 2007,November 22nd,12:08 pm

以前放那个分页类在用的时候发现了点问题,自定义的空间比较小,在AJAX分页的时候就没不能法用了。刚好自己在做个整站类库,今天更新到分页了,再发布一下,希望能对一些朋友有帮助

page类,分页类文件内容:

<?phpclass Page

{

private $_config = array(

"urlOpen"    =>    "",            //*

"urlClose"    =>    "",            //*

"numRows"    =>    0,            //*

"numPages"    =>    0,

"perPage"    =>    10,

"linkSpan"    =>    4,

"curPage"    =>    1,

"firstLink"    =>    "第一页",

"prevLink"    =>    "上一页",

"nextLink"    =>    "下一页",

"lastLink"    =>    "最后页",

"firstOpen"    =>    "",

"firstClose"=>    " ",

"prevOpen"    =>    "",

"prevClose"    =>    " ",

"nextOpen"    =>    "",

"nextClose"    =>    " ",

"lastOpen"    =>    "",

"lastClose"    =>    "",

"linkOpen"    =>    "",

"linkClose"    =>    "",

"curOpen"    =>    "<b>",

"curClose"    =>    "</b>",

"pagination"=>    ""

);

function __construct($config)

{

if(is_array($config) && count($config) > 0){

foreach ($config as $key => $val){

if(array_key_exists($key,$this->_config))

$this->_config[$key] = $val;

}

$this->init();

}

else throw new Exception("Pagination's config can not be blank!");

}

function offset()

{

return ($this->_config['curPage']-1)*$this->_config['perPage'];

}

function link()

{

return $this->_config['pagination'];

}

function getInfo()

{

return $this->_config;

}

function init()

{

if($this->_config['numRows'] > $this->_config['perPage']){

$this->_config['numPages'] = ceil($this->_config['numRows']/$this->_config['perPage']);

$pagination = "";

if($this->_config['curPage'] != 1){

$pagination .= sprintf($this->_config['urlOpen'].

$this->_config['firstOpen'].

$this->_config['firstLink'].

$this->_config['firstClose'].

$this->_config['urlClose'],1);

$pagination .= sprintf($this->_config['urlOpen'].

$this->_config['prevOpen'].

$this->_config['prevLink'].

$this->_config['prevClose'].

$this->_config['urlClose'],($this->_config['curPage']-1));

}

if($this->_config['curPage'] - $this->_config['linkSpan'] > 1)

$startLink = $this->_config['curPage']-$this->_config['linkSpan'];

else $startLink = 1;

if($this->_config['curPage'] + $this->_config['linkSpan'] > $this->_config['numPages'])

$endLink = $this->_config['numPages'];

else $endLink = $this->_config['curPage'] + $this->_config['linkSpan'];

for($i = $startLink; $i <= $endLink; $i++){

if($i == $this->_config['curPage'])

$pagination .= sprintf(

$this->_config['curOpen'].

$i.

$this->_config['curClose'],$i);

else

$pagination .= sprintf($this->_config['urlOpen'].

$this->_config['linkOpen'].

$i.

$this->_config['linkClose'].

$this->_config['urlClose'],$i);

}

if($this->_config['curPage'] != $this->_config['numPages']){

$pagination .= sprintf($this->_config['urlOpen'].

$this->_config['nextOpen'].

$this->_config['nextLink'].

$this->_config['nextClose'].

$this->_config['urlClose'],($this->_config['curPage']+1));

$pagination .= sprintf($this->_config['urlOpen'].

$this->_config['lastOpen'].

$this->_config['lastLink'].

$this->_config['lastClose'].

$this->_config['urlClose'],$this->_config['numPages']);

}

$this->_config['pagination'] = $pagination;

}

}

}

?>

使用举例:

$Page = new Page(array(
"numRows"    =>    100,
"urlOpen"    =>    "<a href='/test.php?p=%d'>",
"urlClose"    =>    "</a>",
"curPage"    =>    $_GET['p']

));
echo $Page->link();

Leave a Reply