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;
//...
}
Related
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();
}
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.
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.
Request yo to help in pagination links .
In my database i have 3 records i want to display single record per page. When I select the next numeric of pagination link, data is not being fetched.Thing is that when I click on number 2 of pagination link, echo var_dump() shows Result is empty and I am not getting any values for echo $data->email.But for the first time when i search i am able to display single record, problem is only with next link of pagination So what might be the error? I'm not able to get an answer,and I'm not sure what happens, so I am posting my code below please go through it and and help me.
Request you to help me.
**HERE STARTS MY CONTROLLER**
public function users($limit=1,$offset = 0)
{
$this->load->helper('url');
$data = array();
$look = $this->input->post('look');
$age = $this->input->post('age');
$age_from = $this->input->post('age_from');
$age_to = $this->input->post('age_to');
$se_ct = $this->input->post('sect');
$subsect = $this->input->post('subsect');
$coun_try = $this->input->post('country');
$sta_te = $this->input->post('state');
$ci_ty = $this->input->post('city');
$qualification = $this->input->post('qualification');
$results = $this->searchresultss->login($look, $age, $age_to, $age_from, $se_ct, $subsect, $coun_try, $sta_te, $ci_ty, $qualification);
$this->load->helper('url');
$config = array();
$config['base_url'] = base_url().'searchresult/users';
$config['total_rows'] = count($results);
$config['per_page'] = $limit;
$this->load->library('pagination', $config);
$data['pagination_links'] = $this->pagination->create_links();
$data['results'] = array_slice($results, $offset, $limit);
$this->load->view('searchresult', $data);
$this->load->view('includes/khelp');
$this->load->view('includes/kfooter');
**HERE STARTS MY MODEL PAGE**
Class Searchresultss extends CI_Model
{
public function login($look, $age, $age_to, $age_from, $se_ct, $subsect, $coun_try, $sta_te, $ci_ty, $qualification)
{
return $this->db->query("SELECT *
FROM users
WHERE gender = '$look'
And status='1'")->result();
}
}
**HERE START MY VIEW PAGE**
echo var_dump($_POST);
if (empty($results)) {
echo 'Results set is empty';
} else
{
foreach ($results as $data) {
echo $data->email.'<br />';
}
}
echo $pagination_links;
The problem lies in the fact that the pagination links do not include the POST variables (as well as the fact that hyper links are requested via GET).
I recommend you do a var_dump() on $_GET and $_POST and the problem will become more obvious.
A possible solution would be to include the post variables as url parameters. So for example
$config['base_url'] = base_url().'searchresult/users/look_param/age_param/etcetc';
However you would need to add functionality to handle the above.
I already using the pagination library in nearly ten modules, with no problems, but it fails in last one (and the most important).
My routing for this section is:
$route['candidate/sort/(:any)/(:any)/page/(:num)'] = 'candidate/sort/$1/$2/$3';
My Controller
public function sort($type, $id, $page = 1) {
/* Load Config */
$data = $this->data;
$data['sub_active'] = 'candidate';
$data['type'] = $type;
/* Get Candidates */
$total = $this->candidates->getTotal($type, $id);
if(($this->limit >= $total) && ($page > 1)) {
$data['candidates'] = $this->candidates->getCandidates(1, $this->limit, $type, $id);
}elseif(((($this->limit * $page) - $this->limit) >= $total) && ($page > 1)) {
$data['candidates'] = $this->candidates->getCandidates(ceil($total / $this->limit), $this->limit, $type, $id);
}else{
$data['candidates'] = $this->candidates->getCandidates($page, $this->limit, $type, $id);
}
/* Pagination */
$this->load->library('pagination');
# Config Pagination
$data['cms']['tables']['total_rows'] = $total;
$data['cms']['tables']['per_page'] = $this->limit;
$data['cms']['tables']['first_url'] = base_url($data['sub_active'].'/sort'.'/'.$type.'/'.$id);
$data['cms']['tables']['base_url'] = base_url($data['sub_active'].'/sort'.'/'.$type.'/'.$id.'/page');
$data['page'] = $page;
$data['total_pages'] = ceil($total / $this->limit);
$data['total'] = $total;
# Initialize Pagination
$this->pagination->initialize($data['cms']['tables']);
$data['pagination'] = $this->pagination->create_links();
/* Display Template */
$this->twig->display('pages/list_candidate.htm', $data);
}
Base first url = myweb.com/candidate/sort/$type/$id and base url = myweb.com/candidate/sort/$type/$id/page
But the pagination doesn’t work, it always the same page (page 1 on this case). I'm using this same schema in other controllers and it works fine, only fails with this.
Thanks in advance.
I finally found the answer:
$config['uri_segment'] = 6;
That's because codeigniter does not detect well the URL.
Nice, I am glad you found the answer Tunnecino!
But I think its need to be further explained , why
According to our wonderful CI user guide, here is how we determine the uri_segment so not to make the same mistake again :)
$this->uri->segment(n)
Permits you to retrieve a specific segment. Where n is the segment number you wish to retrieve. Segments are numbered from left to right. For example, if your full URL is this:
http://example.com/index.php/news/local/metro/crime_is_up
The segment numbers would be this:
1.news
2.local
3.metro
4.crime_is_up
By default the function returns FALSE (boolean) if the segment does not exist. There is an optional second parameter that permits you to set your own default value if the segment is missing. For example, this would tell the function to return the number zero in the event of failure:
$product_id = $this->uri->segment(3, 0);