0%

[技能檢定]題組一 步驟4 製作訪客計數器及頁尾版權文字

由於第一題的前置作業較多,因此建議先把訪客計數器先完成,確認自訂函式及資料庫的存取正常,一來是先看到有個功能完成會比較心安,二來是確認一下前置作業的自訂函式部份有沒有問題。

  1. 打開 phpmyadmin ,分別在 totalbottom 資料表中先新增一筆資料:




  1. /Controller/Total.php 中增加一個可以讀取到目前的訪客人數的方法:
    /Controller/Total.php

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    include_once "DB.php";

    class Total extends DB{

    function __construct()
    {
    parent::__construct('total');
    }

    /**
    * 增加一個方法名為show()
    * 這個方法會找到total資料表中,id為1的資料,
    * 並回傳這筆資料中的total欄位值
    */
    function show(){
    return $this->find(1)['total'];
    }
    }
  2. 由於 show() 這個方法是公開的,因此我們可以直接在頁面上進行存取:
    /index.php/backend.php

    1
    2
    3
    <div class="dbor" style="margin:3px; width:95%; height:20%; line-height:100px;">
    <span class="t">進站總人數 :<?=$Total->show();?></span>
    </div>

    如果可以看到進站總人數的結果,那就表示資料庫的連線及DB類別的內容都是正確的。

  1. 完成進站人數的顯示後,可以按照一樣的流程來顯示頁尾版權文字。
    /Controller/Bottom.php

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    include_once "DB.php";

    class Bottom extends DB{

    function __construct()
    {
    parent::__construct('bottom');
    }

    /**
    * 增加一個方法名為show()
    * 這個方法會找到bottom資料表中,id為1的資料,
    * 並回傳這筆資料中的bottom欄位值
    */
    function show(){
    return $this->find(1)['bottom'];
    }
    }
  2. index.phpbackend.php 中顯示頁尾版權資料

    1
    2
    3
    4
    <div style="width:1024px; left:0px; position:relative; background:#FC3; 
    margin-top:4px; height:123px; display:block;">
    <span class="t" style="line-height:123px;"><?=$Bottom->show();?></span>
    </div>

快速的先完成這兩個只做讀取資料的功能,確認資料庫和類別都是可以正常運作的,接下來的開發也會比較順利