對於用戶信息列表,將其作為表單在頁面中按頁顯示出來。在本項目中,將用戶按每頁15條記錄展示出來,並且按最後互動時間降序排列,相關代碼如下。
1 public function _initialize 2 { 3 // 初始化時檢查用戶登錄狀態 4 if(!Session::has('username')){ 5 $this->redirect('login/index'); 6 } 7 } 8 9 public function index 10 { 11 // 查詢用戶數據,並且每頁顯示15條數據 12 $list = Db::name('user')->order('heartbeat desc')->paginate(15); 13 14 // 把分頁數據賦值給模板變量list 15 $this->assign('list', $list); 16 // 渲染模板輸出 17 return $this->fetch; 18 }
模板文件中顯示用戶信息及分頁的代碼如下。
1 <p> 2 <ul> 3 <a href="{:url('User/accesstoken')}">Access Token</a> 4 <a href="{:url('User/updateList')}">更新列表</a> 5 <a href="{:url('User/updateInfo')}">更新數據</a> 6 </ul> 7 8 </p> 9 <p> 10 <table cellspacing="0"> 11 <thead> 12 <tr> 13 <th >ID</th> 14 <th >微信OpenID</th> 15 <th >暱稱</th> 16 <th >性別</th> 17 <th >地區</th> 18 <th >來源</th> 19 <th >標籤</th> 20 <th >頭像</th> 21 <th >關注時間</th> 22 <th >積分</th> 23 <th >最後互動</th> 24 <th >操作</th> 25 </tr> 26 </thead> 27 <tbody> 28 {volist name="list" key="k"} 29 <tr> 30 <td >{$user.id}</td> 31 <td >{$user['openid']}</td> 32 <td >{$user['nickname']}</td> 33 <td >{$user['sex']}</td> 34 <td >{$user['country']}{$user['province']}{$user['city']} {$user['district']}</td> 35 <td >{$user['scene']}</td> 36 <td >{$user['tagid']}</td> 37 <td ><img src="{$user.headimgurl}" height= "25px"></td> 38 <td >{if condition="$user['subscribe']==''"}{else/} {$user['subscribe']|date="Y-m-d H:i:s",###}{/if}</td> 39 <td >{$user['score']}</td> 40 <td >{if condition="$user['heartbeat']==''"}{else/} {$user['heartbeat']|date="Y-m-d H:i:s",###}{/if}</td> 41 <td > 42 <a href="javascript:confirm_delete('{:url('User/delete',array('id'=> $user['id']))}')">刪除</a> 43 </td> 44 </tr> 45 {/volist} 46 </tbody> 47 </table> 48 <p>{$list->render}</p> 49 </p>
本項目對ThinkPHP框架的分頁功能進行了改造,增加了分頁功能中的參數顯示,最終的用戶列表分頁顯示頁面如圖25-6所示。
圖25-6 用戶信息列表