Welcome Guest, Not a member yet? Register   Sign In
Refactoring the pagination code
#1

[eluser]luffy[/eluser]
I found that I want to implement the pagination, I have to write much.
So I decide to refactor it.

I build one function below:
Code:
// Pagination
    public function pagelist($total, $per_page)
    {
        $config['base_url'] = base_url().$this->uri->segment(1).'/'.$this->uri->segment(2);
        $config['total_rows'] = $total;
        $config['per_page'] = $per_page;
        
        $config['first_link'] = 'first';
        $config['last_link'] = 'last';
        $config['next_link'] = 'next';
        $config['prev_link'] = 'previous';

        $this->pagination->initialize($config);
        return $this->pagination->create_links();
    }


And then I put the function in the common model.
If I want to use it, just only need one line below:
$data['page_links'] = $this->common->pagelist($this->say->get_posts_count(), 10);

How do you think that?
#2

[eluser]designfellow[/eluser]
Hi,
its not valid to put config values in a model in MVC.
But CI accept this without error.
Actually this function is not necessary.
You can put a pagination.Php file under config directory and include the default pagination config values in that file.
It will be automatically initialized and you can simply call the create_links() function.

Happy Coding,
DesignFellow
#3

[eluser]luffy[/eluser]
I don't want to write too much pagination code


If I put the function in the model, why is it not right?

I don't know how to use the method which you said




Theme © iAndrew 2016 - Forum software by © MyBB