How to add pagination codeigniter ?
I tryed to use like this link, but it doesn't work
https://www.cloudways.com/blog/pagination-in-codeigniter/
thank you for help me.
Pagination is one of the libraries in the codeigniter...
$this->load->library('pagination');
$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
you can get reference from here... https://www.codeigniter.com/userguide3/libraries/pagination.html
You can also manage pagination manually, it can be easier sometimes depending on what you do: let's say you want batches of 100 results per page, pass a page_number parameter to your controller, then
$results_limit = 100;
$offset = 0;
if (isset($_GET['page_number']))
$offset = ($_GET['page_number']-1) * $results_limit;
$this->db->select('*');
$this->db->from('sltax_tax_name');
$this->db->order_by("t_id", "desc");
$this->db->limit($offset, $results_limit);
$query = $this->db->get();
// grab your results...
It is easier to use Pagination class from CodeIgniter rather than doing it manually.
https://www.codeigniter.com/userguide3/libraries/pagination.html
In Controller
$this->load->library('pagination');
$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
In the view
echo $this->pagination->create_links();
Related
In developing the site on codeigniter (ver.3), an error occurs: pagination does not work correctly. This only happens if you set the pagination at the fourth URL segment.
My controller (in class Main)
public function category($id=NULL){
$data['category'] = $this->articles_model->get_category($id);
$this->load->library('pagination');
$config['base_url'] = base_url().'main/category/'.$id.'/';
$config['total_rows'] = $this->articles_model
>count_all_articles();
$config['per_page'] = 10;
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$this->pagination->initialize($config);
$data['articles'] = $this->articles_model- >get_articles($config['per_page'],$page,$id);
$data['links'] = $this->pagination->create_links();
}
Links like work but highlighted only the first page. on the second and subsequent page, the first page is also highlighted.
Just tell it to use the 4th segment ...
$config['uri_segment'] = 4;
This is the very first option shown in the documentation.
I was having the same problem but i solved it, and it is working well:
//u need to get the category id from the segment #3
$catid = $this->uri->segment(3);
//here is the segment of pagintation
$config["uri_segment"] = 4;
/*
here is the essantial work, where you should add the category id in the last of the base url
*/
$config["base_url"] = base_url() . "home/category/$catid";
$route['allgrant'] = "grant/allgrant";
The above is working fine for the URL: http://localhost/grant/allgrant
But I am getting error with URL http://localhost/grant/allgrant/6
My route setting is below:
$route['allgrant/(:num)'] = "grant/allgrant/$1";
My controller code:
public function allGrant()
{
$this->load->library('pagination');
$config = array();
$config["base_url"] = base_url('allGrant');
$config['total_rows'] = $this->db->count_all("grant_data");
$config['per_page'] = 3;
$config["uri_segment"] = 2;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$data["allGrant"] = $this->grant_model->allGrant($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$data['mainContent'] = 'viewGrant';
$this->load->view('include/template',$data);
}
when I load the all grant page it show me the first three result but when I click on the pagination it give error with page not found
Try this one add this route in your file
$route['grant/allgrant/(:num)'] = "grant/allgrant/$1";
Change from
$route['allgrant/(:num)'] = "grant/allgrant/$1";
To
$route['allgrant/(:any)'] = "grant/allgrant/$1";
Settings missing with .htaccess, and it is not able to parse the last parameter in URL
I'm using Codeigniter 2.2 and I try to build pagination ask Codeigniter guide and it is work perfectly for me when I used as url below
$pages_num: is the amount of pages for view.
http://localhost/Codeigniter2.1.4/public_html/page/$pages_num
But I get error when I add one argument for url
as code below I try to find it 3 weeks ago But I have not solution Please
Notes: number 2 is id categories and number 4 is amount of pagination view
http://localhost/Codeigniter2.1.4/public_html/cat/2/4
Here is my code in controller
$this->load->model('frontend/categories_m');
$count = $this->db->count_all_results('job');
$perpage = 2;
if ($count > $perpage) {
$this->load->library('pagination');
$config['base_url'] = site_url('cat/2');
$config['total_rows'] = $count;
$config['per_page'] = $perpage;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
$offset = $this->uri->segment(3);
} else {
$this->data['pagination'] = '';
$offset = 0;
}
$this->db->limit($perpage, $offset);
$this->data['job_cat'] = $this->job_m->get_job();
$this->data['subview'] = 'cat';
$this->load->view('_main_layout', $this->data);
Here is for view:
<?PHP if ($pagination): ?>
<section class="pagination">
<?PHP echo $pagination; ?>
</section>
<?PHP endif; ?>
Please help
Sorry I can't post images here
With codeigniter pagination, the default link structure is as below
http://website/pages/getPagesViaAjax/3/5
Here pages is controller, getPagesViaAjax is function inside it and 3 is a parameter for category id and 5 will be the pagination count(i.e. offset)
in this case my pagination config is something like
$config['base_url'] = site_url('getPagesViaAjax/3');
$offset = $this->uri->segment(4);
So basically your offset will be the last argument and i.e. 5(available at segment 4) in this case. So prior to firing your query to model, please check the offset.
In my application i'm allowing user to write a blog. Now there is a universal homepage which shows all blogs by all user and it's pagination code is
$this->load->library('pagination');
$config['base_url'] = "http://localhost/old/lostfoundproject/index.php/welcome/index";
$config['per_page'] = 3;
$config['num_links'] = 10;
$config['total_rows'] = $this->db->get('table_name')->num_rows();
$this->pagination->initialize($config);
$this->load->model('welcome_model');
$data['records'] = $this->welcome_model->get_all_blogs($config['per_page'],$this->uri->segment(3));
$this->load->view('welcome_message',$data);
which is working perfectly but i don't know what is the problem with the user pagination function where i've made just a little change of select just those blogs which are from that user
$this->load->library('pagination');
$config['base_url'] = "http://localhost/old/lostfoundproject/index.php/user/page/user_home/";
$config['per_page'] = 3;
$config['num_links'] = 10;
$config['total_rows'] = $this->db->get_where('table_name',array('user_id'=>$this->session->userdata('some_id')))->num_rows();
$this->pagination->initialize($config);
$this->load->model('user_model');
$data['records'] = $this->user_model->get_all_blogs($config['per_page'],$this->uri->segment(4));
$this->load->view('user/user_home',$data);
now the pagination is working perfectly for the welcome screen but not for user screen
You also need to provide the below to tell the pagination class what is the current offset:
$config["uri_segment"] = $this->uri->segment(4);
Please replace $this->uri->segment(4) to $this->uri->rsegment(3); or you can check this link also http://ellislab.com/codeigniter/user-guide/libraries/uri.html
pagination is working but i dont know Why its showing only one data per page?? and i hv 20 data in my db and i can see only 6 data in 6pages :(
This is my controller
function view($page=0){
$config = array();
$config["base_url"] = base_url() . "index.php/view_expenses/view";
$config["total_rows"] = $this->emp_expenses_model->getTotalExpensesCount();
$config["per_page"] =1;
$this->pagination->initialize($config);
$this->data["results"] = $this->emp_expenses_model->getExpenses($config["per_page"], $page);
$this->data["links"] = $this->pagination->create_links();
$this->data['title'] = 'Payroll System';
$this->data['message'] = $this->session->flashdata('message');
$this->load->view('view_expenses', $this->data);
}
This is my model
function getTotalExpensesCount() {
return $this->db->count_all("emp_expenses");
}
function getExpenses($limit, $start) {
$this->db->limit($limit, $start);
$qry= $this->db->get("emp_expenses");
return $qry->result();
}
Any help thanks in advance :D
Try this:
function view(){
$config = array();
$config["base_url"] = base_url() . "index.php/view_expenses/view/".$this->uri->segment(3);
$config["total_rows"] = $this->emp_expenses_model->getTotalExpensesCount();
$config["per_page"] = 5;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$this->data["results"] = $this->emp_expenses_model->getExpenses($config["per_page"], $this->uri->segment(3));
$this->data["links"] = $this->pagination->create_links();
$this->data['title'] = 'Payroll System';
$this->data['message'] = $this->session->flashdata('message');
$this->load->view('view_expenses', $this->data);
}
function getExpenses($limit, $start = 0) {
$qry= $this->db->get("emp_expenses", $limit, $start);
return $qry->result();
}
$config["per_page"] = 5; means that you want to show 5 data per page, $config['uri_segment'] = 3; says what segment will hold the offset which you will be using in your query. $config["total_rows"] defines the total no. of rows in your table. $config["base_url"] defines the url the pagination will hold.
The data you are showing in your view is no way related to the pagination. Check your query and the offset you are getting. E.g. place echo $this->db->last_qeury();die; in function getExpenses() just before return statement.
You need to pass the uri segment like
$config["uri_segment"] = $last_seg_no;
Here $last_seg_no will be the last segment where you can find the page number.And need to pass the per page param also like
$config["per_page"] = 1;//As per your case
"dont know Why its showing only one data per page??" because of $config["per_page"] =1;.
Add the following,
$config['uri_segment'] = 3; // change based on ur url
Change the value from $config["per_page"] =1; to $config["per_page"] =3; to display 3 data per page.
Try this
$config['uri_segment'] = 3;
$config["per_page"] = 3;
Modifiy modal function like this
function getTotalExpensesCount() {
$this->db->select("count(*) as CNT");
$qry = $this->db->get("emp_expenses");
$result2 = $qry->row()->CNT;
return $result2;
}
i have faced this same problem. Now it solved by changing this
$config['uri_segment']