ThinkPHP? MVC ?? ??? ???? ?? PHP ????????. MVC ??????? ???? ?? ??? ??? ??? ?????. ???? ThinkPHP? ???? ??? ?????? ThinkPHP?? ???? ???? ??? ???????.
1. ThinkPHP ???? ?? ??
???? ??? ??? ??? N ???? ??? ???? ??? ? ? ??? ?? ????. ThinkPHP?? ? ?? ??? ???? ????. ??? ?? ????? ?? ??? AJAX ??????.
?? ???? ? ???? ?? ???? ?? ??? ?? ?????. ???? PHP ??? ?? ???? ??? ?? ?????? ?????. ? ??? ?? ?? ??? ??????? ??? ?????.
AJAX ???? ??? ??? ?? ?????? ???? ????, Ajax ??? ?? ???? ??? ???? ????. ?? ??? ???????? ???? ?? ? ???? ??? ??? ?????. ? ??? ?? ?? ??? ?? ?????? ? ??? ??? ?? ??? ???? ???.
2. ThinkPHP?? ???? ???? ??
????? ThinkPHP?? ???? ???? ???? ?? ?? ??? ? ??? ????. Model ????? ThinkPHP? Query ???? Db ???? ???? ??????? ???? ?????. View ????? ThinkPHP? ??? ??? Pagination? ??? Paginator ???? ???? ??? ?? ??? ?????.
- ?? ???? ??? ?? ?? ??
?? ????? ?? ???????? ???? ??? ?? ??? ??? ?? ???? ?? ???? ?? ??? ???? ???. ??? ??? ???? ?????. ???? ??? ??? ????.
<?php
namespace Home\Model;
use Think\Model;
class UserModel extends Model{
public function getPageUsers($page=1,$rows=10){
$result = array();
$count = $this->count();?//?獲取總記錄數(shù)
????????$offset?=?($page-1)*$rows;?//?查詢的起始位置
????????$data?=?$this->limit($offset,$rows)->select();?//?查詢當前頁的記錄
????????$pagination?=?new?\Think\Paginator($count,$rows);?//?實例化分頁對象
????????$result['rows']?=?$data;
????????$result['pagination']?=?$pagination->show();?//?獲取分頁顯示的HTML代碼
????????return?$result;
????}
}
? ????? count() ???? ???? ?? ??? ?? ????,limit() ???? ???? ?? ???? ???? ????, Pagination ???? ?????. ??? ??? ??????? ? ?????. Limit() ???? ?? ??? ???? where() ???? ?? ???? ? ??? ?? ??? ??? ? ??? ?? ??? ??? ????.
- View ???? ??? ?? ?? ??
View ????? ThinkPHP? ??? Paginator ???? ???? ??? ??? ???? ThinkPHP? ??? ??? Pagination? ???? ??? HTML ??? ???? ???. ???? ??? ??? ????.
<!DOCTYPE html>
<html>
<head>
????<meta charset="UTF-8">
????<title>用戶信息管理</title>
????<link rel="stylesheet" type="text/css" href="__PUBLIC__/bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
????<div class="panel panel-default">
????????<div class="panel-heading">用戶信息管理</div>
????????<div class="panel-body">
????????????<table class="table table-striped">
????????????????<thead>
????????????????<tr>
????????????????????<th>ID</th>
????????????????????<th>用戶名</th>
????????????????????<th>年齡</th>
????????????????????<th>電話</th>
????????????????????<th>郵箱</th>
????????????????</tr>
????????????????</thead>
????????????????<tbody>
????????????????<?php foreach($users['rows'] as $user):?>
????????????????<tr>
????????????????????<td><?php echo $user['id'];?></td>
????????????????????<td><?php echo $user['name'];?></td>
????????????????????<td><?php echo $user['age'];?></td>
????????????????????<td><?php echo $user['phone'];?></td>
????????????????????<td><?php echo $user['email'];?></td>
????????????????</tr>
????????????????<?php endforeach;?>
????????????????</tbody>
????????????</table>
????????????<nav aria-label="Page navigation">
????????????????<?php echo $users['pagination'];?>
????????????</nav>
????????</div>
????</div>
</div>
</body>
</html>
? ????? ?? Paginator ???? Pagination ???? ?????. Paginator ?????? ???? ????, ??? ?? ??? ? ????. Pagination ?????? Paginator ????? ??? ??? ?? HTML ??? ?? ?? show() ???? ??????. ??? foreach ????? ?? ???? ???? ??? $rows ??? ?????.
3. ThinkPHP ???? ?? ?? ?? ??
- ? ???? ???? ??? ?? ???? ??? ??????
?? ???? getPageUsers ????? ??? ?? ???? ?? ??? ?? ??? ? ????.
public?function?getPageUsers($page=1,$rows=10){
????...
}
? ? $rows? ???? ?? ??? ?? ?????. ????? ?? ??? ?? ????. ???? ??? ?? ????? ?? ?????
- ? ???? ??? ??????
View ????? ???? ? ?? ???? ???? $page ????? ?? 1??? ?? ?? ???? ?? ??? ???? ???. ???? ??? ??? ????.
public?function?getPageUsers($page=1,$rows=10){
????$result?=?array();
????$count?=?$this->count();?//?獲取總記錄數(shù)
????$offset?=?($page-1)*$rows;?//?查詢的起始位置
????$data?=?$this->limit($offset,$rows)->select();?//?查詢當前頁的記錄
????
????//?如果是第一頁,直接返回第一頁的數(shù)據(jù)
????if($page?==?1){
????????$pagination?=?new?\Think\Paginator($count,$rows);
????????$result['rows']?=?$data;
????????$result['pagination']?=?$pagination->show();
????????return?$result;
????}
????
????//?如果不是第一頁,則查詢第一頁的數(shù)據(jù),獲取分頁HTML代碼
????$firstPageData?=?$this->limit(0,$rows)->select();
????$pagination?=?new?\Think\Paginator($count,$rows);
????$result['rows']?=?$data;
????$result['pagination']?=?str_replace("1</a>",$firstPageData."</a>",$pagination->show());
????return?$result;
}
? ???? $page? 1?? ? ?? ???? ???? ?? ?????. $page? 1?? ?? ? ?? ??? ???? ?? ???? ??? HTML ??? ?? ?? ?? ?? ???? "? ?? ???"? ????. ?? ???? ?? ?????? "? ?? ???" ??? ???? ???? ? ?? ???? ??? ? ????.
- AJAX ??? ??? ???? ??? ??????
?? ???? ?? ???? ?? ??? ?? ???? ?? ???? ??? ?? ??? ??? ???? ??? ??????? ?? ?? ?? ???? ???? ?? AJAX ??? ?? ?????.
AJAX ??? ?? ? ?? ??? ???? ???.
(1) View ????? ??? ??? ??????? ?? ??? jQuery ??? ???? ???.
$(function?()?{?//?加載頁面時,注冊分頁事件
????$("#page").on('click','a',function(){
????????var?url?=?$(this).attr('href');
????????$("#table").load(url);
????????return?false;
????});
});
? ????, ??? ??? ?????. ?? ??? ???? ???? ??? ??? ???? AJAX ??? ???? ?? ??? ???? ?? ????.
(2) ?? ???? JSON ??? ???? ??? ?? getPageUsers ????? ????? ???.
public?function?getPageUsers($page=1,$rows=10){
????...
????$result?=?array();
????$pagination?=?new?\Think\Paginator($count,$rows);
????$result['rows']?=?$data;
????$result['pagination']?=?$pagination->show();
????return?json_encode($result);
}
????? json_encode() ??? ???? ??? ??? ???? JSON ???? ?????. ??? ???? ? ???? ???? ?? ?? ??? ? ????.
? ??? thinkphp?? ???? ???? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!