Đối với các bạn làm việc với Zend Framework có thể tự code paging hoặc có thể dùng Zend Paging của Zend Framework, bài viết sau đây hướng dẫn từng bước các bạn phân trang với Zend Framework
Lời khuyên: Bạn nên học PHP cơ bản trước khi làm quen với Zend Framework.
Bước 1: Chúng ta thêm vào Controller đoạn code say đây để lấy ra danh sách record được lấy từ câu lệnh: $record->listItemabout()
1
2
3
4
5
6
7
8
9
10
11
12
|
$listArticles = $record–>listItemabout();
$currentPage = 1;
$i = $this–>_getParam(‘page’,1);
if(!empty($i))
{
$currentPage = $i;
}
$paginator = Zend_Paginator::factory($listArticles);
$paginator–>setItemCountPerPage(5)
–>setPageRange(5)
–>setCurrentPageNumber($currentPage);
$this–>view–>paginator = $paginator;
|
Bước 2: Trong view bạn thêm vào code phân trang như sau:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php
$k = 1;
foreach($this–>paginator as $value){
$rowclass = ($k%2==0) ? ” : ‘ class=”alt”‘;
echo ‘<tr’.$rowclass.‘>
<td><input type=”checkbox” name=”ids[]” value=”” /></td>
<td><b>’.$value[‘title’.LANG].‘</b></td>
<td>’.date(‘d-m-Y’, strtotime($value[‘posted_date’])).‘</td>
<td>’.$value[‘hits’].‘</td>
<td><a href=”edit/job” title=”Chinh sua”>Edit</a></td>
</tr>’;
$k++;
}
?>
<?php echo $this–>paginationControl($this–>paginator, ‘Sliding’, ‘paging.phtml’); ?>
|
Bước 3: Một file paging.phtml đặt trong view/scripts với nội dung:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
<div class=“pagination” style=“width:100%”>
<div style=“float:center;width:28%”>
</div>
<div style=“float:center;width:70%;”>
<!— First page link —>
<?php if (isset($this–>previous)): ?>
<a href=“<?php echo $this->url(array(‘page’ => $this->first)); ?>”>Start</a>
<?php else: ?>
<span class=“”><a href=“#”>start</a></span>
<?php endif; ?>
<!— Previous page link —>
<?php if (isset($this–>previous)): ?>
<a href=“<?php echo $this->url(array(‘page’ => $this->previous)); ?>”>&lt; Previous</a>
<?php else: ?>
<span class=“”><a href=“#”> Previous </a></span>
<?php endif; ?>
<!— Numbered page links —>
<?php foreach ($this–>pagesInRange as $page): ?>
<?php if ($page != $this–>current): ?>
<a href=“<?php echo $this->url(array(‘page’ => $page)); ?>”><?php echo $page; ?></a>
<?php else: ?>
<div class = “current”><a href=“#”><?php echo $page; ?></a></div>
<?php endif; ?>
<?php endforeach; ?>
<!— Next page link —>
<?php if (isset($this–>next)): ?>
<a href=“<?php echo $this->url(array(‘page’ => $this->next)); ?>”>Next &gt;</a>
<?php else: ?>
<span class=“”><a href=“#”>Next</a></span>
<?php endif; ?>
<!— Last page link —>
<?php if (isset($this–>next)): ?>
<a href=“<?php echo $this->url(array(‘page’ => $this->last)); ?>”>End</a>
<?php else: ?>
<span class=“”><a href=“#”>End</a></span>
<?php endif; ?>
Page <?php echo $this–>current; ?> of <?php echo $this–>last; ?>
</div>
</div>
|
Bài viết hướng dẫn các bạn làm phân trang với Framework zend sử dụng Zend_Paginator_Adapter_Null. Zend_paginator_adapter_null để làm ra mấy cái nút chuyển trang thôi trang thôi chứ chưa có dữ liệu, còn dữ liệu thì chỉ truy xuất kết quả của trang hiện hành nên sẽ ko phải truy xuất tất cả dữ liệu nữa. Đại loại là bạn cần 2 function trong thư mục models của module đó: 1. là đếm tổng số mẫu tin (COUNT(item_id) AS total). 2. Truy vấn mẫu tin ứng với từng trang LiMIT(0, 1234) gì đó.
Bước 1: Tại Controller của module đó bạn làm như sau:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<?php
class Dashboard_ListPostsController extends GLT_Controller_Action {
private $_data;
private $_paginator;
public function init(){
$this–>_data = $this–>_request–>getParams();
//paging
$this–>_paginator[‘itemCountPerPage’] = 15; //so mau tin can phan trang
$this–>_paginator[‘pageRange’] = 5; //cai nay ko hieu cung ko sao, cu tu tu
$this–>_paginator[‘currentPage’] = $this–>_request–>getParam(‘page’,1);
$this–>_data[‘paginator’] = $this–>_paginator;
}
public function indexAction() {
$item = new Posts_Model_Posts();
$this–>view–>list = $item–>getListItemUser($this–>_data); //Trong day la truy van co limit trong nay
//paging
$totalItem = $item–>countItemUser($this–>_username); //Dem tong so mau tin o day, vidu: 100 mau tin
$paginator = new GLT_Paginator();
$this–>view–>paginator = $paginator–>createPaginator($totalItem, $this–>_paginator);
}
}
|
Bước 2: Trong thư mục models của Module đó bạn làm 2 function getListItemUser và countItemUser
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
//Back end – Danh sách mẫu tin user
public function getListItemUser($user, $data = NULL){
//get paging number
$paginator = $data[‘paginator’];
if ($paginator[‘itemCountPerPage’]>0){
$page = $paginator[‘currentPage’];
$rowCount = $paginator[‘itemCountPerPage’];
}
$db = Zend_Registry::get(‘connectDb’);
$select = $db–>select()–>from($this–>_name, array(‘post_id’, ‘post_title’, ‘post_sort’, ‘post_hit’, ‘post_enable’))
–>where(‘glt_usernames_username =?’, $user)
–>limitPage($page,$rowCount);
$result = $db–>fetchAll($select);
if(count($result) == 0){
$result = ‘No record your post!’;
}
return $result;
}
//Back end – Đếm tổng số mẫu tin của User
public function countItemUser($user){
$db = $this–>getAdapter();
$select = $db–>select()–>from($this–>_name, array(‘count(post_id) as totalItem’))
–>where(‘glt_usernames_username =?’, $user);
return $db–>fetchOne($select);
}
|
Bước 3: Trong views của modules đó bạn chèn thêm:
1
|
<?php echo $this–>paginationControl($this–>paginator, ‘Sliding’, ‘pagination.phtml’) ?>
|
Bước 4: Bạn tạo file pagination.phtml trong thư mục scripts cũa view đó như sau:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?php
$strPage = ”;
foreach ( $this–>pagesInRange as $key ) {
if ($this–>current == $key) {
$strPage .= ‘<b class=”pagecureent”>’ . $key . ‘</b> ‘;
} else {
$strPage .= ‘<a class=”pagenav” href=”‘.$this–>url(array(‘page’ => $key)).‘”>’ . $key . ‘</a> ‘;
}
}
//$page = ‘Page ‘ . $this->current . ‘ Of ‘ . $this->last;
?>
<?php
if (isset($this–>previous)){
echo ‘<a class=”pagenav” href=”‘.$this–>url(array(‘page’ => $this–>first)).‘”>Start</a> ‘;
}
if (isset($this–>previous)){
echo ‘<a class=”pagenav” href=”‘.$this–>url(array(‘page’ => $this–>previous)).‘”>Previous</a> ‘;
}
?>
<?php echo $strPage;?>
<?php
if (isset($this–>next)){
echo ‘<a class=”pageactive” href=”‘.$this–>url(array(‘page’ => $this–>next)).‘”>Next</a> ‘;
}
if (isset($this–>next)){
echo ‘<a class=”pagenav” href=”‘.$this–>url(array(‘page’ => $this–>last)).‘”>End</a>’;
}
?>
|
Nếu dữ liệu của bạn ok là sẽ ra phân trang rồi đó, vấn đề muốn đẹp thì thêm CSS vào, chèn code này vào file CSS của web bạn thì sẽ nhìn đẹp gái hẳn ra hiện Grouplaptrinh của mình đang đang làm như vậy, chia sẻ cùng các bạn
1
2
3
4
|
/* paging */
.pagenav , .pageactive{border: 1px solid #ccc; padding: 2px 5px; color: #036CB4; -moz-border-radius: 2px; -webkit-border-radius: 2px;}
.pagenav:hover{background: #0066a7}
.pagecureent{border: 1px solid #ccc; padding: 2px 5px; background:#036CB4; color:#fff; font-weight:bold; -moz-border-radius: 2px; -webkit-border-radius: 2px}
|
Còn muốn css đẹp là lạ hơn nữa thì search với từ khóa “100CSS3PaginationStyles” sẽ thấy