:::
14-2 新增分頁
- 目前活動少,所以不分頁也沒什麼關係,但未來活動一多時,沒分頁可就麻煩了。
- 分頁可以自己寫,但麻煩,直接套用現成物件會更簡單~
一、在列表函數中加入分頁
- 下載 PageBar fot bootstrap 分頁物件
- 下載解壓到 class/PageBar.php
- 修改 index.php 中的 list_action() 函數:
//列出所有活動 function list_action() { global $db, $smarty; $where = (!isset($_SESSION['group']) or $_SESSION['group'] != "admin") ? 'where `end_date` > now() and `enable`=1' : ''; $sql = "SELECT * FROM `actions` $where order by action_date desc"; include_once "class/PageBar.php"; $PageBar = getPageBar($db, $sql, 5, 10); $bar = $PageBar['bar']; $sql = $PageBar['sql']; $total = $PageBar['total']; $result = $db->query($sql); if (!$result) { throw new Exception($db->error); } $actions = []; while ($values = $result->fetch_assoc()) { $actions[] = $values; } $smarty->assign('actions', $actions); $smarty->assign('bar', $bar); $smarty->assign('total', $total); } - 必須放在原有的 $sql 和 $result 之間才行!
- getPageBar的參數
getPageBar($mysqli, $sql, $show_num = 20, $page_list = 10, $to_page = "", $url_other = "")
-
$mysqli:使用的資料庫物件名稱(必要)
-
$sql:欲執行的語法(必要)
-
$show_num = 20:每頁顯示資料數
-
$page_list = 10:分頁工具列呈現的頁數
-
$to_page = "":要連結到那一個頁面
-
$url_other = "":其他額外的連結參數
-
- 修改 templates/list_action.tpl 樣板,在標題處加入資料數:
<h2>活動列表<small>(共 {$total} 個活動)</small></h2> - 在最下面加入分頁工具列:
{$bar} - 大功告成!
14-1 過期活動不該顯示出來