Codeigniter pagination giving 404 error - php

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.

Related

Pagination not working on my application - CodeIgniter

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();
}

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;

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

In Codeigniter Pagination's generated page links, page 1 is always selected

I'm about to pull my hair over this!
On initial load of my page with pagination (by CI), all rows are displayed, even if I only want 3. On click of other pages, however, it works fine (the correct rows are displayed), but Page 1 is always "selected" (not clickable), even if I click on Page 2, 3, etc.
Any ideas?
My CONTROLLER:
function album($type, $album_id, $album_name) {
$this->load->library('pagination');
$config['base_url'] = base_url("photo_store/album/$type/$album_id/$album_name/");
$config['total_rows'] = $this->Media_model->get_photos($album_id, 'display_date DESC', NULL, NULL, TRUE);
$config['per_page'] = 3;
$this->pagination->initialize($config);
$album_photos = $this->Media_model->get_photos($album_id, 'display_date DESC', $config['per_page'], $this->uri->segment(6), FALSE);
$this->_load_view(array(
/* some other variables here */
'album_photos' => $album_photos
));
)
private function _load_view($more_data) {
$data = array_merge($more_data, array( /* some other variables here */ ));
$this->load->view('template', $data);
}
My MODEL:
public function get_photos($album_id=NULL, $order_by='display_date DESC', $limit=NULL, $offset=NULL, $count=FALSE) {
$result = array();
$query = $this->db->select('medium.*')->join('medium', "$this->item.medium_id = medium.id", 'inner')->order_by($order_by);
$limit = $limit ? $limit : '0';
$offset = $offset ? $offset : '0';
if ($limit!=='0' && $offset!=='0') {
$query->limit($limit, $offset);
}
if ($album_id) { $result = $query->get_where($this->item, array('album_id' => $album_id)); }
else { $result = $query->get($this->item); }
if ($count){ return $result->num_rows(); }
else { return $result->result(); }
}
My VIEW:
foreach ($album_photos as $photo) {
//display photos here
}
echo $this->pagination->create_links();
You can just add this to the config array so the pagination knows where to find the current page:
$config['uri_segment'] = 4;
I believe part of the problem is coming in here:
if ($limit!=='0' && $offset!=='0') {
$query->limit($limit, $offset);
}
Since you don't have an else part for your statement, the query is never limited for that first page.
I suggest you change that code to
if ($limit!=='0') {
$query->limit($limit, $offset);
}
or even just
$query->limit($limit, $offset);
since $limit should theoretically never be null or 0 because you've set it to 3. $offset, unless set, should be 0 so you could replace null with it in your model's function,
When there are more than 3 uri segments passed in the url, selected page of pagination will not be displayed correctly, it will highlight the first page all the time.
Pagination is working, but the selected page is not diplayed correctly.
To solve this, solution:
go to Pagination.php file which is located at system->libraries->Pagination.php
just simply set
var $uri_segment = 4;// or 5 or 6;
It will work.
You can just add this to the config array so the pagination knows where to find the current page:
$config['uri_segment'] = 4; // Your appropriate uri segment: 5 or 6
Try to code your controller like below:
public function index($page=''){
//...
$page = ($page!='')? $page : 0;
$config["cur_page"] = $page;
//...
}

Categories