Hướng dẫn học: Phân trang dữ liệu với CakePHP

Hướng dẫn học: Phân trang dữ liệu với CakePHP

Cũng như bao framework khác như Zend, CodeIgniter … CakePHP cũng hỗ trợ các phương thức phân trang cho dữ liệu.

1.Chuẩn bị CSDL :

CREATE TABLE `books` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `isbn` varchar(13) NOT NULL,
  `title` varchar(64) NOT NULL,
  `description` text NOT NULL,
  PRIMARY KEY (`id`)
) 

INSERT INTO `books` VALUES ('1', 'abcxyz', 'Kĩ thuật lập trình', 'Kĩ thuật lập trình');
INSERT INTO `books` VALUES ('2', 'c,c++', 'Lập trình C', 'Lap trinh C can ban');
INSERT INTO `books` VALUES ('3', 'C++', 'Cấu trúc dữ liệu và giả thuật', 'Cau truc du lieu va giai thuat');
INSERT INTO `books` VALUES ('4', 'php', 'PHP căn bản', 'php, lap trinh php');
INSERT INTO `books` VALUES ('5', 'php nc', 'PHP Nâng cao', 'php,php nang cao');
INSERT INTO `books` VALUES ('6', 'php fw', 'PHP Framework', 'php, php framework');
INSERT INTO `books` VALUES ('22', 'abc', 'Lập trình ứng dụng web tập I', 'Lập trình ứng dụng web tập I');
INSERT INTO `books` VALUES ('24', 'abc', 'Lập trình ứng dụng web tập II', 'abc');
INSERT INTO `books` VALUES ('26', 'abc', 'Lập trình web với CakePHP', 'abc');
INSERT INTO `books` VALUES ('27', 'acb', 'Lập trình web với CodeIgniter', 'abc');
INSERT INTO `books` VALUES ('28', 'abc', 'Tutorial covered by Thái Thanh Phong ^0^', 'abc');

Tạo Controller Books (app/controllers/books/books_controller.php):

<?php
class BooksController extends  AppController{
    var $name = "Books";// ten cua Controller Book
    var $helpers = array('Paginator','Html');
    var $components = array('Session');
    var $paginate = array();
    
    //------- Paging Normal 
    function paging(){
        $this->paginate = array(
                                'limit' => 4,
                                'order' => array('title' => 'desc'),
                             );
        $data = $this->paginate("Book");
        $this->set("data",$data);
    }

Ghi chú :

- Để sử dụng chức năng phân trang của cakePHP thì trong Controller cần có thành phần helper Paginator :

var $helpers = array('Paginator','Html'); 

và namespace paginate :

var $paginate = array(); 

- Ở đây tôi lấy tất dữ liệu bắt đầu với 4 record đầu tiên vào theo thứ tự giảm dần theo “title”

$this->paginate = array(
                                'limit' => 4,
                                'order' => array('title' => 'desc'),
                             ); 

- Nếu chỉ lấy những 1 số field cần thiết :

$this->paginate = array(
‘field’ =>array(“title”,”info”),
                                'limit' => 4,
                                'order' => array('title' => 'desc'),
                             ); 

- Để lấy dữ liệu theo cấu hình của biến namespace paginate ta dùng hàm $this->paginate(‘ten_model’) , trong ví dụ này model của tôi có tên là Book

$data = $this->paginate("Book"); 

- Việc cấu hình biến namspace paginate dùng hàm $this->paginate(‘ten_model’)
Giống như việc cấu hình điều kiện truy xuất dữ liệu và lấy dữ liệu bằng hàm $this->find(‘…’); . Xem lại hàm truy vấn CSDL
- Chúng ta chỉ cần cấu hình như vậy là có thể phân trang cho CSDL , mặc định cakePHP dùng biến page để xác định trang hiện tại
Việc cấu hình có vẻ đơn giản hơn nhiều so với CodeIgniter phải không !

Tạo Model Book (app/models/book.php) :

<?php
class Book extends AppModel{
    var $name = "Book"; // Ten cua Model Book
}

Tạo view cho function Paging của Controller Books (app/views/books/paging.ctp):

<html>
<body>
<?php
    echo $this->Paginator->prev('« Previous ', null, null, array('class' => 'disabled')); //Shows the next and previous links
    echo " | ".$this->Paginator->numbers()." | "; //Shows the page numbers
    echo $this->Paginator->next(' Next »', null, null, array('class' => 'disabled')); //Shows the next and previous links
    echo " Page ".$this->Paginator->counter(); // prints X of Y, where X is current page and Y is number of pages
?> 

<?php
if($data==NULL){
    echo "<h2>Dada Empty</h2>";
}
else{
    echo "<table>
          <tr>
            <td>id</td>
            <td>Title</td>
          </tr>";
    foreach($data as $item){
        echo "<tr>";
        echo "<td>".$item['Book']['id']."</td>";
        echo "<td><a href='".$this->webroot."books/view/".$item['Book']['id']."' >".$item['Book']['title']."</a></td>";
        echo "</tr>";
    } 
}
?>
</body>
</html>

Chạy thử : http://localhost/cakephp/books/paging

Hướng dẫn học:  Phân trang dữ liệu với CakePHP

Tags: 
File: 
Bạn thấy bài viết này như thế nào?: 
Average: 4.1 (7 votes)
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
70 Best Free Google Android Apps(P1)

Những ứng dụng chat từ android

Some months back we mentioned about 21 Best Google Android Applications, now today we present you 70 Free Google Android apps, which are divided in categories Like, Tools, Social, Entertainment, Travel and more.

Những từ khóa làm đẹp được người Châu Á tìm kiếm nhiều nhất

Những từ khóa làm đẹp được người Châu Á tìm kiếm nhiều nhất

Với những tiến bộ về mặt công nghệ, giải phẩu thẫm mỹ đã trở nên phổ biến và không còn là một dịch vụ xa xỉ như trước đây

HTC Ville siêu mỏng chạy Android 4.0 Ice Cream Sandwich

HTC Ville siêu mỏng chạy Android 4.0 Ice Cream Sandwich

Hình ảnh và thông tin mới nhất trên trang PocketNow cho thấy HTC Ville sử dụng màn hình Super AMOLED 4,3 inch với độ phân giải qHD

Công ty diệt chuột T&C

 

Diet con trung