Wednesday 30 October 2013

Codeigniter Pagination problem to locate the current page (if using CI default Library), AJAX Pagination Calls

If you are using default CI Pagination library then it would be chances of displaying the current page active although results status is displaying correctly.

In particular, when you are using config/routes.php to rewrite the URLs.

Well, if that case, you can resolve that issue by adding the uri_segment's value while initializing the pagination as below;

PS:  First find which segment of URI carries the page value, then add the segment value (not page value coz once you provide segment value then, it will use it to pull out the page value)
// Locate the uri_segment to locate the current page value.
$pagination['uri_segment'] = 3; 
Also, if you are wondering how to find the uri_segment value, then use below code to print all uri_segments array
$segments = $this->uri->segment_array();
print_r($segments);
Please find the sample pagination code as below;
$pagination = array();
$pagination['start']  = $start+1;
$pagination['limit']  = $limit;
$pagination['per_page'] = $limit;
$pagination['ipage']  = $ipage;
$pagination['total_rows'] = $this->m_yourmodel->function_result_count($where=array()); // only pull the approved ones.
$pagination['anchor_class'] = 'class="loadAjaxPage" '; // This class is added to make a AJAX Call if you want to load the pages via AJAX
$pagination['offset']  = ($start+$limit); // This is just for the View;

$pagination['uri_segment'] = 3; // Locate the uri_segment to locate the current page value.

$this->load->library('pagination');
$this->pagination->initialize($pagination);
a$pagination['page_links'] = $this->pagination->create_links();

To Load the Page from AJAX Call
Also I have added the loadAjaxPage class name on anchor_class pagination variable to load the page from AJAX calls.
// This class is added to make a AJAX Call if you want to load the pages via 
$pagination['anchor_class'] = 'class="loadAjaxPage" ';

1 comment:

  1. I found the problem thanks to uri_segment detection.
    Thank you RC Adhikari

    ReplyDelete

Please post any queries and comments here.