Some bug with code igniter pagination library - php

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

Related

How to add pagination codeigniter

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

How do segment 4 for pagination in codeigniter

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";

URL issue with route setting

$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

Codeigniter Pagination first page is always current page

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

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