doutan1970 2015-06-29 06:38
浏览 60
已采纳

在CodeIgniter分页中排序

The other day I managed to implement Pagination in CI using a wonderful tutorial found on the net. The next day, I noticed that Sorting was not covered in the tutorial. So I went on to implement sorting, and successfully implemented it. However, the issue arose that the pagination links did not contain the specific URI Segments related to "sortfield" and "sortorder" and hence the confusion.

If anyone knows about a way to implement sorting such that it is retained on all pages, let me know.

My test URL : http://zzz.zzz.z.zz/frog/index.php/questions/page/1/id/desc which is not retained when I click on page 2

My code:

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 );
}

Then in the view:

<?php echo $this->pagination->create_links(); ?>
  • 写回答

2条回答 默认 最新

  • doskmc7870 2015-06-29 08:25
    关注

    I did the following to get the answer:

    1. Changed the order of the parameters passed in the function

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

    2. As told by @rejoanul-alam, modified the base URL as such

      $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 );

    There was no need for getting URI segments in controller as they were already being passed in the function

    1. Changed the sort table heading in view accordingly.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化