Welcome Guest, Not a member yet? Register   Sign In
Creation of a pagination with $_POST data
#1

[eluser]jeanv[/eluser]
Hi everyone, i'm new in the community and this is my first post... Sorry if my english isn't that good, i'm not a native english speaker!

Ok then, so my problem is about the pagination class and the fact that i need to keep the $_POST data to go from a page to another, and the second problem is the url written during the pagination... let me explain you that:

For my pages i have only use a controller and a view file, no model.
My code, in the controller is:

Code:
function index()
{        
if(isset($_POST['search'])) { $this->session->set_userdata('search', $_POST['search']); }
$search = $this->session->userdata('search');

        
    // PAGINATION
    if( $this->uri->segment(3) == '')
    {
        $limit_min = 0;
        $limit_max = 5;
    }
    else
    {
        $limit_min = $this->uri->segment(3);
        $limit_max = 5;
    }
        
    if( isset($search)) )
    {

        // QUERY
        $data['query'] = $this->db->query('
SELECT my-fields
FROM my-tables
WHERE field1 LIKE \'%'.$search.'%\' LIMIT '.$limit_min.','.$limit_max.'');
            

        // NUMBER OF RESULTS

            $nb = $this->db->query('
SELECT my-fields
FROM my-tables
WHERE field1 LIKE \'%'.$search.'%\' ');
            
            $config['base_url'] = base_url().'home/index/';
            $config['total_rows'] = 18; // i don't know how to find this value
            $config['uri_segment'] = 3;
            $config['per_page'] = '5';
            $config['first_link'] = '<<';
            $config['last_link'] = '>>';
            $config['full_tag_open'] = '<p align="center">';
            $config['full_tag_close'] = '</p>';
            $this->pagination->initialize($config);
            
        }

        // Chargement hpage
        if( isset($search))
        {
            $this->load->view('header', $data);
        }
        else
        {
            $this->load->view('header', $data);
        }
        $this->load->view('main', $data);
        $this->load->view('footer');
        
$this->output->enable_profiler(TRUE);
    }

Ok, with this code i have a "correct" pagination, i mean that when i click on the links created by the pagination, the results change and i can go from a page to another.... but it doesn't seem to be good code... i have to use the session library to keep the values and i don't know how the get the number of results for $config['total_rows'].

AND... when for exemple i click on the link "2" (to go to page 2) of the pagination my url is: http://www.mysite.com/home/index/5 and if i click on the link "3" i have http://www.mysite.com/home/index/10


so i would like the point of view of people that know how to do it better because i would like to improve my code... (if possible without model file, i am not very used to MVC achitecture)

Thanks a lot to the guys that will thake time to help me !
#2

[eluser]Yash[/eluser]
I'm not going to touch your code.I can guide you.

use models.Even for giving HTML based output.Use return in model function.From controller just query item. and then take data from model and print that.Easy? Try
#3

[eluser]jeanv[/eluser]
hi Yash, as i said i didn't ask for someone to touche my code, i just nedd the best method to use the pagination class ! i'm going to try with a model file, i saw a few days ago a topic in the forum about that using a model file... thanks
#4

[eluser]Yash[/eluser]
Just refer [url="http://ellislab.com/codeigniter/user-guide/general/models.htm"]user guide - Models[/url] very easy to use.

Enjoy coding.




Theme © iAndrew 2016 - Forum software by © MyBB