CodeIgniter Forums
How to mantain POST data when using pagination in CI - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to mantain POST data when using pagination in CI (/showthread.php?tid=50947)



How to mantain POST data when using pagination in CI - El Forum - 04-14-2012

[eluser]ReyPM[/eluser]
Hi, I'm trying to paginate results in a search form but I lost the parameter when pass from one page to another using pagination class. My code is as follow:

controller
Code:
public function ajaxsearch($start = 0) {
        $this->output->enable_profiler();
        
        $this->config->load('sicautogas');
        
        $this->load->model('Owners_model');
        $output_string = $this->Owners_model->AjaxSearch($this->security->xss_clean($this->input->post('keyword')), $this->config->item('max_per_page'), $start);
        
        $this->load->library('pagination');
        $config['base_url'] = site_url('users/ajaxsearch');
        $config['total_rows'] = count($output_string);
        $config['row_per_page'] = $this->config->item('max_per_page');
        
        $config['full_tag_open'] = '<div class="pagination"><ul>';
        $config['full_tag_close'] = '</ul></div>';
        $config['first_link'] = false;
        $config['last_link'] = false;
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['prev_link'] = '&larr; Previous';
        $config['prev_tag_open'] = '<li class="prev">';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = 'Next &rarr;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';

        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $data['output_string'] = $output_string;
        
        $this->load->view('users/search', $data);
}

and the model associated is:
Code:
function AjaxSearch($keywords, $end, $start) {
        if ($this->session->userdata('regiones') != '') {
            $sql = "SELECT p.codigo, p.nb_propietario, p.cedula, p.telefono, p.direccion FROM tr023_propietario p, v002_geografico_codigos gc, tr003_geografico g WHERE p.co_geografico = g.codigo AND g.co_estado = gc.co_estado AND gc.region = '" . $this->session->userdata('regiones') . "' AND cedula LIKE UPPER(?) OR nb_usuario LIKE UPPER(?) GROUP BY p.codigo, p.nb_propietario, p.cedula, p.telefono, p.direccion";
        } else {
            $sql = "SELECT * FROM tr023_propietario WHERE (cedula LIKE UPPER(?)) OR (nb_propietario LIKE UPPER(?)) LIMIT ? OFFSET ?";
        }

        $query = $this->db->query($sql, array('%' . $keywords . '%', '%' . $keywords . '%', $end, $start));
        return $query->result();
}
What's wrong here?


How to mantain POST data when using pagination in CI - El Forum - 04-14-2012

[eluser]Glazz[/eluser]
Maybe you need to append the data you don't want to lose while paginating in the $config['base_url'] variable..



How to mantain POST data when using pagination in CI - El Forum - 04-14-2012

[eluser]ReyPM[/eluser]
I don't want this because then could be changed and could be a security issue for my application


How to mantain POST data when using pagination in CI - El Forum - 04-14-2012

[eluser]Glazz[/eluser]
Save in session ?


How to mantain POST data when using pagination in CI - El Forum - 04-14-2012

[eluser]ReyPM[/eluser]
I think in that solution but let me ask something, I have previous session created with data if I do this:
Code:
$search_data = array('keyword' => $this->input->post('keyword', TRUE));
I loss the previous session data or this new one is added?

Cheers


How to mantain POST data when using pagination in CI - El Forum - 04-14-2012

[eluser]ReyPM[/eluser]
Answer myself: the previous session data isn't lost. I found a great solution at this page: http://www.andyhawthorne.co.uk/how-to-paginate-search-results-with-codeigniter-2-1/ hope others can use it too.

@Glazz thanks for you time


How to mantain POST data when using pagination in CI - El Forum - 04-14-2012

[eluser]Glazz[/eluser]
No, i don't think so, you just need to be sure that you don't overwrite your other session variables.

For example:
Code:
$test = array(
   'searchdata' => 'your search data here'
);
$this->session->set_userdata($test);
echo $this->session->userdata('searchdata');
It should echo your search data here

If you do this:
Code:
$test = array(
   'searchdata' => 'it is overwrited'
);
$this->session->set_userdata($test);
echo $this->session->userdata('searchdata');
It should echo it is overwrited

So the session variable was overwritten, if you know what i mean..


How to mantain POST data when using pagination in CI - El Forum - 04-14-2012

[eluser]skunkbad[/eluser]
I have a full working example of a paginated search in Community Auth. It uses ajax though, and does not fall back to a non-js solution.


How to mantain POST data when using pagination in CI - El Forum - 04-14-2012

[eluser]jellysandwich[/eluser]
There's another option you should consider: using GET instead of POST.

Searches are typically "safe", so it's okay to use GET because you don't need to worry about users performing destructive actions.

If you use GET, it will be much easier to maintain and control the states (i.e., pagination). Not only that, but users can bookmark the searches and come back to it easily.

Also, users won't need to deal with the annoying "Are you sure you want to resubmit?" popups when they refresh. Personally, this is one of my biggest peeves.

See more here:
http://stackoverflow.com/questions/46585/when-do-you-use-post-and-when-do-you-use-get