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 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!