PHP 分頁類
來源:互聯網
上載者:User
下面是我自己的分頁類,可適當的修改 [php] <?php /** * @name Pager's class * @author Refer * @copyright www.phpcq.com * @version Alpha 1.0 * @todo 下拉式功能表分頁---From表單傳值...可以根據自己的情況(採用GET傳值) * */ class Pager{ /** * startRow * * @var Int */ public $startRow; /** * 頁面的總數 * * @var Int */ public $pageTotal; /** *翻頁值 * * @var Int */ public $offset; /** * 每頁所顯示記錄數 * * @var Int */ public $pageSize; /** * get參數 * * @var Int */ public $get; /** * post參數 * * @var Int */ public $post; /** * 建構函式--初始化翻頁類 (PS:^_^不錯的php開發學習交流群:256271784,驗證:gole,有興趣的話可以加入進來一起討論) * * @param Int $total * @param Int $pageSize */ public function __construct($total,$pageSize){ $this->get = (int)$_GET['startRow']; $this->post = (int)$_POST['pageNums']; $this->pageSize = $pageSize; $this->pageTotal = ceil($total / $pageSize); switch ($_GET['method']){ default: $this->startRow = 1; $this->offset = 0; break; case 'first': $this->first(); break; case 'previous': $this->previous(); break; case 'next': $this->next(); break; case 'last': $this->last(); break; } } /** * 首頁方法 * */ public function first(){ $this->startRow = 1; $this->offset = ($this->startRow - 1) * $this->pageSize; } /** * 上一頁方法 * */ public function previous(){ $this->startRow = $this->get - 1; if ($this->startRow < 1) { $this->startRow = 1; } $this->offset = ($this->startRow - 1) * $this->pageSize; } /** * 下一頁方法 * */ public function next(){ $this->startRow = $this->get + 1; if ($this->startRow > $this->pageTotal) { $this->startRow = $this->pageTotal; } $this->offset = ($this->startRow - 1) * $this->pageSize; } /** * 最後頁方法 * */ public function last(){ $this->startRow = $this->pageTotal; $this->offset = ($this->startRow - 1) * $this->pageSize; } /** * 下拉式功能表翻頁 * * @return Int */ public function numPages(){ if ($this->post) { $pageNums = "<option>$this->post</option>"; $this->startRow = $this->post; $this->offset = ($this->post - 1) * $this->pageSize; } for ($i = 1; $i <= $this->pageTotal;$i++){ if ($i != $this->post) { $pageNums .= "<option>$i</option>"; } } return $pageNums; } /** * 當前是第幾頁 * * @return Int */ public function noncePage(){ if ($this->post) { return $this->startRow = $this->post; } else { return $this->startRow = $this->startRow; } } } 調用方法: [php] $total = $total->total();//總記錄 $pageSize = 15;//每頁顯示的記錄 $pager = new Pager($total,$pageSize); if ($pager->post){$startRow = $pager->noncePage();} $startRow = $pager->startRow; $pager->numPages(); $article->query("SELECT * FROM `categories` as c,`articles` as n WHERE c.id = n.pid ORDER BY n.id DESC LIMIT $pager->offset,$pageSize"); while ($article->fetchArray()) { $article->data['content'] = mb_substr(strip_tags($article->data['content']),0,40,'utf-8'); $articleIndex[] = $article->data; } $article->free(); } else { header("location:?act=login"); } $view->view->assign('startRow',$startRow);//caption $view->view->assign('total',$total);//caption $view->view->assign('pageNums',$pager->numPages());//pageNums $view->view->assign('noncePage',$pager->noncePage());//pageNums $view->view->assign('pageTotal',$pager->pageTotal);//pageTotal 模板頁面: [php] <!--{if $pageTotal > 1}--> <form action="" method="post"> <div id="projectpages" class="fontpage"> Pager: <select name="pageNums" onchange="javascript:submit();" style="height:20px;"> <!--{$numPages}--> </select> <a href="?act=blog&method=first&startRow=<!--{$startRow}-->">First</a> <a href="?act=blog&method=previous&startRow=<!--{$startRow}-->">Previous</a> <a href="?act=blog&method=next&startRow=<!--{$startRow}-->">Next</a> <a href="?act=blog&method=last&startRow=<!--{$startRow}-->">Last</a> </div> </form> <!--{/if}-->