0

我在弄清楚了一些事情后对其进行了编辑,但是如果我希望我的链接出现在索引上,这是一个好方法吗?如果没有功能页面,base_url 为 test/index 将无法正常工作,但 test/test 可以。

控制器

类测试扩展 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 } ?>
4

2 回答 2

2

尝试这个:

function  __construct()
    {

        parent::__construct();
        $this->load->helper('array');
        $this->load->helper('url');
        $this->load->library('pagination');
        $this->load->model('Test_model','',TRUE);
    }

function index($uri_segment="3")
{   
        $config['base_url'] = base_url('test/index');
        $config['total_rows'] = $this->Test_model->record_count();
        $config['per_page'] = 5;
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);

$page['data']['products'] = $this->Test_model->getItems($config['per_page'],$this->uri->segment(3));

    $page['data']['pagination']= $this->pagination->create_links();
    $page['title']  = '';
    $page['file']   = 'test/index';

    $this->load->view('template', $page);
}

如果生成的列表在单击下一页时显示随机页面,则它应该采用下一页编号而不是下一个 id(+5)。在这种情况下,添加

   $config['use_page_numbers'] = TRUE;

在初始化分页配置之前。

于 2012-09-03T06:55:30.123 回答
1

在控制器中,您必须更新它

public function index()
    {

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

        $config['base_url'] = base_url().'test/index'; // use test/test and it works
        $config['total_rows'] = $this->Test_model->record_count();
        $config['per_page'] = 5;
        $config['num_links'] = 10;
        $config['uri_segment'] = 3;  

        $offset = $this->uri->segment(3,0);

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

        $page['data']['products'] = $this->Test_model->getItems($config['per_page'], $offset);
        $page['data']['pagination'] = $this->pagination->create_links();

        $page['title']  = '';
        $page['file']   = 'test/index';

        $this->load->view('template', $page);
    }

这是对您 Codeigniter 分页有用的链接

于 2012-09-03T04:31:02.973 回答