當列出尋找的內容有很多條的時候我們可以將資料進行分頁顯示。
user表的結構
現在要將使用者以列表的形式顯示,顯然不可能將查詢結果顯示在一頁當中,此時要將結果分頁顯示,首先將分頁控制項page.php複製到項目的control檔案中,然後我們可以在控制器中輸入如下代碼:
01
function actionCusList()
02
{
03
$cus_info = User::find();
04
//擷取當前是第幾頁
05
$page = intval( $this->_context->page );
06
$page<1?1:$page;
07
//設定每頁顯示的數量
08
$page_size = 10;
09
10
//按條件尋找使用者
11
if($id = $this->_context->get('cus_id'))
12
$cus_info->where('id = ?', $id);
13
else
14
{
15
if($first_name = $this->_context->get('first_name'))
16
$cus_info->where('first_name = ?', $first_name);
17
18
if($last_name = $this->_context->get('last_name'))
19
$cus_info->where('last_name = ?', $last_name);
20
21
if($email = $this->_context->get('email'))
22
$cus_info->where('email = ?', $email);
23
24
if($code = $this->_context->get('code'))
25
$cus_info->where('pro_id = ?', Program::find('code = ?', $code)->getOne()->id);
26
}
27
28
$cus_info->limitPage($page, $page_size);
29
$cus = $cus_info->getAll();
30
31
//渲染視圖
32
$this->_view['url_args'] = $this->_context->get();
33
$this->_view['pagination'] = $cus_info->getPagination();
34
$this->_view['cus'] = $cus;
35
}
此代碼中有一處是按條件尋找使用者,我們可以進行前台設計一個表單讓使用者輸入特定條件來尋找使用者,代碼如下:
01
02
Search >
03
04
05
06
07
close
08
34
35
36
37
_control('page', 'p', array('pagination' => $pagination, 'url_args' => $url_args));?>
38
39
40
41
42
| Cust. ID | 43
First Name | 44
Last Name | 45
Email Address | 46
Program | 47
Last Login | 48
Action | 49
50
51 52
53
54
| 55
| 56
| 57
| 58
| 59
| 60
61
$cus['id']))?>" title="View">View
62
| 63
64
65 66
67
68
_control('page', 'p', array('pagination' => $pagination, 'url_args' => $url_args));?>
69
分頁關鍵代碼為:
1
_control('page', 'p', array('pagination' => $pagination, 'url_args' => $url_args));?>
顯示結果
作者:frylan
http://www.bkjia.com/PHPjc/478118.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478118.htmlTechArticle當列出尋找的內容有很多條的時候我們可以將資料進行分頁顯示。 user表的結構 現在要將使用者以列表的形式顯示,顯然不可能將查詢...