在CodeIgniter分页中排序

时间:2015-06-29 06:38:41

标签: php codeigniter sorting codeigniter-pagination

前几天,我设法使用网上发现的精彩教程在CI中实现分页。第二天,我注意到教程中没有介绍排序。 所以我继续实现排序,并成功实现了它。但是,问题出现了分页链接不包含与" sortfield"相关的特定URI段。和" sortorder"因此混乱。

如果有人知道实现排序的方法,以便将其保留在所有页面上,请告诉我。

我的测试网址: http://zzz.zzz.z.zz/frog/index.php/questions/page/1/id/desc 点击第2页时不保留,

我的代码:

function page($offset = 0,$sortfield=null,$order=null) {
/**
* Removed all the unnecessary code
*/
    $config = array (
                    'base_url' => base_url () . 'index.php/questions/page/',
                    'total_rows' => $this->db->count_all ( 'questions' ),
                    'per_page' => $this->input->get ( 'perpage' ) ? $this->input->get ( 'perpage' ) : (@$layoutspecificpagination->per_page ? $layoutspecificpagination->per_page : 5),
                    'num_links' => 5,
                    'reuse_query_string' => true
            );

    $config ['total_rows'] = $this->questionsm->read ( $config ['per_page'], $offset, true );

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

然后在视图中:

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

2 个答案:

答案 0 :(得分:1)

所有你必须改变你的base_url&amp;检查URL以进行排序选项。这是你的控制器

function page($offset = 0,$sortfield='DESC',$order='id') {
 /**
* Removed all the unnecessary code
 */


 if ($this->uri->segment(4)) {
    $order = $this->uri->segment(4);
  }

 if ($this->uri->segment(3)) {
   $sortfield = $this->uri->segment(3);
 } 

$config = array (
                'base_url' => base_url () . 'index.php/questions/page/'.$sortfield.'/'.$order,
                'total_rows' => $this->db->count_all ( 'questions' ),
                'per_page' => $this->input->get ( 'perpage' ) ? $this->input->get ( 'perpage' ) : (@$layoutspecificpagination->per_page ? $layoutspecificpagination->per_page : 5),
                'num_links' => 5,
                'reuse_query_string' => true
        );

$config ['total_rows'] = $this->questionsm->read ( $config ['per_page'], $offset, true );

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

id在DESC中用于默认soting

答案 1 :(得分:0)

我做了以下事情来得到答案:

  1. 更改了函数

    中传递的参数的顺序

    function page($sortfield=null,$order=null,$offset = 0) {}

  2. 正如@ rejoanul-alam所述,修改基本网址

    $config = array ( 'base_url' => base_url () . 'index.php/questions/page/'.$sortfield.'/'.$order.'/', 'total_rows' => $this->db->count_all ( 'questions' ), 'per_page' => $this->input->get ( 'perpage' ) ? $this->input->get ( 'perpage' ) : (@$layoutspecificpagination->per_page ? $layoutspecificpagination->per_page : 5), 'num_links' => 5, 'reuse_query_string' => true );

  3. 不需要在控制器中获取URI段,因为它们已经在函数

    中传递
    1. 相应地更改了视图中的排序表标题。