我编辑了这个后,弄清楚了几件事情,但是这是一个好方法,如果我想我的索引链接?没有函数页面,如果base_url是test/index,它将无法正常工作,但test/test将起作用。Codeigniter分页功能索引
控制器
类试验延伸是CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Test_model');
$this->load->library('pagination');
}
public function index()
{
$page['title'] = '';
$page['file'] = 'test/index';
$config['base_url'] = base_url().'test/page';
$config['total_rows'] = $this->Test_model->record_count();
$config['per_page'] = 2;
$config['num_links'] = 5;
$offset = $this->uri->segment(3,0);
$this->pagination->initialize($config);
$page['data']['items'] = $this->Test_model->getItems($config['per_page'], $offset);
$page['data']['pagination'] = $this->pagination->create_links();
$this->load->view('template', $page);
}
public function page()
{
$page['title'] = '';
$page['file'] = 'test/index';
$config['base_url'] = base_url().'test/page';
$config['total_rows'] = $this->Test_model->record_count();
$config['per_page'] = 2;
$config['num_links'] = 5;
$offset = $this->uri->segment(3,0);
$this->pagination->initialize($config);
$page['data']['items'] = $this->Test_model->getItems($config['per_page'], $offset);
$page['data']['pagination'] = $this->pagination->create_links();
$this->load->view('template', $page);
}
}
模型
public function record_count()
{
return $this->db->count_all('item');
}
public function getItems($limit, $offset)
{
$query = $this->db->get('item', $limit, $offset);
$result = $query->result();
return $result;
}
视图
<h2><?=$pagination; ?></h2>
<table>
<?php foreach($items as $item) { ?>
<tr><td><?=$item->name?></td></tr>
<?php } ?>
你忘了'$ config ['uri_segment'] = 3;'。 –
我在第一次尝试,然后看到它更容易传递$模型的偏移量。如果我将'函数索引'改为'函数页'并将base_url改为'测试/页面',它也可以工作。只是由于某种原因,它不会在'函数索引' – dynamo
传递'$ uri_segment'来索引和检查输出。 –