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

 
Hướng dẫn học ngôn ngữ Swift của Apple trong 24h thôi

Hướng dẫn học ngôn ngữ Swift của Apple trong 24h thôi

Khi Apple công bố ngôn ngữ lập trình hoàn toàn mới có tên là Swift của họ, cộng đồng lập trình đều rất vui mừng. 

Hướng dẫn nhỏ Updating to Drupal 8.5 with composer

Hướng dẫn nhỏ Updating to Drupal 8.5 with composer

If you are like me, you might have already started planning the upgrade to Drupal 8.5, now that the first release candidate is out. It's awesome by the way, among other things, thanks to the incredible work done with layout builder.

Performance and SEO impact of not rewriting your urls to use or not use www

Performance and SEO impact of not rewriting your urls to use or not use www

Here's a quick tip which will influence your performance and SEO. It's just about uncommenting 2 lines of code in the .htaccess file that ships with Drupal, but I've seen a lot of sites that tend to forget this.

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

 

Diet con trung