Your Location is: Home > Cakephp

CakePHP Ajax search : Showing same data two times for pagination

From: China View: 3209 Alimon Karim 

Question

Here ajax search working fine but the problem is same data is showing two times, because I have given pagination limit 2.If I make it 3, same result is showing 3 times.

Here is my index.ctp

$( "#search" ).keyup(function() {

    var value=$('#search').val();   

        $.get("<?php echo   Router::url(array('controller'=>'userTypes','action'=>'search'));?>",{search:value},function(data){
        $('.search_data').html(data);
     });
})

Here is my search action

public function search()
    {
        $search=$_GET['search'];
        $request=$this->UserType->find('all', array(
        'conditions' => array('UserType.name LIKE' => "%$search%")
        ));
        $this->set('usertype',$request);
    }

Here is the search.ctp

<?php foreach($usertype as $usertypes) { ?>
    <tr>
            <td><?php echo $usertypes['UserType']['id']; ?></td>
            <td><?php echo $usertypes['UserType']['name']; ?></td>
            <td><?php echo $usertypes['UserType']['description']; ?></td>

    </tr>
<?php } ?>

Pagination limit In appcontroller

 parent::beforeFilter();
            $this->Paginator->settings = array(
              'limit'=>2
        );

Result is showing me like this

enter image description here

May anybody help me for solve it ?

Best answer

If you want to use pagination, your search method should look like this:

public function search()
{
    if(isset($this->request->query['search'])){
       $search = $this->request->query['search'];
    }else{
       $search = '';
    )
    $this->Paginator->settings = array(
        'conditions' => array('UserType.name LIKE' => "%$search%"),
        'limit' => 2
    );
    ));
    $this->set('usertype',$this->Paginator->paginate());
}