我正在使用Codeigniter 3.1.8中的博客应用。
我在
有一个帖子控制器应用/控制器/ Posts.php
我有一个类别控制器
应用/控制器/ Categories.php
在帖子控制器中,我有:
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' => '»',
'last_tag_open' => '<li>',
'last_tag_close' => '</li>',
'full_tag_open' => '<ul class="pagination">',
'full_tag_close' => '</ul>',
'next_link' => '›',
'next_tag_open' => '<li>',
'next_tag_close' => '</li>',
'prev_link' => '‹',
'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
}
类别控制器中的$config
数组几乎相同:
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' => '«',
'first_tag_open' => '<li>',
'first_tag_close' => '</li>',
'last_link' => '»',
'last_tag_open' => '<li>',
'last_tag_close' => '</li>',
'full_tag_open' => '<ul class="pagination">',
'full_tag_close' => '</ul>',
'next_link' => '›',
'next_tag_open' => '<li>',
'next_tag_close' => '</li>',
'prev_link' => '‹',
'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>'
];
}
我怎样才能建立一个&#34;基地&#34; $config
数组,对于它所使用的每个控制器,我只需编写特定的
答案 0 :(得分:2)
您唯一需要做的就是创建一个pagination.php
配置文件,并将所有内容放入其中。
下次加载分页库时,它将使用此文件,您可以使用
$config['whatever'] = 'anything';
// Option: 1
$this->load->library('pagination', $config);
// Option: 2
$this->load->library('pagination');
$this->pagination->initialize($config);
Almot所有CodeIgniter库都可以拥有自己的配置文件,无需创建库或帮助程序甚至控制器的方法来处理配置文件可以执行的操作。
您希望为所有应用程序提供持续的分页配置,并可以动态更改它吗?使用配置文件pagination.php
并停止浪费您的时间和执行时间来编写整个库或帮助程序来处理它。
答案 1 :(得分:1)
您可以使用方法创建库,然后在整个应用程序中使用它。就像我在Nuts_lib.php
文件夹
generate_pagination()
方法的库libraries
一样
public function generate_pagination($url, $total_rows, $per_page = 10, $num_link = 5) {
$this->CI->load->library('pagination');
$config['base_url'] = site_url_tr($url);
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$config['num_links'] = $num_link;
$config['enable_query_strings'] = TRUE;
$config['use_page_numbers'] = TRUE;
$config['query_string_segment'] = 'page';
$config['page_query_string'] = TRUE;
$config['full_tag_open'] = '<ul class="pagination no-margin">';
$config['full_tag_close'] = '</ul>';
$config['cur_tag_open'] = '<li class="active"><a href="javascript:void();">';
$config['cur_tag_close'] = '</a></li>';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['next_link'] = 'Next >';
$config['prev_link'] = '< Prev';
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$this->CI->pagination->initialize($config);
return $this->CI->pagination->create_links();
}
不,我在控制器中使用它如下
$total_rows = $this->db->get('routes')->num_rows();
$per_page = 15;
$num_links = 5;
$url = 'routes/all';
$links = $this->nl->generate_pagination($url, $total_rows, $per_page, $num_links);
if ($this->input->get('page')) {
$sgm = (int) trim($this->input->get('page'));
$segment = $per_page * ($sgm - 1);
} else {
$segment = 0;
}
$data = array(
'title' => lang('all_routes'),
'routes' => $this->rm->get_all($per_page, $segment, $d, $t, $ttype, $i),
'links' => $links,
'segment' => $segment
);
然后在视野中
<?php echo $links; ?>
也会像$segment
$segment++
答案 2 :(得分:1)
根据我的想法,
您需要创建一个通用函数模型,并在autoload.php
如需参考,请查看以下代码。
在Common_function_model
application/models
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
Class Common_functions_model extends CI_Model {
public function get_pagination_config($settings = array()) {
$config = [
'base_url' => isset($settings['base_url']) ? $settings['base_url'] : base_url(),
'page_query_string' => isset($settings['page_query_string']) ? $settings['page_query_string'] : TRUE,
'query_string_segment' => isset($settings['query_string_segment']) ? $settings['query_string_segment'] : 'page',
'display_pages' => isset($settings['display_pages']) ? $settings['display_pages'] : TRUE,
'use_page_numbers' => isset($settings['use_page_numbers']) ? $settings['use_page_numbers'] : TRUE,
'per_page' => isset($settings['per_page']) ? $settings['per_page'] : 12,
'total_rows' => isset($settings['total_rows']) ? $settings['total_rows'] : "",
'uri_segment' => isset($settings['uri_segment']) ? $settings['uri_segment'] : 3,
'first_link' => isset($settings['first_link']) ? $settings['first_link'] : '«',
'first_tag_open' => isset($settings['first_tag_open']) ? $settings['first_tag_open'] : '<li>',
'first_tag_close' => isset($settings['first_tag_close']) ? $settings['first_tag_close'] : '</li>',
'last_link' => isset($settings['last_link']) ? $settings['last_link'] : '»',
'last_tag_open' => isset($settings['last_tag_open']) ? $settings['last_tag_open'] : '<li>',
'last_tag_close' => isset($settings['last_tag_close']) ? $settings['last_tag_close'] : '</li>',
'full_tag_open' => isset($settings['full_tag_open']) ? $settings['full_tag_open'] : '<ul class="pagination">',
'full_tag_close' => isset($settings['full_tag_close']) ? $settings['full_tag_close'] : '</ul>',
'next_link' => isset($settings['next_link']) ? $settings['next_link'] : '›',
'next_tag_open' => isset($settings['next_tag_open']) ? $settings['next_tag_open'] : '<li>',
'next_tag_close' => isset($settings['next_tag_close']) ? $settings['next_tag_close'] : '</li>',
'prev_link' => isset($settings['prev_link']) ? $settings['prev_link'] : '‹',
'prev_tag_open' => isset($settings['prev_tag_open']) ? $settings['prev_tag_open'] : '<li>',
'prev_tag_close' => isset($settings['prev_tag_close']) ? $settings['prev_tag_close'] : '</li>',
'num_tag_open' => isset($settings['num_tag_open']) ? $settings['num_tag_open'] : '<li>',
'num_tag_close' => isset($settings['num_tag_close']) ? $settings['num_tag_close'] : '</li>',
'cur_tag_open' => isset($settings['cur_tag_open']) ? $settings['cur_tag_open'] : '<li class="active"><span>',
'cur_tag_close' => isset($settings['cur_tag_close']) ? $settings['cur_tag_close'] : '</span></li>'
];
return $config;
}
}
适用于您的Post
控制器。将您的代码更改为以下内容。
public function index() {
$this->load->library('pagination');
$settings = array(
"base_url" => base_url("/posts"),
"total_rows" => $this->Posts_model->get_num_rows(),
);
$config = $this->common_functions_model->get_pagination_config($settings);
// More code
}
适用于您的Categories
控制器。将您的代码更改为以下内容。
public function posts($id) {
$this->load->library('pagination');
$settings = array(
"base_url" => base_url('/categories/posts/' . $id),
"total_rows" => $this->Posts_model->get_num_rows_by_category($id),
);
$config = $this->common_functions_model->get_pagination_config($settings);
}
您可以在任何Controller上使用get_pagination_config
函数,并使用您想要修改的参数。
请注意,您需要自动加载common_functions_model
文件中的autoload.php
。