2016-10-21 16 views
1

これはすべての検索結果がforeachループから来ているコントローラファイルです。どのようにそれを行うかを理解する。CakePHP 3.xを使用して検索結果をページングし、テンプレートの表示ファイルに表示しようとしています

<?php 
class SerController extends AppController 
{ 
    public function initialize() 
    { 
     parent::initialize(); 
     $this->loadComponent('Paginator'); 
    } 

    public function index() 
    { 
     $check_string=$this->request->data('check'); 
     $check_break=explode(" ",$check_string); 
     $s1=TableRegistry::get('ser_tab'); 
     foreach($check_break as $check) 
     { 
      $s11=$s1->find('all')->where(['OR' =>['text LIKE' => '%'.$check.'%','name LIKE' => '%'.$check.'%']]); 
      $this->set(array('data' => $s11)); 
     }     
     $this->render('index'); 
    } 
} 
+0

DualController.php

<?php public function initialize() { parent::initialize(); $this->loadComponent('Paginator'); } 

パブリック関数指数(){

$r = TableRegistry::get('d11'); $results = $r->find('all'); $this->set(array('data11' => $results)); $this->Paginator->paginate($results,$settings); $this->set('index'); 

} **全文検索**のhttp://www.hackingwithphp .com/9/3/18/advanced-text-searching-full-text-indexesを使用してください – tarikul05

+0

ページネーションを使用して見てくださいhttp://stackoverflow.com/a/6184178/3278789 – tarikul05

答えて

0

あなたのクエリは実行に時間がかかります。あなたはそれを簡単に行うことができます。 は

例[URLからのデータ]

public function index($data='Default text here'){ /*When get Data from the URL*/ 
    $data = explode(' ', $data); 
    $conditions = array(); /*All OR conditions will store here*/ 

    foreach ($data as $key => $value) { 
     $conditions['or'][] = array('text LIKE' => "%$value%"); 
     $conditions['or'][] = array('name LIKE' => "%$value%"); 
    } 

    $this->paginate = [ 
     'limit' => 25, #Set the limit of post 
     'conditions'=>[ 
      $conditions /*OR conditions array*/ 
     ] 
    ]; 

    $data = $this->paginate(); 
    var_dump($conditions,$data);   /*Debug OR Condition array and Query data*/ 
    $this->set(compact('data'));   /*Set the Posts to a variable*/ 
} 

例[POSTからのデータ]

public function index(){ 
    if ($this->request->is('post')) { 
     $data = $this->request->data('check'); 
     # YOUR CODE HERE 
    } 
} 

のCakePHPの3.xページネーションのドキュメント以下のようにのようなあなたのindex()機能を更新しますis HERE

+0

または、 [検索](https://github.com/FriendsOfCake/search)プラグインのために調整しました;)コントローラーのスコープに適切なコードを入れずに解決しました。 – mark

+0

は、コントローラとビューを使用するだけで、モデルなしでページネーションを追加することは可能ですか? –

+0

はい! Paginatorコンポーネントを直接使用することができます。 '$ this-> Paginator-> paginate(YOUR_QUERY_OBJECT、CONFIGS)のように'ここに詳細があります(http://book.cakephp.org/3.0/en/controllers/components/pagination.html#using-the-paginator-directly )@HarjeevSingh –

1

私はデータセットの結果の値をページングしようとしていますが、これまでコントローラ内のすべてのコードを書きましたが、エラーは発生していませんでしたが、私の質問はどのようにVIEWの値を取得しますか? MySQLを使用すること

関連する問題