程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP入門知識 >> PHP代碼實例:常用的PHP分頁類

PHP代碼實例:常用的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));
}
}
?>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved