Pagination not working on my application - CodeIgniter - php

I have been able to develop a pagination for my page which contains about 25000 records. The page paginates in 100's but for now, when i click on the pagination link to move to the next page, it leads me to the first page again. But the URI on my browser shows the per page number like customers/100 . What could i be doing wrong below
Controller
$config['base_url'] = base_url() . 'customers/index/';
$config["total_rows"] = $customers
$config["per_page"] = 100;
$config['num_links'] = 10;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['items'] = $this->customer->get_customer_all($config["per_page"]);
Model
public function get_customer_all($limit = null) {
$this->db->select('*');
$this->db->from('courses');
if($limit!=''){
$this->db->limit($limit);
}
$query = $this->db->get();
return ($query) ? $query->result() : false;
}

Why do you not use $page variable?
For getting data partially you need both limit and offset.
limit for count of customers per page and offset for understanding what page you are on.
So try to change you request to model like this:
$data['items'] = $this->customer->get_customer_all($config["per_page"], $page);
And update model like this (with some changes):
public function get_customer_all($limit = 0, $offset = 0) {
return $this->db->select('*')->
from('courses')->
limit($limit)->
offset($offset)->
get()->
result_array();
}

Related

Codeigniter pagination giving 404 error

Could you help me with my problem about pagination in Codeigniter? I have a view that lists books. I wrote some code and now I see the buttons but the content is not limited as I wished. Can't find what is wrong with the code.
That is the updated code:
Controller:
public function index($offset=0){
$config['base_url'] = 'http://localhost/myLibrary/books/index';
//$config['total_rows'] = 200;
$config['total_rows'] = $this->db->get('books')->num_rows();
$config['per_page'] = 1;
$config['uri_segment']= 3;
$config['attributes'] = array('class' => 'pagination-link');
$config['page_query_string'] = TRUE;
$this->pagination->initialize($config);
$book_list = $this->Books_model->list_books();
$genre_list= $this->Books_model->list_genres();
$author_list= $this->Books_model->list_authors();
$view_data = array(
"book_list" => $book_list,
"genre_list" => $genre_list,
"author_list" => $author_list
);
$this->db->get('books', $config['per_page'], $this->uri->segment(3));
$start = isset($_GET['start']) ? $_GET['start'] : 0;
$book_list = $this->Books_model->list_books($start, $config['per_page']);
$this->load->view("book_list",$view_data);
$this->load->library('pagination');
$this->load->library('table');
}
Model:
public function list_books($limit = FALSE, $offset = FALSE){
if($limit)
{
$this->db->limit($offset, $limit);
}
$list=$this->db->get("books")->result();
return $list;
}
View:
<?php echo $this->pagination->create_links(); ?>
I'd be so happy if you could help and thanks in advance
You can't use base_url() function as a base_url for pagination unless this index function belongs to your default site controller.
Instead change your line to this:
$config['base_url'] = site_url('controller/method/');
Also the reason your data isn't limited is because you're sending start parameter as false to model
It should be:
$data['records'] = $this->Books_model->list_books($this->uri->segment(3), $config['per_page'], $offset);
By looking at your code, the list of books being sent to the view file is stored in the $book_list variable, so in order to limit the number of books returned alter your line to this:
$start = isset($_GET['start']) ? $_GET['start'] : 0;
$book_list = $this->Books_model->list_books($start, $config['per_page']);
And add this you your pagination config array:
$config['page_query_string'] = TRUE;
And in your model function change this line:
$this->db->limit($limit, $offset);
To:
$this->db->limit($offset, $limit);
as stated in the CI documentation
$this->db->limit(10, 20); // Produces: LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)
Check here for more reference CI Limiting
Also as a side note, by looking at your code the $query and $data['records'] variables are assigned values but are never used or passed to the view file, so they don't do anything
This is how an index method in your controller should look like now:
Controller:
public function index($offset=0){
$config['base_url'] = site_url('books/index');
//$config['total_rows'] = 200;
$config['total_rows'] = $this->db->get('books')->num_rows();
$config['per_page'] = 1;
$config['uri_segment']= 3;
$config['attributes'] = array('class' => 'pagination-link');
$config['page_query_string'] = TRUE;
$this->pagination->initialize($config);
$start = isset($_GET['start']) ? $_GET['start'] : 0;
$book_list = $this->Books_model->list_books($start, $config['per_page']);
$genre_list= $this->Books_model->list_genres();
$author_list= $this->Books_model->list_authors();
$view_data = array(
"book_list" => $book_list,
"genre_list" => $genre_list,
"author_list" => $author_list
);
$this->load->view("book_list",$view_data);
$this->load->library('pagination');
$this->load->library('table');
}
Model:
public function list_books($limit = FALSE, $offset = FALSE){
if($limit !== FALSE)
{
$this->db->limit($offset, $limit);
}
$list=$this->db->get("books")->result();
return $list;
}
$config['base_url'] = 'http://example.com/controller/index/page/'
Try to add index as method name config['base_url]. so that whenever your pagination url created it contains method name.
this happens due to index method call automatically i.e. we don't need to mention method name but in pagination url we have to pass offset parameter we must have to use full url.

Codeigniter pagination do not work properly

I use codeigntier 3 pagination library.
It works, but not exactly how should.
First I will post my code.
controller setting
$config['base_url'] = base_url('items');
$config['total_rows'] = $this->ItemsM->countAll();
$config['per_page'] = 5;
$config['use_page_numbers'] = TRUE;
$config['nex_link'] = "Next";
$limit = $config['per_page'];
$offset = $this->uri->segment(2);
$this->pagination->initialize($config);
$data = array(
'musicians' => $this->ItemsM->allItems($limit, $offset),
'pagination' => $this->pagination->create_links()
);
$this->load->view('public/header');
$this->load->view('public/item-list', $data);
$this->load->view("public/footer");
Model
class ItemsM extends CI_MODEL{
function allUsers($limit="", $offset ="")
{
$query = $this->db->select('*')
->from('items')
->limit($limit)
->offset($offset)
->order_by('creation_date', 'desc')
->get();
return $query->result();
}
public function countAll(){
return $this->db->count_all_results('items');
}
Route
$route['items/(:any)'] = 'items';
This pagination works, but the results Im getting are not correct.
On the first page/tab it shows items with this order
item 1 item 2 item 3 item 4 item 5 // ok
but when open page 2
I get next 5 results this way
item 3
item 4
item 5
item 6
item 7
Honestly I do not understand where is the problem, I someone know, please help. Thank you.
It seems your problem is with varialble $offset as you are passing $offset as your page number for eg. if your page number is 2 then it will skip 2 results but you need to skip 5 results. To solve this replace this code with you controller setting.
$config['base_url'] = base_url('items');
$config['total_rows'] = $this->ItemsM->countAll();
$config['per_page'] = 5;
$config['use_page_numbers'] = TRUE;
$config['nex_link'] = "Next";
$limit = $config['per_page'];
$page = $this->uri->segment(2);
if($page > 1){
$offset = ($page-1) * $limit;
}else{
$offset = $page;
// Or you can set $offset = 0;
}
$this->pagination->initialize($config);
$data = array(
'musicians' => $this->ItemsM->allItems($limit, $offset),
'pagination' => $this->pagination->create_links()
);
$this->load->view('public/header');
$this->load->view('public/item-list', $data);
$this->load->view("public/footer");
Here i edited your variable $offset. Now you will get results as expected.

Pagination with codeigniter URI issue

This is my first time working with pagination in codeigniter and I'm a little confused. I believe my problem may have something to do with the URI segments offset variable.
Here's my controller. I have replaced the $config["base_url"] with the full URL so you can see how many URI segments I have.
My controller
$gutterId = $this->gutter->convertGutterNameToId($name);
$this->load->library("pagination");
$config = array();
$config["base_url"] = "http://localhost/gutter/g/random/"; //base_url() . "/g/$name";
$config["total_rows"] = $this->gutter->countThreadRows($gutterId);
$config["per_page"] = 1;
$config["uri_segment"] = 5;
$this->pagination->initialize($config);
$limit = 1;
$offset = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0;
$data['threads'] = $this->gutter->grabThreads($limit, $offset, $gutterId);
$data['title'] = $name;
echo $this->pagination->create_links();
And my model.
public function grabThreads($limit, $offset, $gutterId){
$query = $this->db->limit($limit, $offset)->order_by('thread_id', 'DESC')->get_where('threads', array('thread_sub_gutter' => $gutterId));
return $query->result();
}
So this is giving me one result on the http://localhost/gutter/g/random/ page which leads me to believe the query is working correctly. However, when I navigate to http://localhost/gutter/g/random/1 I get the following 404 error
The page you requested was not found.
You will need to route the request to your controller.
Should be something like this:
$route['g/(:any)/(:any)'] = 'g/index/$1/$2';
Also if your page number is going to be in the third segment, do this:
$config[‘uri_segment’] = 3;

How to create pagination with 3 argument in url using codeigniter?

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.

Pagination codeigniter not working

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']

Categories