Welcome Guest, Not a member yet? Register   Sign In
Codeigniter 3 blog application: how can I avoid using a redundant $config pagination
#1

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?
Reply
#2

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

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB