• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pagination / Reduce footprint / Advice

#1
Hi,

I’m fairly new to CI but have been loving it so far! I currently have a ‘staff’ listing with pagination which is working as expected. However feel my code is a little ‘bloated’ every time i want to initialize paging in my controllers. I’m looking for a way to minimize the footprint in my controllers to maybe a couple lines of code, but am unsure on the best way to do this. I hoped someone would be able to point me in the right direction.

As mentioned above, the code works perfectly - but seems long winded

1)

After a bit of scanning around online, it said that using a config file may help. I therefore have a ‘pagination.php’ in the configs folder which does a little bit of customization to how the paging control will render. This seems to work well, and I’m fairly happy with that part.

2)

In my controller, I have the following few lines of code (which seems too much for me) to set up the paging control. I’m looking for a way to minimize this down to a couple lines ideally. What way would you suggest doing this?

PHP Code:
public function index(){
$data['title'] = 'Staff Listing';

/* PAGING */
$config['base_url'] = '/staff/index';
$config['per_page'] = '1';
$config["total_rows"] = $this->staff_model->count();
$config["num_links"] = round($config["total_rows"] / $config["per_page"]);
$this->pagination->initialize($config);

$page = ($this->uri->segment($this->config->item('uri_segment'))) ? $this->uri->segment($this->config->item('uri_segment')) : 0;
$data["staff"] = $this->staff_model->get_all($config["per_page"], $page);
$data["paging"] = $this->pagination->create_links();

$this->load->view('staff/index'$data);


Thanks for your time.

MoFish
Reply

#2
The pagination config stuff can be placed in a config file as application/config/pagination.php and it will load it from the file instead of from your controller. Just create the pagination.php file with the config array inside of it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
First of all you can make array in 1 line if its too much for you each key to be in new line.
Second as insitfx said you can use default pagination config.
And at last every Pagination class (not just CI) has minimum requested params to make a proper generation. So there is no much possibility to decrease the settings at the moment.

Another point is that I personally don't like CI Pagination and I don't use it usually Smile
Best VPS Hosting : Digital Ocean
Reply

#4
Thank you for your replies.

I use a config file for the 'core' pagination setup. I have tried to leave everything in there which I feel could be globally set, but still have a few to specify on a per controller basis. As sv3tli0 mentioned, this is maybe the nature of the beast.

Please see my config file below.

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['uri_segment'] = 3;
$config['per_page'] = '10';
$config['page_query_string'] = FALSE;
$config['use_page_numbers'] = FALSE;
$config['query_string_segment'] = '';
$config['full_tag_open'] = '<nav><ul class="pagination">';
$config['full_tag_close'] = '</ul></nav>';
$config['first_link'] = '&laquo; First';
$config['first_tag_open'] = '<li class="prev page">';
$config['first_tag_close'] = '</li>';
$config['last_link'] = 'Last &raquo;';
$config['last_tag_open'] = '<li class="next page">';
$config['last_tag_close'] = '</li>';
$config['next_link'] = 'Next &rarr;';
$config['next_tag_open'] = '<li class="next page">';
$config['next_tag_close'] = '</li>';
$config['prev_link'] = '&larr; Previous';
$config['prev_tag_open'] = '<li class="prev page">';
$config['prev_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li class="page">';
$config['num_tag_close'] = '</li>';
$config['anchor_class'] = 'follow_link'

sv3tli0, I have moved the array elements onto one line. It's maybe a little ugly, but it will do for now.  Undecided

PHP Code:
$config['base_url'] = '/staff/index'$config['per_page'] = '1'$config["total_rows"] = $this->staff_model->count(); $config["num_links"] = round($config["total_rows"] / $config["per_page"]);
$this->pagination->initialize($config); 

I may just have to suck it up on this one. I was hoping there was going to be a cleaner way around this. sv3tli0, out of curiosity - which paging do you use?

Thanks for your time.

MoFish
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.