:::
13-2 列出已報名名單
- 要先思考,要列在哪裡?(右邊空空...要不放在右側?)
- 好,放右側...所以,我們得做一個右側的樣板...就暫名為side_signups.tpl 好了。
- 那...請問何時要顯示已報名列表?(點進去活動時?)
- 好,那什麼情況才叫做「點進去活動時」?(網頁連到index.php,且 op=show_action 時!)
- 沒錯!所以,我們可以做個函數,放到 index.php,然後在op=show_action 時呼叫該函數,讓該函數把報名列表的值丟到樣板去列出。
一、修改流程
- 開啟 index.php 修改其流程,加入 list_signup($action_id);
default: if ($action_id) { $op = 'show_action'; show_action($action_id); list_signup($action_id); } else { $op = 'list_action'; list_action(); } break;
二、製作函數
- 一樣在 index.php 中,新增 list_signup() 函數
//已報名名單 function list_signup($action_id) { global $db, $smarty; $sql = "SELECT * FROM `signups` where `action_id` = '{$action_id}'"; if (!$result = $db->query($sql)) { throw new Exception($db->error); } $signups = []; while ($signup = $result->fetch_assoc()) { $signups[] = $signup; } $smarty->assign('signups', $signups); }
三、製作樣板
- 新增 templates/side_signups.tpl
{if $op=="show_action"} <h2>已報名名單</h2> <table class="table table-hover table-striped"> <thead> <tr class="info"> <th>姓名</th> </tr> </thead> <tbody> {foreach $signups as $signup} <tr> <td>{$signup.uid}</td> </tr> {/foreach} </tbody> </table> {/if} -
我們在第一行判斷目前 op 狀態,只有在 show_action 時才顯示。
-
由於目前我們沒讀出姓名,暫時先顯示uid編號即可。
四、連結樣板
- 修改 index.tpl 將剛剛做的樣板連結上來
<h2>Hello {$name}!</h2> {if $group} {include file='side_tools.tpl'} {include file='side_signups.tpl'} {else} {include file='side_login.tpl'} {/if}
五、練習
- 關於 side_signups.tpl 的顯示,是否可以改成判斷有無 $action_id 或者有無傳來 $signups 才顯示呢?其實是不妥的,答案...請想想。
13-1 怎樣才叫做已經報名?