2011-04-26 8 views
0

CakePhp 사이트에 문제가 있습니다.CakePHP 검색 및 페이지 매김 관련 문제

내 색인이 페이지 매김에 문제가 없지만 페이지를 검색 할 때 페이지 매김이 사라지고 검색 결과가 표시되지 않습니다 (대신 색인이 페이징없이 표시됨).

어디에서 코드가 잘못 되었습니까?

class ItemsController extends AppController { 
var $name = 'Items'; 


// load any helpers used in the views 
var $helpers = array('Paginator', 'Html', 'Form', 'Javascript', 'Misc', 'Time', 'Tagcloud'); 
var $components = array('RequestHandler'); 

/** 
* index() 
* main index page for items 
* url: /items/index 
*/ 
function index() { 
    // get all options for form 
    $tags = $this->Item->Tag->find('list', array(
     'fields'=>'id, name', 
     'order'=>'Tag.name', 
     'conditions'=> array(
      'Tag.status'=>'1' 
     ) 
    )); 

    // add name to option 
    $tags = array(''=>'Tags') + $tags; 

    //pr($tags); 

    // if form submitted 
    if (!empty($this->data)) { 
     // if reset button pressed redirect to index page 
     if(isset($this->data['reset'])) { 
      $this->redirect(array('action'=>'index')); 
     } 

     // init 
     $url = ''; 

     // remove search key if not set 
     if($this->data['search'] == '') { 
      unset($this->data['search']); 
     } 

     // loop through filters 
     foreach($this->data as $key=>$filter) { 
      // ignore submit button 
      if($key != 'filter') { 
       // init 
       $selected = ''; 

       switch($key) { 
        case 'tag': 
         $selected = $tags[$filter]; 
        break; 
        case 'search': 
         $selected = $filter; 
        break; 
       } 
       // if filter value is not empty 
       if(!empty($filter)) { 
        $selected = $this->slug($selected); 
        $url .= "/$key/$selected"; 
       } 
      } 
     } 

     // redirect 
     $this->redirect('/items/index/'.$url); 
    } else { 
     // set form options 
     $this->data['tag'] = ''; 
     $this->data['search'] = ''; 
    } 

    // if any parameters have been passed 
    if(!empty($this->params['pass'])) { 
     // only select active items 
     $conditions = array('Item.status'=>1); 

     // get params 
     $params = $this->params['pass']; 
     // loop 
     foreach($params as $key=>$param) { 
      // get the filter value 
      if(isset($params[$key+1])) { 
       $value = $params[$key+1]; 
      } 

      // switch on param 
      switch($param) 
      { 
       case 'tag': 
        // get tag 
        $tag = $this->Item->Tag->find('first', array(
         'recursive' => 0, 
         'conditions' => array(
          'Tag.slug'=>$value 
         ) 
        )); 
        // save value for form 
        $this->data['tag'] = $tag['Tag']['id']; 
       break; 
       case 'search': 
        // setup like clause 
        $conditions['Item.name LIKE'] = "%{$value}%"; 
        // save search string for form 
        $this->data['search'] = str_replace('_', ' ', $value); 
       break; 
      } 
     } 

     //pr($conditions); 

     // get all items with param conditions 
     $items = $this->Item->find('all', array(
      'order' => 'Item.name', 
      'conditions' => $conditions 
     )); 

     // if tag filter has been set 
     if(isset($tag)) { 
      // loop through items 
      foreach($items as $key=>$item) { 
       // init 
       $found = FALSE; 
       // loop through tags 
       foreach($item['Tag'] as $k=>$g) { 
        // if the tag id matches the filter tag no need to continue 
        if($g['id'] == $tag['Tag']['id']) { 
         $found = TRUE; 
         break; 
        } 
       } 

       // if the tag was not found in items 
       if(!$found) { 
        // remove from list 
        unset($items[$key]); 
       } 
      } 
     } 

    } else { 
     // get all items from database where status = 1, order by name 
     $items = $this->Item->find('all', array(
      'order' => 'Item.name', 
      'conditions' => array(
       'Item.status'=>1 
      ) 
     )); 
    } 

    $this->paginate = array( 
    'limit' => 10 
    );  

    $data = $this->paginate('Item');  


    // set page title 
    $this->pageTitle = 'Index Page'; 
    // set layout file 
    $this->layout = 'index'; 
    // save the items in a variable for the view 

    $this->set(compact('data', 'tags', 'items')); 

} 

답변

0

검색 조건을 페이지 매김 기능에 전달하려고합니다. 컨트롤러의 paginate 속성을 통해이 작업을 수행 할 수 있습니다.

function index() { 
    ... 
    switch($param) { 
     ... 
     case 'search': 
      $this->paginate['conditions']['Item.name LIKE'] = "%{$value}%"; 

페이지 매김 설정에 대한 자세한 내용은 here입니다.

+0

아니요, 작동하지 않습니다 ... – grosseskino

관련 문제