Welcome Guest, Not a member yet? Register   Sign In
Validation, Search & Pagination
#1

[eluser]Guerra[/eluser]
Hi all. Help solve the problem. When i add validation to the search form on the site, no longer work correctly pagination search results - displays only the first page of results. If i remove the form validation - pagination working correctly. In what could be the problem?
Code:
function search(){
   $fields['term'] = 'пошук';
   $this->validation->set_fields($fields);
   $rules['term'] = 'trim|required|min_length[4]|max_length[20]|xss_clean';
   $this->validation->set_rules($rules);
   if ($this->validation->run() == TRUE){
      if($this->input->post('term')){
         $this->session->set_userdata('keyword', $this->input->post('term'));
      }  
      $per_page = 2;
      $data['results'] = $this->search->searcht((int)$this->uri->segment(3),$per_page,$this->session->userdata('keyword'));                        
      $config['base_url'] = base_url().'blog/search';
      $config['total_rows'] = $this->search->searchc($this->session->userdata('keyword'));;
      $config['per_page'] = $per_page;            
      $this->pagination->initialize($config);            
      $data['pag_links'] = $this->pagination->create_links();            
      
      $data['main'] = 'search';
      $this->load->view('template',$data);                
   }else{
      $this->session->set_userdata('err',$this->validation->error_string);
      
      $data['main'] = 'emptysearch';
      $this->load->view('template',$data);
   }
}
#2

[eluser]K-Fella[/eluser]
From the code you posted, if validation fails it should show the emptysearch view. Correct? Does this view require pagination? If so, you need to set the pagination config values. At the moment the pagination values are only being set when the form validation returns true.
#3

[eluser]Guerra[/eluser]
Pagination need only to True - 'search' view.
The problem is that when enabled validation of the search form is not correctly handle pagination to display the search results - displays only the first page of search :grrr:
#4

[eluser]K-Fella[/eluser]
Try adding:
Code:
config['uri_segment'] = 3
after
Code:
config['base_url'] = base_url().'blog/search';
#5

[eluser]Guerra[/eluser]
tried, does not help :grrr:
#6

[eluser]zimco[/eluser]
Did you ever solve this problem? I am having the exact same problem: first page of search results work, but whenever i try to go to the paginated second page the validation always fails, and dumps me back to the start search, like it no longer has the session search keywords.
#7

[eluser]Guerra[/eluser]
I decided my problem Smile
You validation always returns false?
#8

[eluser]zimco[/eluser]
What was your fix? Mine always fails validation when i click the pagination second page link. First page of search results always work as expected.

My controller:
Code:
function index()
{      
   #Validations
   $this->load->library('form_validation');
   $this->form_validation->set_rules('searchbox', 'Search Box', 'required|alpha_space|encode_php_tags|xss_clean');

   #Load proper view either for validation fail or success
   if ($this->form_validation->run() == FALSE)
   { //validation FAILED
     $data['program_count'] = $this->quickstats_model->get_stats();
     echo "FAIL";
     $this->template->display('start', $data);
   }
   else
   { //validation SUCCESS
    if($this->input->post('searchbox')) {
        $this->session->set_userdata('keywords', $this->input->post('searchbox'));
      }
    $word = array();
    $keys = $this->session->userdata('keywords');
    echo $keys;
       $word = explode(" ", $keys);
       //Initialize Pagination Class
    $this->load->library('pagination');
    $config['base_url'] = base_url().'searchform/index/';
    $config['uri_segment'] = 3;
    $config['total_rows'] = $this->Search_model->count_all_byword($word);
    $config['per_page'] = '5';
    $config['num_links'] = '15';
    $config['full_tag_open'] = '<div id="pagination">';
    $config['full_tag_close'] = '</div>';
    $this->pagination->initialize($config);

       $data['output'] = $this->Search_model->multiColSearch($word, $config['per_page'], $this->uri->segment(3,0));
       $data['pagination'] = $this->pagination->create_links();
    $data['total_rows'] = $config['total_rows'];

    $this->template->display('prog_searchresults', $data);
    }
  }
#9

[eluser]Wayne Smallman[/eluser]
Earlier today, I started building a search tool based on some code from another project. I thought adding pagination would be easy — oh yeah.

I've seen plenty of people talking about solving the pagination problem, but they're doing so at the expense of cleansing the form data, which is a huge security problem.

Anyway, if you look at the pagination here on the CodeIgniter forums, and on most other fora, there's an initial re-direct, and then you arrive at the search results.

That's where I'm going to begin my search — and perhaps store the search result in a database table.




Theme © iAndrew 2016 - Forum software by © MyBB