I am new in Codeigniter. I am creating pagination in codeigniter, but paging number not working.
This is my record where i want pagination
when i click on paging link i got this type error.
Here is my controller code
public function benadharStatus($rowno=0) {
$this->load->library("pagination");
if ($this->session->userdata('session_name')) {
$data = array();
$data = array('title' => 'Beneficiaries Aadhaar Status', 'pageTitle' => 'Beneficiaries Aadhaar Status');
$obj = new Admin_model;
$obj1 = new Reporting_model;
$data['states'] = $obj->getState();
if($_POST){
// Row per page
$rowperpage = 25;
// Row position
if($rowno != 0){
$rowno = ($rowno-1) * $rowperpage;
}
// All records count
$allcount = $obj1->getBenficzReportOLD($_POST);
// Get records
$users_record = $obj1->getBenficzReports($_POST,'1',$rowno,$rowperpage);
// Pagination Configuration
$config['base_url'] = base_url().'benadharStatus';
$config['use_page_numbers'] = TRUE;
$config['total_rows'] = $allcount;
$config['per_page'] = $rowperpage;
$config['uri_segment'] = 5;
// Initialize
$this->pagination->initialize($config);
$data['links'] = $this->pagination->create_links();
$data['benficirz'] = $users_record;
$data['row'] = $rowno;
$data['passed_state']=$this->input->post('ben_state');
$data['passed_district']=$this->input->post('ben_district');
$data['passed_awc']=$this->input->post('ben_awc');
$data['flag']=1;
}else{
$data['flag']=2;
}
$this->load->view('reports/benadharStatus', $data);
} else {
redirect('admin');
}
}
Guys please help... Thanks in advance
Please try this replace this code:
public function benadharStatus($rowno=0) {
$this->load->library("pagination");
if ($this->session->userdata('session_name')) {
$data = array();
$data = array('title' => 'Beneficiaries Aadhaar Status', 'pageTitle' => 'Beneficiaries Aadhaar Status');
$obj = new Admin_model;
$obj1 = new Reporting_model;
$data['states'] = $obj->getState();
if($_POST){
// Row per page
$rowperpage = 25;
// Row position
if($rowno != 0){
$rowno = ($rowno-1) * $rowperpage;
}
// All records count
$allcount = $obj1->getBenficzReportOLD($_POST);
// Get records
$users_record = $obj1->getBenficzReports($_POST,'1',$rowno,$rowperpage);
// Pagination Configuration
$controler_name = $this->router->fetch_class();
$config['base_url'] = base_url().'/'.$controler_name.'/benadharStatus';
$config['use_page_numbers'] = TRUE;
$config['total_rows'] = $allcount;
$config['per_page'] = $rowperpage;
$config['uri_segment'] = 5;
// Initialize
$this->pagination->initialize($config);
$data['links'] = $this->pagination->create_links();
$data['benficirz'] = $users_record;
$data['row'] = $rowno;
$data['passed_state']=$this->input->post('ben_state');
$data['passed_district']=$this->input->post('ben_district');
$data['passed_awc']=$this->input->post('ben_awc');
$data['flag']=1;
}else{
$data['flag']=2;
}
$this->load->view('reports/benadharStatus', $data);
} else {
redirect('admin');
}
}
Related
For my Codeigniter project I have below pagination code:
$this->load->library('pagination');
$config['base_url'] = base_url().'/joblist';
$config['total_rows'] = $this->joblist_model->joblist_count();
$config['per_page'] = 2;
$config['use_page_numbers'] = TRUE;
$config["uri_segment"] = 2;
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$data['title'] = 'Job list in Cambodia';
$data['rows'] = $this->joblist_model->joblist($config["per_page"], $page);
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
Question: How do I change the url from domain/joblist/page/xx to domain/joblist?page=xx ?
Yes, you can enable the query string in pagination.
$this->load->library('pagination');
$config['base_url'] = base_url().'/joblist';
$config['total_rows'] = 4;
$config['per_page'] = 2;
$config['use_page_numbers'] = TRUE;
$config["uri_segment"] = 2;
$config['page_query_string']=TRUE;
$config['query_string_segment'] = 'page';
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$this->pagination->initialize($config);
$this->pagination->create_links());
Here is an example using $this->input->get(); and page_query_string set to true
You may need to set some routes on config/routes.php
https://www.codeigniter.com/user_guide/general/routing.html#examples
$route['product'] = 'joblist/index';
controller example
class Joblist extends CI_Controller {
public function index() {
$this->load->library('pagination');
$config['base_url'] = base_url('product');
$config['total_rows'] = $this->db->count_all_results('jobs');
$config['per_page'] = 2;
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'page';
// Use the input->get();
$page = ($this->input->get('page')) ? $this->input->get('page') : 0;
$this->pagination->initialize($config);
$data['rows'] = $this->fetch_jobs($config['per_page'], $page);
$data['links'] = $this->pagination->create_links());
$this->load->view('example', $data);
}
public function fetch_jobs($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("jobs");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
Can anyone please help me? I am trying to paginate my table. all links are working, but the items are repeated on other pages. I tried similar questions on here, but nothing is working.
function index($type="deposit", $limit_from=0)
{
$limit_from = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$total_records = $this->Expense->get_total_rows($type);
$data['controller_name'] = $this->get_controller_name();
$lines_per_page = 3;
$data[$type.'s'] = $this->Expense->get_records($lines_per_page, $limit_from, $type);
$config['base_url'] = base_url('index.php/expenses/index/'.$type);
$config['total_rows'] = $total_records;
$config['per_page'] = 3;
$config["uri_segment"] = 4;
// $choice = $total_records / $config['per_page'];
// $config['num_links'] = round($choice);
$config['use_page_numbers'] = TRUE;
$config['first_url'] = base_url('index.php/expenses/index/'.$type.'/1');
$this->pagination->initialize($config);
$data["links"] = $this->pagination->create_links();
$this->load->view('expenses/'.$type, $data);
$this->_remove_duplicate_cookies();
}
Thank you all. I got it to work: below is what I finally did and it is working great!
function index($type="deposit", $sort_by='deposit_date', $sort_order='asc', $page_limit=0)
{
$data['controller_name'] = $this->get_controller_name();
$config = array();
$config['base_url'] = site_url("expenses/index/$type/$sort_by/$sort_order");
$config['total_rows'] = $this->Expense->get_total_rows($type);
$config['per_page'] = 4;
$config["uri_segment"] = 6;
$data['num_rows'] = $config['total_rows'];
$data['type'] = $type;
$this->pagination->initialize($config);
$page = ($this->uri->segment(6)) ? $this->uri->segment(6) : 0;
$data[$type.'s'] = $this->Expense->get_records($config["per_page"], $page, $type, $sort_by, $sort_order);
$data["links"] = $this->pagination->create_links();
$data['headers'] = $this->Expense->table_headers($type);
$data['sort_by'] = $sort_by;
$data['sort_order'] = $sort_order;
$this->load->view('expenses/'.$type, $data);
$this->_remove_duplicate_cookies();
}
how to link the pages using codeigniter pagination?
the problem is with the variable $start its supposed to pass the start value to the database limit query . but its not.
Model
public function fetch_countries($limit,$start) {
$this->db->limit($limit, $start);
//$query ="SELECT * FROM mail_to Order By id Desc LIMIT ".$limit.",".$start;
$query = $this->db->get("mail_to");
//$qry=$this->db->query($query);
var_dump($query->result_array());
if($query->num_rows()>0){
return $query->result_array();
}
else
{
return false;
}
}
Controller
public function example1() {
$config = array();
$config["base_url"] = base_url() . "index.php/admin/user/example1";
$config["total_rows"] = $this->mdl_user->record_count();
$config["per_page"] = 5;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
//var_dump($config['per_page']);
$data["results"] = $this->mdl_user->fetch_countries($config["per_page"],$page);
//var_dump($data['results']);
$data["links"] = $this->pagination->create_links();
$this->load->view("example1", $data);
}
URI Segment should be
$config["uri_segment"] = 4;
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
In Model
public function fetch_countries($limit, $page)
{
$query = $this->db->query("SELECT * FROM mail_to LIMIT $page, $limit");
$result = $query->result_array();
return $result;
}
In Controller
public function example1() {
$config["base_url"] = base_url() . "index.php/admin/user/example1";
$config["total_rows"] = $this->mdl_user->record_count();
$config["per_page"] = 5;
$config['uri_segment'] = 4;
$limit = $config['per_page'];
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$data["results"] = $this->mdl_user->fetch_countries($page $limit);
$data["links"] = $this->pagination->create_links();
$this->load->view("example1", $data);
}
I have model code function in Codeigniter like below, if me sort order by RAND() can display all item but if order by field name like part_no can't display all item, in next page same display with page 1.
Model
function listProductPerCategory($id, $limit, $start){
$this->db->select('
category.id, category.category, product.id, product.id_category,
product.name, product.picture, product.description, product.permalink, product.part_no
');
$this->db->join('category', 'product.id_category = category.id');
$this->db->where('product.id_category', $id);
$this->db->order_by('product.part_no');
$this->db->limit($limit, $start);
$query = $this->db->get($this->tableProduct);
return $query->result();
}
Controller
function listcategory($page = NULL) {
$id = $this->input->get('list');
$data['categoryHead'] = $this->M_home->listCategory();
$this->load->view('frontend/template/header', $data);
$data['productsLast'] = $this->M_home->listProductLast();
$data['productsLast2'] = $this->M_home->listProductLast2();
$data['category'] = $this->M_home->listCategory();
//paging
$config['base_url'] = base_url('categoryproduct'.'?'.http_build_query($_GET));
$config['total_rows'] = $this->M_home->listProductPerCategory_num_rows($id);
$config['per_page'] = $per_page = 12;
$config['uri_segment'] = 2;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Prev';
$config['page_query_string'] = TRUE;
//end paging
$this->pagination->initialize($config);
$data['paging'] = $this->pagination->create_links();
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$data['listCategory'] = $this->M_home->listProductPerCategory($id, $per_page, $page);
$this->load->view('frontend/category', $data);
$this->load->view('frontend/template/footer');
}
You have understand that
In SQL LIMIT, start is starting number of record not PAGE.
But from what you passing to listProductPerCategory is page number
So from page 1 to page 2 it is only one record move on next page not 12 records.
So you need to fix in function listProductPerCategory (dont forget to change variable name which may you confuse yourself , change $start -> $page)
So you should get this as below
function listProductPerCategory($id, $limit, $page){
if($page < 1){
$page = 1;
}
if($page > 1){
$start = $limit * $page;
}else{
$start = 0;
}
$this->db->select('
category.id, category.category, product.id, product.id_category,
product.name, product.picture, product.description, product.permalink, product.part_no
');
$this->db->join('category', 'product.id_category = category.id');
$this->db->where('product.id_category', $id);
$this->db->order_by('product.part_no');
$this->db->limit($limit, $start);
$query = $this->db->get($this->tableProduct);
return $query->result();
}
Controller -
Try this -
function listcategory($page = 0){
$id = $this->input->get('list');
$data['categoryHead'] = $this->M_home->listCategory();
$this->load->view('frontend/template/header', $data);
$data['productsLast'] = $this->M_home->listProductLast();
$data['productsLast2'] = $this->M_home->listProductLast2();
$data['category'] = $this->M_home->listCategory();
//paging
$config['base_url'] = base_url('categoryproduct'.'?'.http_build_query($_GET));
$config['total_rows'] = $this->M_home->listProductPerCategory_num_rows($id);
$config['per_page'] = $per_page = 12;
$config['num_links'] = 2;
$config['uri_segment'] = 3;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Prev';
$config['page_query_string'] = TRUE;
//end paging
$this->pagination->initialize($config);
$data['paging'] = $this->pagination->create_links();
$data['page'] = $page;
// $page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$data['listCategory'] = $this->M_home->listProductPerCategory($id, $per_page, $page);
$this->load->view('frontend/category', $data);
$this->load->view('frontend/template/footer');
Order by condition is wrong in your model.
Try this -
$this->order_by('title','ASC or DESC or RANDOM');
I want to display some specific data from database through pagination using CI.but pagination by default passess parameter in function which creates problem for me.
Here is my Controller
function page($catagoryid){
$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/pagination/page/';
$count = $this->db->query("select * from tbl_products where category_id='$categoryid'");
$total=$count->num_rows();
$per_page = 4;
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->model("mpagination");
$data['list'] = $this->mpagination->get_s(,$categoryid,$config['per_page'],$this->uri->segment(3));
if ($data['list']!== null){
$this->load->view('pagination_view', $data);
}
else {
$this->load->view('noresult');
}
}
Here is my Model
function get_s($categoryid,$num, $offset) {
$this->db->select ('*');
->where('category_id',$categoryid); // field name
$sql = $this->db->get('tbl_products',$num, $offset); // table name
if ($sql->num_rows () >0) {
return $sql->result();
}
else {
return null;
}
}
Here is my view
<?php
foreach($list as $row):?>
<?php echo $row->_prduct_id." ".$row->name." ".$row->description;?>
<br/>
<?php endforeach;?>
<br/>
<?php echo $pagination; ?>
This code work at first but when i clicked on second link of pagination it will not work because by default pagination sends its value to url as parameter and doesnot show data.
what can i do?
I didn't understand your code properly but this is the way you need to do pagination.
public function page($category_id)
{
$result = $this->model_file->model_function($category_id);
$start_index = 0;
$total_rows = count($result);
$items_per_page = 10;
$filtered = array_splice($result,$start_index,$items_per_page);
$model['data'] = $filtered;
$model['page_link'] = create_page_links (base_url()."/controller_file_name/page", $items_per_page, $total_rows);
$this->load->view('view_file_name_path',$model);
}
function create_page_links($base_url, $per_page, $total_rows) {
$CI = & get_instance();
$CI->load->library('pagination');
$config['base_url'] = $base_url;
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$CI->pagination->initialize($config);
return $CI->pagination->create_links();
}
Try like this
public function index($offset = 0)
{
$language_id = 1;
$this->load->library('pagination');
$limit = 10;
$total = $this->legend_model->get_legend_count($language_id);
$config['base_url'] = base_url().'legend/index/';
$config['total_rows'] = $total;
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['first_link'] = '<< First';
$config['last_link'] = 'Last >>';
$config['next_link'] = 'Next ' . '>';
$config['prev_link'] = '<' . ' Previous';
$config['num_tag_open'] = '<span class="number">';
$config['num_tag_close'] = '</span>';
$config['cur_tag_open'] = '<span class="current"><a href="#">';
$config['cur_tag_close'] = '</a></span>';
$this->pagination->initialize($config);
$data['offset'] = $offset;
$data['legends'] = $this->legend_model->get_legend($language_id, $limit, $offset);
$this->template->write('title', 'Legend : Manage Legend');
$this->template->write_view('content', 'legend/index', $data);
$this->template->render();
}