Reduce lines of code for pagination codeigniter/declare once

时间:2018-02-03 08:34:38

标签: php codeigniter pagination

I'm building a project and i have to paginate data more than once.

In a controller function, there is 2 outcomes. A failure and a success but regardless of the outcome the page will be redirected back to the page where the function is called.

Suppose that page has 2 data paginated on it, (example if a page has table of contents and comments).

So in my controller the line of code will be very high since i have to add pagination lines in my controller for both failure and success outcomes.

This is my pagination code:

$config['base_url'] = ;
    $config['total_rows'] = $num_rows;
    $config['per_page'] = 10;
    $config['num_links'] = $num_rows;
    $config['uri_segment'] = 4;

    $config['full_tag_open'] = '<ul class="pagination">';
    $config['full_tag_close'] = '</ul>';
    $config['first_link'] = '0';
    $config['last_link'] = 'Last';
    $config['first_tag_open'] = '<li class="page-item">';
    $config['first_tag_close'] = '</li>';
    $config['prev_link'] = '&laquo';
    $config['prev_tag_open'] = '<li class="page-item prev">';
    $config['prev_tag_close'] = '</li>';
    $config['next_link'] = '&raquo';
    $config['next_tag_open'] = '<li class="page-item">';
    $config['next_tag_close'] = '</li>';
    $config['last_tag_open'] = '<liclass="page-item">';
    $config['last_tag_close'] = '</li>';
    $config['cur_tag_open'] = '<li class="page-item active"><a href="#">';
    $config['cur_tag_close'] = '</a></li>';
    $config['num_tag_open'] = '<li class="page-item">';
    $config['num_tag_close'] = '</li>';

It makes my code very messy and hard to follow because it becomes very long.

Is there any way that i can do this only once and just initiate the pagination for everytime i do it.

I think the first part is only needed, the second part i think can be declared only once and make it global?

Edit: Maybe i can add a constructor in my controller and declare it there? would it make it global? Idk if this is the right solution. or an index function?

Edit1: This is now my pagination.php file in my config folder. I've also tested this out to check whether it is working/affecting my pagination links.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

    $config['full_tag_open'] = '<ul class="pagination">';
    $config['full_tag_close'] = '</ul>';
    $config['first_link'] = 'First';
    $config['last_link'] = 'Last';
    $config['first_tag_open'] = '<li class="page-item">';
    $config['first_tag_close'] = '</li>';
    $config['prev_link'] = '&laquo';
    $config['prev_tag_open'] = '<li class="page-item prev">';
    $config['prev_tag_close'] = '</li>';
    $config['next_link'] = '&raquo';
    $config['next_tag_open'] = '<li class="page-item">';
    $config['next_tag_close'] = '</li>';
    $config['last_tag_open'] = '<liclass="page-item">';
    $config['last_tag_close'] = '</li>';
    $config['cur_tag_open'] = "<li class='page-item active'><a href='#'>";
    $config['cur_tag_close'] = "</a></li>";
    $config['num_tag_open'] = '<li class="page-item">';
    $config['num_tag_close'] = '</li>';

?>

3 个答案:

答案 0 :(得分:1)

Just create a pagination.php file in the config folder and put your configuration in it. codeigniter will autimatically load your configuration.

答案 1 :(得分:1)

  1. 在配置文件夹中创建pagination.php文件

  2. 将以下代码放在pagination.php中

         <?php
             $config['full_tag_open'] = '<ul class="pagination">';
             $config['full_tag_close'] = '</ul>';
             $config['num_tag_open'] = '<li class="page-item">';
             $config['num_tag_close'] = '</li>';
             $config['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';
             $config['cur_tag_close'] = '</a></li>';
             $config['next_tag_open'] = '<li class="page-item">';
             $config['next_tagl_close'] = '</a></li>';
             $config['prev_tag_open'] = '<li class="page-item">';
             $config['prev_tagl_close'] = '</li>';
             $config['first_tag_open'] = '<li class="page-item disabled">';
             $config['first_tagl_close'] = '</li>';
             $config['last_tag_open'] = '<li class="page-item">';
             $config['last_tagl_close'] = '</a></li>';
             $config['attributes'] = array('class' => 'page-link');    
    

    ?>

  3. 控制器视图

     $total_users = $this->User_model->get_total_users(1,$search_text);
     $config['base_url'] = base_url().'manage/users/index/';
     $config['total_rows'] = $total_users;
     $config['per_page'] = 10;
    
     $this->pagination->initialize($config);
    
  4. 输出如下

enter image description here

答案 2 :(得分:0)

The simplest/shortest way is to use array_fill_keys with array_merge, e.g.:

$keys = ['first_tag_close', 'next_tag_close','prev_tag_close',
         'last_tag_close','num_tag_close'];
$config = array_merge($config, array_fill_keys($keys, '</li>'));

However, a simple reusage would also shorten the number of lines:

$config['first_tag_open'] = $config['last_tag_open'] =
    $config['next_tag_open'] = '<li class="page-item">';
$config['first_tag_close'] = $config['next_tag_close'] =
    $config['prev_tag_close'] = $config['last_tag_close'] =
    $config['num_tag_close'] = '</li>';
$config['prev_link'] = $config['next_link'] = '&laquo';

Or, if you prefer list from PHP 7:

list($config['first_tag_open'], $config['last_tag_open'],
  $config['next_tag_open']) = array_fill(0, 3, '<li class="page-item">');
list($config['first_tag_close'], $config['next_tag_close'],
  $config['prev_tag_close'], $config['last_tag_close'],
  $config['num_tag_close']) = array_fill(0, 5,'</li>');