Hướng dẫn học : Tìm Kiếm và phân trang dữ liệu với CakePHP

Hướng dẫn học : Tìm Kiếm và phân trang dữ liệu với CakePHP

Trong các bài viết trước tôi có trình bày về các kiểu truy vấn CSDL và phân trang dữ liệu . Trong bài viết này , tôi sẽ hướng dẫn các bạn cách tìm kiếm và phân trang dựa trên dữ liệu vừa tìm được .

1.Tạo 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`)
);

-- ----------------------------
-- Records
-- ----------------------------
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');
INSERT INTO `books` VALUES ('30', '', 'Kĩ thuật lập trình', '');

2.Viết ứng dụng :

Vào app/controllers/ tạo Book Controller : books_controller.php

class BooksController extends  AppController{

    var $name = "Books";// ten cua Controller Book
    var $helpers = array('Form','Paginator','Html','Javascript','Common');
    var $components = array('Session','RequestHandler','Common');
    var $paginate = array();
    
    function search() {
        // the page we will redirect to
        $url['action'] = 'result';
        // build a URL will all the search elements in it
        // the resulting URL will be 
        // example.com/cake/posts/index/Search.keywords:mykeyword/Search.tag_id:3
        foreach ($this->data as $k=>$v){ 
            foreach ($v as $kk=>$vv){ 
                $url[$k.'.'.$kk]=$vv; 
            } 
        }
        // redirect the user to the url
        $this->redirect($url, null, true);
    }

      function result(){
        $conditions = array();
        $data = array();
        if(!empty($this->passedArgs)){
            //Fillter title
            if(isset($this->passedArgs['Book.title'])) {
                $title = $this->passedArgs['Book.title'];
                $conditions[] = array(
                    'Book.title LIKE' => "%$title%",
                );
                $data['Book']['title'] = $title;
            }
            //Fillter description
            if(isset($this->passedArgs['Book.description'])) {
                $keywords = $this->passedArgs['Book.description'];
                $conditions[] = array(
                    "OR" => array(
                                'Book.description LIKE' => "%$keywords%",
                                'Book.isbn LIKE' => "%$keywords%" 
                            )
                );
                $data['Book']['description'] = $keywords; 
            }
            //Limit and Order By
            $this->paginate= array(
                'limit' => 4,
                'order' => array('title' => 'desc'),
            );
            
            $this->data = $data;
            $this->set("posts",$this->paginate("Book",$conditions));
        }
    }
} 

Ghi chú :

$this->data = $data; 

Giữ lại giá trị tìm kiếm trên form tìm kiếm.

Trong Book Controller có 2 hàm chính là : search() và result()
- hàm result() : vụ hiển thị form search và hiển thị phân trang dữ liệu
- hàm search() : nhận request từ form và chuyển dữ liệu lại cho result() .

Vào app/views/books/ tạo file result.ctp :

<?php 

    $this->Paginator->options(array('url' => $this->passedArgs));
?>
<?php echo $this->Form->create('Book',array('action'=>'search'));?>
    <fieldset>
         <legend><?php __('Book Search');?></legend>
    <?php
        echo $this->Form->input('title');
        echo $this->Form->input('description');
        echo $this->Form->submit('Search');
    ?>
    </fieldset>
<?php echo $this->Form->end();?>

<?php
if(!empty($posts)){
    echo "<table>";
    echo "<tr>";
    echo "<th>".$this->Paginator->sort("Id","id");
    echo "<th>".$this->Paginator->sort("Title","title");
    echo "<th>".$this->Paginator->sort("Description","description");
    echo "</tr>";
    
    foreach($posts as $item){
        echo "<tr>";
        echo "<td>".$item['Book']['id']."</td>";
        echo "<td>".$item['Book']['title']."</td>";
        echo "<td>".$item['Book']['description']."</td>";
        echo "</tr>";
    }
    echo "</table>";
    
    //---- Paging 
    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
}
?>

Trong phần file view này có vào hàm kha hay đó là :

    echo "<th>".$this->Paginator->sort("Id","id");

    echo "<th>".$this->Paginator->sort("Title","title");
    echo "<th>".$this->Paginator->sort("Description","description"); 

   $this->Paginator->sort("tên hiểu thị","tên_field") 

Hàm này có chức năng hiển thị tên filed thông qua 1 dòng text nào đó , khi bấm vào thì danh sách dữ liệu sẽ tự sắp sếp tăng dần, bấm lần nữa thì tự giảm dần . Nó giống dạng sort table.

3.Chạy thử ví dụ :

http://localhost/cakephp/books/search nó tự chuyển sang http://localhost/cakephp/books/result

Kết quả tìm kiếm với từ khóa : php và abc được 1 kết quả

Search thử với từ khóa "a" để có dữ liệu nhiều hơn cho việc phân trang :

Bài viết tham khảo tại :

- http://mrphp.com.au/code/search-forms-cakephp
- http://book.cakephp.org/view/1030/Co...ind-Conditions

Source code demo gồm các file sau :

  • Controllers/books_controller.php
  • Views/result.ctp
  • Models/book.php
  • books.sql
Tags: 
Bạn thấy bài viết này như thế nào?: 
No votes yet
Ả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

 
Integrated Device Technology, Inc. - IDT.com

Thiết kế Drupal site: www.idt.com

IDT required a solution that would prove to be fast, have an extremely easy-to-use interface, scale to thousands of products and offer the flexibility to grow and change quickly to keep pace with the company's markets

Amazon Kindle Fire được cài đặt sẵn Facebook, Twitter

Amazon Kindle Fire được cài đặt sẵn Facebook, Twitter

Các ứng dụng được cài đặt sẵn sẽ bao gồm: Facebook, Netflix, Rhapsody, Pandora, Twitter, Comics by comiXology, các trò chơi từ các nhà phát triển nổi tiếng như Zynga, EA, Gameloft, PopCap và Rovio.

Running Drupal in Kubernetes with Docker in production

Running Drupal trong Kubernetes với Docker in production

You might think that Drupal and Docker work together nicely. They definitely can and do, in many cases, as we see with local development environments built around Docker

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

 

Diet con trung