:::

14-2 新增分頁

  1. 目前活動少,所以不分頁也沒什麼關係,但未來活動一多時,沒分頁可就麻煩了。
  2. 分頁可以自己寫,但麻煩,直接套用現成物件會更簡單~

一、在列表函數中加入分頁

  1. 下載 PageBar fot bootstrap 分頁物件
  2. 下載解壓到 class/PageBar.php
  3. 修改 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);
    }
  4. 必須放在原有的 $sql 和 $result 之間才行!
  5. getPageBar的參數
    getPageBar($mysqli, $sql, $show_num = 20, $page_list = 10, $to_page = "", $url_other = "")
    1. $mysqli:使用的資料庫物件名稱(必要)

    2. $sql:欲執行的語法(必要)

    3. $show_num = 20:每頁顯示資料數

    4. $page_list = 10:分頁工具列呈現的頁數

    5. $to_page = "":要連結到那一個頁面

    6. $url_other = "":其他額外的連結參數

  6. 修改 templates/list_action.tpl 樣板,在標題處加入資料數:
    <h2>活動列表<small>(共 {$total} 個活動)</small></h2>
  7. 在最下面加入分頁工具列:
    {$bar}
    
  8. 大功告成!

:::

書籍目錄

展開 | 闔起

http%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D751

計數器

今天: 551551551
昨天: 3438343834383438
總計: 7390880739088073908807390880739088073908807390880