I am pulling my hair out on this one. I am using CodeIgniters pagination library and right now it is always stuck on page 1 as the current page. I have checked a bunch of StackOverflow questions and I don't have the same problem as anyone else.
Here is my url structure
website.com/leaders/page/[page_number]
Here is the pagination code in my controller
$this->load->library('pagination');
$config['per_page'] = $query_config['limit'];
$config['base_url'] = base_url() . 'leaders/page/';
$config['total_rows'] = 2000; // I actually use a function for this number
$config['full_tag_open'] = '<div id="paginate">';
$config['full_tag_close'] = '</div>';
$config['first_link'] = '« First';
$config['last_link'] = 'Last »';
$config['use_page_numbers'] = true;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
When I echo the pagination in the view it looks like it works. The urls on each link is correct and everything looks fine. The last link shows the last page url and the current page is 1. However when I click on page 2 or any other page from the pagination, it still shows page 1 as the current page even though the url is as follows
website.com/leaders/page/2
I used the $this->uri->segment(3) to grab the page number for my database queries so the page number is in the right segment. Just to double check I set the $config['uri_segment'] values to 1,2,3,4,5,6 just to make sure.
I found out the problem while writing this but I am still confused
Then I thought maybe there is something going on with the url itself as I have a route directing it to the index method in the controller. Here is what my routes file looks like
routes.php
$route['leaders/page/(:num)'] = 'leaders/index';
$route['leaders/page'] = 'leaders/index';
Then I tried setting the base_url for the pagination config to send it to the index directly like so:
$config['base_url'] = base_url . 'leaders/index';
Now it seems to be working properly. But how do I make it so that it works with the url structure I had before? I just think it looks nicer and I don't really need a method in the controller for this. Is there something conflicting in my routes.php file?
Thanks
define cur_page and define controller like :
public function index($page=''){
//...
$page = ($page!='')? $page : 0;
$config["cur_page"] = $page;
//...
}
Use this in your code, hope this will work-
if ($this->uri->segment(3) > 0) {
$offset = $this->uri->segment(3) * $config['per_page'] - $config['per_page'];
} else {
$offset = $this->uri->segment(3);
}
I've been working for about 6 hours to make CI pagination work as I expected, and I don't know if is the order of the config elements or just my browser joking with me.
Below is my configuration array for pagination to work properly.
As you can see this is normal code, but my problem was, when I rendered for the first time, my pagination view, all seems to be ok, but if $config['per_page'] = 10; was set to 10, when I clicked on 11 link of pagination links, link number 2 showed Cseguimiento/buscar_reportes/# and looks like current page was 2 not 11.
I was very tired and I started to change the order of $config array, and suddenly it worked. SO I preesnt it here.
$config['base_url'] = base_url().'Cseguimiento/buscar_reportes/';
$config['uri_segment'] = 3;
$config['use_page_numbers'] = TRUE;
$config['first_link'] = FALSE;
$config['last_link'] = FALSE;
$config['next_link'] = '>';
$config['prev_link'] = '<';
$config["full_tag_open"] = '<ul class="pagination">';
$config["full_tag_close"] = '</ul>';
$config["first_tag_open"] = '<li>';
$config["first_tag_close"] = '</li>';
$config["last_tag_open"] = '<li>';
$config["last_tag_close"] = '</li>';
$config["next_tag_open"] = '<li>';
$config["next_tag_close"] = '</li>';
$config["prev_tag_open"] = "<li>";
$config["prev_tag_close"] = "</li>";
$config["cur_tag_open"] = "<li>";
$config["cur_tag_close"] = "</li>";
$config["num_tag_open"] = "<li>";
$config["num_tag_close"] = "</li>";
$config['total_rows'] = $this->mseguimiento->filas($fecha_inicio,$fecha_fin);
$config['per_page'] = 10;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = round($choice);
$page = $config['uri_segment'] * $config['per_page'];
$this->pagination->initialize($config);
$offset = ($this->uri->segment(3)-1)*$config['per_page'];
$output = array(
'pagination_link' => $this->pagination->create_links(),
'lista_reportes' => $this->mseguimiento->fetch_details($this->pagination->per_page, $offset,$fecha_inicio,$fecha_fin)
);
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";
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.
My current product pagination links look like this:
http://ecommerce.site/products/category/sub-category/page/5/per_page/9
I would like to build them like this:
http://ecommerce.site/products/category/sub-category?page=5&per_page=9
So as to separate pagination from the URL and be able to tell search engines what to do with each parameter.
My current pagination config is this:
$config['per_page'] = $per_page;
$config['base_url'] = base_url().'/products/'.$data['category']->slug.'/'.$data['sub-category']->slug.'/';
$config['prefix'] = 'page/';
$config['suffix'] = ($per_page != 9 ? '/per_page/'.$per_page : '').'?'.http_build_query($_GET, '', "&");
$config['uri_segment'] = '4';
$config['use_page_numbers'] = TRUE;
$config['num_links'] = '3';
$config['prev_link'] = '‹';
$config['next_link'] = '›';
$config['first_link'] = '«';
$config['last_link'] = '»';
$config['total_rows'] = $data['products_total'];
Edit
kumal_v suggested to use the page_query_string parameter which I had missed in CodeIgniter's pagination documentation, I've answered the question with my current config.
This is what I ended up doing
Enabled query strings for pagination and changed the parameter that contains the page number (by default it is set to 'per_page')
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'page';
Set the base URL for the pagination links
$config['base_url'] = base_url().'/products/'.$data['category']->slug.'/'.$data['sub-category']->slug;
I use additional parameters which I preserve in the pagination URLs, this is to avoid recursively including the page parameter. Also it will keep the parameters for the first_url which by default will have none.
$query_string = $_GET;
if (isset($query_string['page']))
{
unset($query_string['page']);
}
if (count($query_string) > 0)
{
$config['suffix'] = '&' . http_build_query($query_string, '', "&");
$config['first_url'] = $config['base_url'] . '?' . http_build_query($query_string, '', "&");
}
Set the rest of the pagination config
$config['per_page'] = $per_page;
$config['uri_segment'] = '4';
$config['use_page_numbers'] = TRUE;
$config['num_links'] = '3';
$config['prev_link'] = '‹';
$config['next_link'] = '›';
$config['first_link'] = '«';
$config['last_link'] = '»';
$config['total_rows'] = $data['products_total'];
Optional Modify / extend the pagination library to use '?' instead of '&'.
Replace this line
$this->base_url = rtrim($this->base_url).'&'.$this->query_string_segment.'=';
With this line
$this->base_url = rtrim($this->base_url).'?'.$this->query_string_segment.'=';
This works in this case because I know 'page' will always be the first parameter as I'm not using enable_query_strings.
If you don't want to modify / extend the pagination library, you will have to add the '?' to the $config['base_url']. The pagination URLs will have both '?' and '&' like '?&page=6'.
Sources
kumar_v's answer
CodeIgniters pagination documentation
Preserving GET URL parameters
Getting pagination to use '?' instead of '&'
First enable query string in pagination
$config['page_query_string'] = TRUE;
$config['enable_query_strings'] = TRUE;
Then change pagination url sufix as:
$config['suffix'] = "?page=".$yourcurrent_page_no."&per_page=".$per_page;
i have a page where books are to be display either with "grid" view or with "list" view.
So my current url is
http://localhost/thebestbookfinder.com/viewallbooks/books/pgn/grid/8
and if i click on "list" link from my view page then my current page should load with only changing this part /grid/ to /list/ and other same.
i.e http://localhost/thebestbookfinder.com/viewallbooks/books/pgn/list/8
I tried this but this is not helpful. Please help me to solve this issue.
inMyViewPage.php
<td width="10%" style="font-size:12px;color: red;">
<?php
echo "".anchor('#', 'Grid', array('id' => ''))."";
echo "".anchor(base_url().'viewallbooks/books/pgn/'.$this->uri->segment(4),' List', array('id' => ''))."";
?>
</td>
Instead of link if form submition solve my problem then please help me with that
Controller.php
function books()
{
$config = array();
$config['base_url'] = base_url().'viewallbooks/books/pgn/'.$this->uri->segment(4);
$config["total_rows"] = $this->Booksmodel->record_count_for_secondtopBooks('3');
$config['uri_segment'] = 5;
$config['per_page'] = 8;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0;
$data["query"] = $this->Subjectmodel->get_subjects();
$data["query1"]= $this->Editionmodel->get_edition();
$data["query2"]=$this->Booksmodel->get_all_book_list_atHomeTop('3',$config["per_page"], $page);
$viewType = ($this->uri->segment(4)) ? $this->uri->segment(4) : 'list';
if($viewType=='list')
{
$this->load->view('commonfiles/bookslistview',$data);
}
else {
$this->load->view('commonfiles/booksgridview',$data);
}
}
Need to add route in route.php like this
$route['viewallbooks/books/(:any)'] = "viewallbooks/books/$1";
Function books() shold contain 3 params with values by default
Below is more correct varian
function books($img_type = 'png', $view_type = 'list', $page = 0) {
$config = array();
$config['base_url'] = base_url().'viewallbooks/books/'.$img_type.'/'.$view_type.'/'.$page;
$config["total_rows"] = $this->Booksmodel->record_count_for_secondtopBooks('3');
$config['uri_segment'] = 5;
$config['per_page'] = 8;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data["query"] = $this->Subjectmodel->get_subjects();
$data["query1"]= $this->Editionmodel->get_edition();
$data["query2"]= $this->Booksmodel->get_all_book_list_atHomeTop('3',$config["per_page"], $page);
if($viewType=='list')
$this->load->view('commonfiles/bookslistview',$data);
else
$this->load->view('commonfiles/booksgridview',$data);
}
Do this one:
<?php echo "".anchor(base_url().'viewallbooks/books/pgn/grid/'.$this->uri->segment(5),' Grid', array('id' => ''))."";
echo "".anchor(base_url().'viewallbooks/books/pgn/list/'.$this->uri->segment(5),' List', array('id' => '')).""; ?>
And no need to change anything in above controller.
You will have to do either:
Reload the page with the new url (grid) to render that template
OR:
Render both the grid and list view and then dynamically (with some css/javascript) hide content based on what you'll want to see.
OR:
Load the grid-view with AJAX and replacing the list with it.
I am using the paging class of the codeigniter for the first time. Everything else is working fine, but whichever page I am going, the active link is always the [1] and it doesn't change. Also the next button is always linked to the first page. Culdn't figure out why ! Please help !
Controller
public function unverified_images()
{
$data['title'] = 'Choose from the below Images to verify them >>';
$data['total_rows'] = $this->admin_model->all_unverified_images();
$config['base_url'] = base_url().'index.php/admin/admin/unverified_images';
$config['total_rows'] = $data['total_rows']->num_rows();
$config['per_page'] = 1;
$config['num_links'] = 8;
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data['query'] = $this->db->where('image_status', 0)
->get('tbl_img', $config['per_page'], $this->uri->segment(4));
$data['links']=$this->pagination->create_links();
$this->load->view('admin/image_verify', $data);
}
public function verify_image()
{
$data['title'] = 'Choose from the below Images to verify them >>';
$data['msg'] = $this->admin_model->verify_image();
$data['query'] = $this->admin_model->all_unverified_images();
redirect('admin/admin/unverified_images');
}
Model
function all_unverified_images()
{
$this->db->where('image_status', 0);
$query = $this->db->get('tbl_img');
return $query;
}
View
<?php echo $links; ?>
Sound like that CI can't figure out what page you supposed to be on.
Try adding the uri_segment or the cur_page for the $config array.
The uri_segment should tell CI what part of the url holds the current page number, from your example code it seem to be 4 (from the query) while CI's default is 3.