CodeIgniter Forums
Codeigniter 3 blog application: how can I avoid using a redundant $config pagination - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Codeigniter 3 blog application: how can I avoid using a redundant $config pagination (/showthread.php?tid=70712)



Codeigniter 3 blog application: how can I avoid using a redundant $config pagination - Ajax30 - 05-19-2018

I am working on a blog application in Codeigniter 3.1.8.
I have a Posts controller at

Quote:application/controllers/Posts.php

I have a Categories controller at

Quote:application/controllers/Categories.php

In the Posts controller I have:

Code:
public function index() {
   $this->load->library('pagination');
   $config = [
       'base_url' => base_url("/posts"),
       'page_query_string' => TRUE,
       'query_string_segment' => 'page',
       'display_pages' => TRUE,
       'use_page_numbers' => TRUE,
       'per_page' => 12,
       'total_rows' => $this->Posts_model->get_num_rows(),
       'uri_segment' => 3,
       'first_link' => '«',
       'first_tag_open' =>  '<li>',
       'first_tag_close' => '</li>',
       'last_link' => '&raquo;',
       'last_tag_open' =>  '<li>',
       'last_tag_close' => '</li>',
       'full_tag_open' =>  '<ul class="pagination">',
       'full_tag_close' => '</ul>',
       'next_link' =>  '&rsaquo;',
       'next_tag_open' =>  '<li>',
       'next_tag_close' => '</li>',
       'prev_link' => '&lsaquo;',
       'prev_tag_open' =>  '<li>',
       'prev_tag_close' => '</li>',
       'num_tag_open' =>   '<li>',
       'num_tag_close' =>  '</li>',
       'cur_tag_open' =>   '<li class="active"><span>',
       'cur_tag_close' =>  '</span></li>'
   ];
    // More code
}

The 

Code:
$config

 array in the Categories controller is almost the same:

Code:
public function posts($id) {
   $this->load->library('pagination');
   $config = [
       'base_url' => base_url('/categories/posts/' . $id),
       'page_query_string' => TRUE,
       'query_string_segment' => 'page',
       'display_pages' => TRUE,
       'use_page_numbers' => TRUE,
       'per_page' => 12,
       'total_rows' => $this->Posts_model->get_num_rows_by_category($id),
       'uri_segment' => 3,
       'first_link' => '&laquo;',
       'first_tag_open' =>  '<li>',
       'first_tag_close' => '</li>',
       'last_link' => '&raquo;',
       'last_tag_open' =>  '<li>',
       'last_tag_close' => '</li>',
       'full_tag_open' =>  '<ul class="pagination">',
       'full_tag_close' => '</ul>',
       'next_link' =>  '&rsaquo;',
       'next_tag_open' =>  '<li>',
       'next_tag_close' => '</li>',
       'prev_link' => '&lsaquo;',
       'prev_tag_open' =>  '<li>',
       'prev_tag_close' => '</li>',
       'num_tag_open' =>   '<li>',
       'num_tag_close' =>  '</li>',
       'cur_tag_open' =>   '<li class="active"><span>',
       'cur_tag_close' =>  '</span></li>'
   ];
}

How could I make a "base" 
$config array which, for every controller it is used in, I would only have to write what is specific?


RE: Codeigniter 3 blog application: how can I avoid using a redundant $config pagination - InsiteFX - 05-19-2018

Read the CodeIgniter User's Guide on Pagination, it tell's you to create a ./application/config/pagination.php
configuration file.