CodeIgniter Forums
Refactoring the pagination code - 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: Refactoring the pagination code (/showthread.php?tid=23055)



Refactoring the pagination code - El Forum - 09-28-2009

[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?


Refactoring the pagination code - El Forum - 09-29-2009

[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


Refactoring the pagination code - El Forum - 09-29-2009

[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