[eluser]zimco[/eluser]
I have a search form that returns paged results and have been struggling with the logic for getting the form_validation class and the pagination class to correctly work together.
It finds and loads the first page of results fine, but when you click the next page link it fails validation everytime. Why?
If i make the validation check like:
Code:
if ( $this->form_validation->run() == FALSE AND !$this->session->userdata('keywords') )
then the paging works, but validation fails as it lets me enter a blank form, or invalid characters.
I have already searched the forum and read many posts relating to this problem but did not find a clear solution to the problem. Does anybody have a simple working example they could share?
Here is my controller below. What am i doing wrong? What is the correct logic?
Code:
function index()
{
$data['css'] = $this->css;
$data['base'] = $this->base;
$data['loadmyjavascript'] = FALSE;
#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 AND !$this->session->userdata('keywords') )
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.php', $data);
}
}