Pagination codeigniter not working - php

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

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

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

Codeigniter - reverse data in table

I'm look for a way to display data in reverse order using the table library along with pagination in Codeigniter. From
public function pagination(){
$this->load->library("pagination");
$this->load->library("table");
$this->table->set_heading("ID", "Name", "Address");
$config["base_url"] = site_url('site/pagination');
$config["total_rows"] = $this->db->get("mail")->num_rows();
$config["per_page"] = 10;
$config["num_links"] = 10;
$config['page_query_string'] = TRUE;
$this->pagination->initialize($config);
$data["records"] = $this->db->get("mail", $config["per_page"], $this->input->get('per_page', TRUE));
$data['pagination'] = $this->pagination->create_links();
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("content_about", $data);
$this->load->view("site_footer");
}
The table is working just fine but i can't figure out a way to display the data in reverse order. I would like to make the table start with the latest entries (ID, Name, Address) from the database and not the first.
I suppose their i something wrong with how i query the database:
$data["records"] = $this->db->get("mail", $config["per_page"], $this->input->get('per_page', TRUE));
Change :
$data["records"] = $this->db->get("mail", $config["per_page"], $this->input->get('per_page', TRUE));
to :
$data["records"] = $this->db->select("ID, Name, Address")->from("mail")->order_by("ID DESC")->limit($config["per_page"], $offset)->result_array();
only add in your model
$this->db->order_by('id', 'asc');//or desc

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, sending parameters between functions

I have a problem with pagination and codeigniter. I have a quick_searh view from witch I am submitting the information to a index controller function and there setting the pagination and calling the quick_search method to get the data I want. It just doesnt work . I've spent more then 5 hours rewriting those methods and even starting with quick_search and then passing to index function but nothing worked, please help.
public function index(){
// search parameters config
$lawyer_name = $this->input->post('lawyer_name');
$kanzlei = $this->input->post('kanzlei');
$area_of_expertise = $this->input->post('area_of_expertise');
$post_code = $this->input->post('post_code');
$city = $this->input->post('city');
$result = $this->quick_search(
$this->uri->segment(3),
$lawyer_name,
$kanzlei,
$area_of_expertise,
$post_code,
$city);
if(isset($result)){
// pagination config
$this->load->library('pagination');
$this->load->library('table');
$config['total_rows'] = count($result);
$config['base_url'] = 'http://localhost/anwalt/index.php/search/index';
$config['per_page'] = 5;
$config['num_links'] = 5;
$this->pagination->initialize($config);
$data['search_result_array'] = $result;
$data['main_content'] = 'pages/quick_search_results';
$this->load->view('templates/home_body_content', $data);
}
}
the quick_search function:
public function quick_search($offset, $lawyer_name, $kanzlei, $area_of_expertise, $post_code, $city){
// no input in the quick search
if( empty($lawyer_name) && empty($kanzlei) && empty($area_of_expertise)
&& empty($post_code) && empty($city))
{
$result = 'nothing';
} else {
$this->load->model('quick_search_model');
$result = $this->quick_search_model->get_search_results(
$offset,
$lawyer_name,
$kanzlei,
$area_of_expertise,
$post_code,
$city
);
}
return $result;
}
the sql is like this:
$sql = "SELECT users.user_id, users.canonical_name, first_name, last_name, city, phone_number, kanzlei
from users
inner join user_normal_aos
on users.user_id = user_normal_aos.user_id
inner join normal_areas_of_expertise
on user_normal_aos.normal_areas_of_expertise_id = normal_areas_of_expertise.normal_areas_of_expertise_id
where ".implode(" AND ", $where);
if(empty($offset)){
$offset = 0;
}
$sql = $sql." LIMIT ".$offset.", 4";
The data are displayed but I dont see the pagination in there .. and even when I want to change the url for segmenting it says I dont have any data.
The view is like:
<h1>Quick search results</h1>
<?php
if($search_result_array == "nothing"){
echo "<h3>You havent inputed anything</h3>";
} else {
echo $this->table->generate($search_result_array);
}
echo $this->pagination->create_links();
As per your search variables you can use this:
$lawyer_name = $this->input->post('lawyer_name');
$kanzlei = $this->input->post('kanzlei');
$area_of_expertise = $this->input->post('area_of_expertise');
$post_code = $this->input->post('post_code');
$city = $this->input->post('city');
/*pagination start*/
$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/index/lawyer/'.$lawyer_name.'/kanzlei/'.$kanzlei.'/area_of_expertise/'.$area_of_expertise.'/post_code/'.$city.'/page/';
$config['total_rows'] = $this->model->count_all_results(); ###implement this function to count all the vodeos as per the search variables, just use the same function as "quick_search" but without the limit clause
$config['per_page'] = count($result);;
$config['uri_segment'] = 10;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Prev';
$config['cur_tag_open'] = '<span class="active_page">';
$config['cur_tag_close'] = '</span>';
$this->pagination->initialize($config);
/*pagination end*/
You can not use $this->pagination->create_links(); method in view.
Use $data['pagination'] = $this->pagination->create_links(); in controller just before loading view
and echo $pagination in view
hope this will help you.

Categories