0

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(); ?>
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
Arcanyx
  • 852
  • 10
  • 25

2 Answers2

1

All you have to change your base_url & checking URL for sorting option. Here is your controller

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 is in DESC for default soting

Rejoanul Alam
  • 5,435
  • 3
  • 39
  • 68
  • Ah! As I said, the sorting and all is working fine. I am getting the necessary output on the above URL. But the issue is that the URL on the pagination links is wrong and I can't make it work. Going by your way, I would have to change the URL...w8, I think I have the solution – Arcanyx Jun 29 '15 at 07:25
  • Your `base_url` was wrong. I have corrected it in answer. Now your pagination link must contain sorting parameter. Have you tried my code? – Rejoanul Alam Jun 29 '15 at 07:31
  • Yes, I tried your code(thanks for that), the order of URI segments is getting messed up because the parameters in the page function and hence reflecting wrongly in the pagination links. I am trying to rectify the same... – Arcanyx Jun 29 '15 at 07:34
  • as ur answer is partially correct +1 . Thanks for ur help!! – Arcanyx Jun 29 '15 at 08:26
0

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.
Arcanyx
  • 852
  • 10
  • 25