Pagination past limit in codeigniter - codeigniter

The code listing below is in a function in a message controller. I want that a page to only have 5 records at a time on a page. Using pagination class in codeignuter i have set the limits meaning that if a page has only three records then in the pagination link in the view only one is shown e.g < 1 and not < 1 2. This also happens with the last page, if the pages are 8 it shows 9 thus <... 8 9. What am i missing
$data['message_available'] = $this->getCheckForMessages();
$config["base_url"] = base_url() . "messages/inbox/";
$config['total_rows'] = $this->getMessageCount();
$config['per_page'] = 5;
$config["num_links"] = 10;
$config["full_tag_open"] = '<div id="pagination">';
$config["full_tag_close"] = '</div>';
$this->pagination->initialize($config);
$data['messages'] = $this->getMessages($config['per_page'], $this->uri->segment(3));
$data['links'] = $this->pagination->create_links();

You have to check deeply this :
$config['total_rows'] = $this->getMessageCount();
maybe is returning some field more than the Results query getMessages();
please post also your getMessages() and getMessageCount(); queries to be more accurated
also please change this:
$config["base_url"] = base_url() . "messages/inbox/";
to this:
$config["base_url"] = site_url('messages/inbox');

Related

Need understanding $config["uri_segment"] related code in CodeIgniter

Hey CodeIgniter developers I am new in codeIgniter please see my code related to pagination. Pagination is working fine. I just need your help to understand few lines of code, please see the commented lines in code where I just need your help to understand it.
public function example1() {
$config = array();
$config["base_url"] = base_url() . "welcome/example1";
$config["total_rows"] = $this->services->record_count();
$config["per_page"] = 10;
$config["uri_segment"] = 3; // Need help on this line
$config["next_link"] = '>';
$config["prev_link"] = '<';
$this->pagination->initialize($config);
// Need help on this if condition blocks
if ($this->uri->segment(3)) {
$page = ($this->uri->segment(3));
} else {
$page = 1;
}
$data["results"] = $this->services->fetchServicesByPagination($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view("example1", $data);
}
Agree With Javier Larroulet
From the Codeigniter documentation $config['uri_segment'] defines what URI segment will contain the page number. It defaults to 3, but you may use another segment if you need. The $this->uri->segment(3) condition on the if clause is checking if the URI Segment number 3 (first after the method name) is set or not. If it is set, use its value as page number, otherwise, default to page 1. Reference

Codeigniter pagination is not passing the correct link value in url

I have created a pagination in codeigniter, In that I have totally 13 records and i want to display 5 records in each page. When i click on the pagination link number 2,It should pass value 2 in url and it show the next 5 records but I am getting value 5 in url instead of 2.
My controller code:
public function activities($page_num = 1){
$config =array();
$config['base_url'] = base_url().'admin/skills/activities';
$config['per_page'] = 5;
$config['total_rows'] = $this->skill_model->all_activities(0,'',''); // I will get the total count from my model
if(empty($page_num))
$page_num = 1;
$limit_end = ($page_num-1) * $config['per_page']; //end limit
$limit_start = $config['per_page']; // start limit
$this->pagination->first_url = $config['base_url'].'/1';
$this->pagination->initialize($config);
$data['activity_list'] = $this->skill_model->all_activities(1,$limit_start,$limit_end);
}
This is my view code:
<?php echo '<div class="pagination" style="float:right;">'.$this->pagination->create_links().'</div>'; ?>
My Model part:
function all_activities($flag,$limit_start,$limit_end){
$this->db->select('*');
$this->db->from('skills_activities');
//echo $limit_start." ".$limit_end;
if($flag == 1 ){
$this->db->limit($limit_start, $limit_end);
$query = $this->db->get();
return $query->result_array();
} else {
$query = $this->db->get();
return $query->num_rows();
}
}
When i click on pagination link I am getting the following url:
http://192.168.1.97/projects/homecare/admin/skills/activities/5
but I should get:
http://192.168.1.97/projects/homecare/admin/skills/activities/2
I don't know where i have done a mistake.
can anybody help me?
Thanks in advance.
$config['use_page_numbers'] = true;
This will produce page number in the url instead of record number. By default its false.
So you controller will look like this
public function activities($page_num = 1)
{
$config =array();
$config['base_url'] = base_url().'admin/skills/activities';
$config['per_page'] = 5;
$config['use_page_numbers'] = true;//you missed this line
$config['total_rows'] = $this->skill_model->all_activities(0,'',''); // I will get the total count from my model
if(empty($page_num)) $page_num = 1;
$limit_end = ($page_num-1) * $config['per_page']; //end limit
$limit_start = $config['per_page']; // start limit
$this->pagination->first_url = $config['base_url'].'/1';
$this->pagination->initialize($config);

codeigniter's pagination offset not working

In CI's pagination, the data index should follow the offset. For example : if the limit is 10 then the 2nd index should have 10 offset, which means the index will start from 11 to 20.
I followed some tutorials, but i still cant get this offset thing works correctly. My index always reseted to 1 each time i click the differet pagination index.
This is my pagination code, note that i have tried to echo the $offset and the value is true (in 2nd index = 10, in 3rd index = 20, with $limit = 10) so i dont know why its not working:
public function index($offset = 0) {
//check authorization
if(!isset($_SESSION['username']))
redirect('backend_umat/login');
// the $offset value is true, but the index is still reseted to 1
echo $offset;
$limit = 10;
$result = $this->umat_m->get_umat($limit, $offset);
//pagination
$config['base_url'] = site_url('/backend_umat/index');
$config['total_rows'] = $result['num_rows'];
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
And this is my model :
public function get_umat($limit, $offset) {
$this->db->select('*')->from('msumat')->limit($limit, $offset)->
join('mskelas', 'msumat.kelas_id = mskelas.kelas_id');
$q = $this->db->get();
$result['rows'] = $q->result();
$result['num_rows'] = $this->db->select('*')->from('msumat')->
join('mskelas', 'msumat.kelas_id = mskelas.kelas_id')
->get()->num_rows();
return $result;
Thanks for your help :D
public function index() {
//check authorization
if(!isset($_SESSION['username']))
redirect('backend_umat/login');
// the $offset value is true, but the index is still reseted to 1
//pagination
$config['base_url'] = base_url().'backend_umat/index';
// basically you need a separate query to return only numrows
$config['total_rows'] = ?;
$config['per_page'] = 10;
$config['uri_segment'] = 3;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$offset = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$result = $this->umat_m->get_umat( $config['per_page'], $offset);
$data['pagination'] = $this->pagination->create_links();
Here try this, i changed some code hope this works. Basically i intitialized first the pagination config, before calling anything from the model. Just do a separate query to get the numrows.
Controller
site.com/<controller>/index/<page>
public function index($offset = 0) {
//check authorization
if(!isset($_SESSION['username']))
redirect('backend_umat/login');
$limit = 10;
$offset = (int) $offset;
$result = $this->umat_m->get_umat($limit, $offset);
//pagination
$config['base_url'] = site_url('/backend_umat/index');
$config['total_rows'] = $result['num_rows'];
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
Model
public function get_umat($limit, $offset) {
$result['rows'] = $this->db
->select('SQL_CALC_FOUND_ROWS, msumat.*, mskelas.*', FALSE)
->limit($limit, $offset == 1 ? 0 : $offset)
->join('mskelas', 'msumat.kelas_id = mskelas.kelas_id')
->get('msumat')
->result();
$req = $this->db->query('SELECT FOUND_ROWS()')->row_array();
$result['num_rows'] = $req['FOUND_ROWS()'];
return $result;
}
To not have to rewrite a second sql req, you can use SQL_CALC_FOUND_ROWS to retrieve the total if the request did not contain a LIMIT statement. But this can be slower than two requests.
I used FALSE as second arguments in the select() in order that CI not tries to protect field or table names with backticks.
->select('*'): is useless if you want all fields, CI will do it by default if select method was not called.
->get()->num_rows(): use instead ->count_all_results([table]).
First, thanks to tomexsans and lighta for their help. Sorry, i made a "stupid" mistake - which took about 2-3 hours to realize -, the index is not following the $offset because i FORGET to use that variable. I use an integer that i reset to 1 everytime the page load, so the index will reset to 1 forever :P

How to fix codeigniter pagination and route

I have problem whit codeignitet pagination and route!
I set the route my function class like this:
$route['admin/panel/students/new-stuedents-list/:num'] = "admin/newStuedentsList/$1";
then in my controller, I create a function that call newStuedentsList and load pagination library, every things work fine but pagination nav... :(
the page loaded successfully...
and the data is correct...
bat when I click for example page 2 , the page 2 loaded successfully but the pagination nav show page i button ...!
when I call page 4 form url (http:// localhost/d/index.php/admin/panel/students/new-stuedents-list/30) , again the data is correctly ... but the pagination nav show page 1 button and the number of page don't change!
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/d/index.php/admin/panel/students/new-stuedents-list/';
$config['total_rows'] = $this->db->get('new_contest')->num_rows();
$config['pre_page'] = 10;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$adminInfo = $this->admin_model->adminInfo();
$newStudentsList['students'] = $this->admin_model->newStudentsList($config['pre_page'],$this->uri->segment(5));
$data = array_merge($adminInfo,$newStudentsList);
$this->load->view('admin/newStudentsList',$data);
but when the newStuedentsList is:
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/d/index.php/admin/newStuedentsList/';
$config['total_rows'] = $this->db->get('new_contest')->num_rows();
$config['pre_page'] = 10;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$adminInfo = $this->admin_model->adminInfo();
$newStudentsList['students'] = $this->admin_model->newStudentsList($config['pre_page'],$this->uri->segment(3));
$data = array_merge($adminInfo,$newStudentsList);
$this->load->view('admin/newStudentsList',$data);
every things work fine!
How do I fix this problem ...?
You forgot one configuration:
$config['uri_segment'] = 5;
To determines which segment of your URI contains the page number.
And replace pre_page by per_page

Pagination in codeigniter

Help me my url in pagination ! :(
$data['total'] = $this->news_model->all_list();
$this->load->library('pagination');
$config['base_url'] = site_url().'admin/news/page';
$config['total_rows'] = count($data['total']);
$config['per_page'] = '5';
$this->pagination->initialize($config);
$data['columns'] = array('Tile', 'Date', 'User', 'Active', 'Edit', 'Delete');
$data['list'] = $this->tintuc_model->all_list($config['per_page'],$this->uri->segment(3));
$data['pagination'] = $this->pagination->create_links();
$this->smarty->view( 'admin/news.tpl', $data );
That's my code. List of pages showed ok. But, when all links are wrong.
eg: when i click the 2 page link => http://localhost/mysite/admin/news/page/5 ?
Why it is 5 instead of 2 ?
you can change
$config['per_page'] = '5';
to
$config['per page'] = '2';
in order to get 2 in your uri segment, but it indicates the limit of data to be displayed in your page not the clicked pagination number.
It's not the page number - it's page number * items per page
In your case $config['per_page'] = '5'; So everything seems to be okay.
You need to set:
$config['use_page_numbers'] = TRUE;
http://ellislab.com/codeigniter/user-guide/libraries/pagination.html

Resources