Autoload Config for Pagination in CodeIgniter not working

为君一笑 提交于 2019-12-07 07:35:08

问题


I am trying to implement pagination in my CI webapp. Now I put the config for pagination inside a config file like this...

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['base_url'] = "http://example.com/index.php/home/index";
$config['num_links'] = "9";
$config['per_page'] = "20";
$config['total_rows'] = "200";

/* End of file pagination.php */
/* Location: ./system/application/config/pagination.php */

In my controller, I have loaded the library

$this->load->library("pagination");

And I have defined the pagination config file to be autoload in config/autoload.php

$autoload['config'] = array('pagination');

At last I called the method to create links in my view template:

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

This did not create any links. The configuration is being autoloaded correctly. I checked using...

<?php echo $this->config->item("num_links"); ?> <!-- this dislayed 9 -->

What am I missing here? Just for the record, putting the config inside the controller didn't work either.

Update #1- I have found out that the config settings are loading correctly but they are not reaching the library or something like that. Inside the pagination library - I did some hard coding to find out that per_page parameter was zero in there.

Update #2- I was mistaken when I said that putting the config inline wasn't working. It is working fine. The autoload isn't working.

Regards


回答1:


Finally used this code to solve my problem...

$this->config->load("pagination");
$page_limit = $this->config->item("per_page");
$config['total_rows'] = $var; // Some variable count
$this->pagination->initialize($config);

This lets me define the config items in a file as well as initialize the items I want in controller like in my case, the total no. of rows - retrieved from database.

Regards




回答2:


Your autoload line in your config file should be this

$autoload['libraries'] = array('pagination');

And you must have this line in you controller after your config array, before you use create_links() etc.

$this->pagination->initialize($config);


来源:https://stackoverflow.com/questions/3316428/autoload-config-for-pagination-in-codeigniter-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!