0

I am using cakePHP 4 and I want to paginate my search query. I have users index page and when I search something whose resultant data exceed the defined limit of showing data. Whenever I search something I want the search string to retain in the input box even if I jump to the next page

public function index(){ 
        $this->conditions = array();
        if($this->session->check('conditions')){
            $this->conditions = $this->session->read('conditions');
        } 
        if ($this->request->is('post')) {
            $data = $this->request->getData();
            if(!empty($data['search_string'])){
                $searchString = trim($data['search_string']);
                if($data['search_by'] == 1){
                    $conditions = array('Users.id LIKE '=> $searchString);
                }elseif($data['search_by'] == 2){
                    $conditions = array('Users.username LIKE '=>'%'.$searchString.'%');
                }elseif($data['search_by'] == 3){
                    $conditions = array('Groups.name LIKE ' => '%'.$searchString.'%');
                }
            } 
            $this->conditions = array ($conditions);
            $this->session->write('conditions',$this->conditions);
            $this->paginate['conditions'] = $this->conditions;
            $this->paginate['contain'] = ['Userdetails','Groups'];
            $this->paginate['limit'] = 8;
            $users = $this->paginate($this->Users);
            $this->set('users',$users);  
        }else{ 
            $this->session->write('conditions',$this->conditions);
            $this->paginate['conditions'] = $this->conditions; 
            $this->paginate['contain'] = ['Userdetails','Groups']; 
            $this->paginate['limit'] = 8; 
            $users = $this->paginate($this->Users);
            $this->set('users',$users);
        }
    }
Salines
  • 5,674
  • 3
  • 25
  • 50
vanshika
  • 1
  • 2

0 Answers0