Pagination is a piece of cake in codeigniter but adding sorting columns is one heck of a task. This is how i did it
The Controller
<?php
class View extends Controller {
function view() {
parent::Controller();
$this->load->helper('url');
$this->load->library('pagination');
}
function orders() {
//page title
$data['title']='Page Title';
//pagination configurations
$config['total_rows'] = $this->db->get('dbname')-> num_rows();
$config['per_page'] = '50';
//get all the URI segments for pagination and sorting
$segment_array=$this->uri->segment_array();
$segment_count=$this->uri->total_segments();
//for ordering the data items
$do_orderby = array_search("orderby",$segment_array);
//asc and desc sorting
$asc = array_search("asc",$segment_array);
$desc = array_search("desc",$segment_array);
//get the records
$this->db->order_by($this->uri->segment($do_orderby+1), $this->uri->segment($do_orderby+2));
//getting the records and limit setting
if (ctype_digit($segment_array[$segment_count])) {
$data['page']=$segment_array[$segment_count];
$this->db->limit($config['per_page'], $segment_array[$segment_count]);
array_pop($segment_array);
}
else {
$data['page']=NULL;
$this->db->limit($config['per_page']);
}
$config['base_url'] = site_url(join("/",$segment_array));
$config['uri_segment'] =count($segment_array)+1;
$data['records'] = $this->invoice_model->getInvoiceList();
//initialize pagination
$this->pagination->initialize($config);
//load the view
$this->load->view('invoice',$data);
}
}
The View
Writing the view would have been very easy without the sorting, just add the table class and tabulate the data. For sorting there has to be some extra steps taken. I’ll show just a part of the code
<?php // For Pagination echo $this->pagination->create_links(); ?>
<?php
// For Sorting
//default in descending order
$sort['col1']='desc';
$sort['col2']='desc';
$sort['col3']='desc';
//get the segment array
$segment_array=$this->uri->segment_array();
//search for the orderby string
$do_orderby = array_search("orderby",$segment_array);
//check to toggle asc and desc sorting in columns
if($do_orderby !== FALSE) {
$sort[$segment_array[$do_orderby+1]]= $segment_array[$do_orderby + 2] == 'desc' ? 'asc' : 'desc' ;
}
?>
I will not discuss how to create the table. For linking the table header for sorting, check out the example below
<?php
echo "<a href=\"".$url."/orderby/col1/{$sort['col1']}/".$page."\">";
?>
Col1</a>;
That would do the job. Questions, suggestions, problems, comments ? if any, do use the box below.
Note : Since there is a lot of traffic in this page, i have decided to write some more posts regarding codeigniter, please comment on this post if u have any suggestions or any particular topic to cover
Similar post in StudioUltimate. Next up is codeigniter with backbone.paginator. keep checking StudioUltimate for new awesome posts.
Oh… Thank you for great help. I never seen better blog.
thank you so much!
Pingback: 8 Best Tutorials On ‘How To Create Pagination In Codeigniter’
Thx..this is very helpfull…
Great tutorial! It saves my time figuring how to do it on my own.. Thanks!!
Great post, and you’re right, adding a simple sort can really increase the difficulty of an otherwise simple task… but it’s worth it. :)
Thanks Avinash. That is exactly what I am looking for. It is working like a charm.