PHP代码实例:常用的PHP分页类

2010-01-28 19:59:17  来源:网页教学网 整理 

WebjxCom提示:PHP代码实例:常用的PHP分页类.

工作经常会用到的一个分页类..记录一下.

/**
Example: 
 
$page = (isset($this->params[named][page])) ? $this->params[named][page] : 1;
// 导入分页类
App::import(Vendor, page_class);
// 获取公告总数
$total = $this->Bulletin->getBulletinTotal();
$perpage = ITEMPREPAGE;
$offset = $perpage * ($page - 1);
$pagebar = new Pagebar($total, $page, $offset, $perpage);
$pagenav = $pagebar->construct_page_nav($total, $page, SYSTEMURL . /bulletins/index, null, null);
*/
 
perpage = $perpage;
 
$numpages = ceil($numresults / $this->perpage);
if ($numpages == 0)
{
$numpages = 1;
}
 
if ($page < 1)
{
$page = 1;
}
else if ($page > $numpages)
{
$page = $numpages;
}
 
$limitlower = ($page - 1) * $this->perpage;
$limitupper = ($page) * $this->perpage;
 
if ($limitupper > $numresults)
{
$limitupper = $numresults;
if ($limitlower > $numresults)
{
$limitlower = ($numresults - $this->perpage) - 1;
}
}
if ($limitlower < 0)
{
$limitlower = 0;
}
}
 
/**
* Returns the HTML for multi-page navigation
*
* @param	integer	Total number of items found
* @param	integer	Page number being displayed
* @param	string	Base address for links eg: q.php?t=99{&page=4}
* @param	string	Ending portion of address for links
*
* @return	string	Page navigation HTML
*/
function construct_page_nav($results, $pagenumber, $address, $address2 = , $option = array())
{
$perpage = $this->perpage;
 
$show = array();
$curpage = 0;
$pagenav = ;
$firstlink = ;
$prevlink = ;
$lastlink = ;
$nextlink = ;
 
if ($results <= $perpage)
{
$show[pagenav] = false;
return ;
}
 
$show[pagenav] = true;
 
$total = number_format($results);
$totalpages = ceil($results / $perpage);
 
$show[prev] = false;
$show[next] = false;
$show[first] = false;
$show[last] = false;
 
if ($pagenumber > 1)
{
$prevpage = $pagenumber - 1;
$prevnumbers = $this->fetch_start_end_total_array($prevpage, $perpage, $results);
$show[prev] = true;
}
if ($pagenumber < $totalpages)
{
$nextpage = $pagenumber + 1;
$nextnumbers = $this->fetch_start_end_total_array($nextpage, $perpage, $results);
$show[next] = true;
}
 
// create array of possible relative links that we might have (eg. +10, +20, +50, etc.)
$pagenavsarr = array(10, 50, 100, 500, 1000);
 
$pagenavpages = 5;
 
while ($curpage++ < $totalpages)
{
if (abs($curpage - $pagenumber) >= $pagenavpages AND $pagenavpages != 0)
{
if ($curpage == 1)
{
$firstnumbers = $this->fetch_start_end_total_array(1, $perpage, $results);
$show[first] = true;
}
if ($curpage == $totalpages)
{
$lastnumbers = $this->fetch_start_end_total_array($totalpages, $perpage, $results);
$show[last] = true;
}
// generate relative links (eg. +10,etc).
if (in_array(abs($curpage - $pagenumber), $pagenavsarr) AND $curpage != 1 AND $curpage != $totalpages)
{
$pagenumbers = $this->fetch_start_end_total_array($curpage, $perpage, $results);
$relpage = $curpage - $pagenumber;
 
if ($relpage > 0)
{
$relpage = + . $relpage;
}
 
$pagenav .= " $curpage ";
}
}
else
{
if ($curpage == $pagenumber)
{
$numbers = $this->fetch_start_end_total_array($curpage, $perpage, $results);
$pagenav .= "	 $curpage ";
}
else
{
$pagenumbers = $this->fetch_start_end_total_array($curpage, $perpage, $results);
$pagenav .= " $curpage ";
}
}
}
$pagenav = "

".(($show[prev]) ? ("上一页") : ("上一页"))."
 
$pagenav
 
".(($show[next]) ? ("下一页") : ("下一页"))."
 
第 $pagenumber / $totalpages 页,共 $total条记录";
return $pagenav;
}
/**
* Returns an array so you can print Showing results $arr[first] to $arr[last] of $totalresults
*
* @param	integer	Current page number
* @param	integer	Results to show per-page
* @param	integer	Total results found
*
* @return	array	In the format of - array(first => x, last => y)
*/
function fetch_start_end_total_array($pagenumber, $perpage, $total)
{
$first = $perpage * ($pagenumber - 1);
$last = $first + $perpage;
 
if ($last > $total)
{
$last = $total;
}
$first++;
 
return array(first => number_format($first), last => number_format($last));
}
}
?>
更多