doushaiyu5065 2017-04-12 16:42
浏览 103
已采纳

配置文件中的Codeigniter分页

According to the CI official documentation it's possible to set preferences in a config file, but I can't get this to work.

I think I have narrowed the issue down to line 3 of my pagination.php file - the error message I receive is;

Message: Undefined property: CI_Loader::$db

Message: Call to a member function get() on null

Whenever I change this line of code from;

$config['total_rows'] = $this->db->get('item')->num_rows();

to

$config['total_rows'] = 200;

Everything works as expected. Am I failing to load something or call the database?

My code is:

Controller

public function index() {
    $this->load->library('pagination');
    $data = array(
        'items'     => $this->items_model->itemList(),
        'title'     => 'Library Items'
        );
    $this->load->view('item_list', $data);
}

Model

public function itemList() {
    $this->db->select('item_id, item_title');
    $this->db->from('item');
    $this->db->limit(5, $this->uri->segment(3));
    return $this->db->get()->result();
}

View

<?php echo $this->pagination->create_links() ?>

pagination.php

defined('BASEPATH') OR exit('No direct script access allowed');
$config['base_url'] = '/ci/items/index';
$config['total_rows'] = $this->db->get('item')->num_rows();// when I change this to `$config['total_rows'] = 200` it works
$config['per_page'] = 5;
$config['num_links'] = 5;
$config['full_tag_open'] = '<div><ul class="pagination">';
$config['full_tag_close'] = '</ul></div><!--pagination-->';
$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';

Thanks

  • 写回答

3条回答 默认 最新

  • douyun3022 2017-04-13 18:21
    关注

    If you keep changing the question it makes previous answers look a bit strange. However you cannot load the db or use a db call in your config file. If you have to run a database query to get a value you are no longer using a config setting, but assigning a value to an arguement for use in a library method call.

    So just set the config value to some fixed reasonable value, say 50, then in your controller override the setting using something like:

    // load config file
    $this->config->load('pagination', TRUE);
    
    // access pagination settings
    $settings = $this->config->item('pagination');
    
    // change whatever you need to
    $settings['total_rows'] = $this->db->get('item')->num_rows();
    
    // use the settings to initialize the library
    $this->pagination->initialize($settings);
    

    There might be an easier way but as far as I know there are no other setters in the pagination library.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误