【问题标题】:Codeigniter pagination on function index功能索引上的 Codeigniter 分页
【发布时间】:2012-09-03 02:26:01
【问题描述】:

我在弄清楚了一些事情后对其进行了编辑,但如果我希望我的链接出现在索引中,这是一个好方法吗?没有功能页面,如果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 } ?>

【问题讨论】:

  • 你忘了$config['uri_segment'] = 3;
  • 我起初尝试过,然后发现将 $offset 传递给模型更容易。如果我将“功能索引”更改为“功能页面”并将 base_url 更改为“测试/页面”,它也会起作用。只是由于某种原因,它在“功能索引”中不起作用
  • $uri_segment 传递给索引并检查输出。

标签: php codeigniter pagination


【解决方案1】:

试试这个:

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;

在初始化分页配置之前。

【讨论】:

    【解决方案2】:

    在控制器中你必须更新这个

    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 pagination

    【讨论】:

      猜你喜欢
      • 2017-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-19
      • 1970-01-01
      • 1970-01-01
      • 2012-12-03
      • 1970-01-01
      相关资源
      最近更新 更多