Error in generating page number links using Codeigniter - php

I am creating a pagination using Codeigniter and my problem is I have an error in displaying the page number links in my URL.
example I go to page 2 my URL would be like this:
http://localhost/my_project/inbound/listing/1
if page 3
http://localhost/my_project/inbound/listing/2
Here's my controller
$config = array();
$config['base_url'] = base_url('inbound/listing');
$config['total_rows'] = $this->mod->countList();
$config['per_page'] = 1;
$config['uri_segment'] = 3;
//fd($config);
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
fp($page, 'pink'); //print out result
$order = ($this->input->get('order'))? $this->input->get('order'): '';
$sort = ($this->input->get('sort'))? $this->input->get('sort'): '';
// Query data
$data['data_list'] = $this->mod->listing($config['per_page'], $page); //where, limit, page, field, sort
fP($data['data_list']); //print out result
$data['pagination'] = $this->pagination->create_links();
Then my model for generating links:
function listing($limit, $start)
{
//DATE_FORMAT(`post_date_added`, "%m/%d/%Y %H:%i") as `proper_post_date_added`,
$this->db->select('*');
$this->db->from('inventory');
$this->db->limit($limit, $start);
$query = $this->db->get();
//$rows = $query->result_array();
if($query->num_rows() > 0) {
foreach($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
Can you help me with this?

If you want to go to page number based on your url, modify your listing function to be like this:
function listing($limit, $start)
{
$offset_1 = $start - $limit;
$offset_2 = $offset_1 < 0 ? 0 : $offset_1;
//DATE_FORMAT(`post_date_added`, "%m/%d/%Y %H:%i") as `proper_post_date_added`,
$this->db->select('*');
$this->db->from('inventory');
$this->db->limit($limit, $offset_2);
$query = $this->db->get();
//$rows = $query->result_array();
if($query->num_rows() > 0) {
foreach($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}

Related

Codeigniter pagination fetch only the first page unable to navigate second and third page

i have an issue with codeigniter pagination, the same technique i did in the older codeigniter version it works fine but not in this latest version(3.1.9). The first front page fetch data fine but when i click on page 2, 3 or 4 still it display those of the first front row data.
Here is my controller:
$total_rows = $this->estate_model->estate_count();
$config = pagination_configuration(base_url("real_estate"), $total_rows, 10, 3, 5, true);
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(3) : 0;
$page_num = $page-1;
$page_num = ($page_num<0)?'0':$page_num;
$page = $page_num*$config["per_page"];
$data["links"] = $this->pagination->create_links();
$obj_result = $this->estate_model->get_all_active_estate($config["per_page"], $page);
Model:
public function get_all_active_estate($limit, $start)
{
$this->db->select('*');
$this->db->from('estate');
$this->db->where('status', 'active');
$this->db->limit($limit, $start);
$Q = $this->db->get();
if ($Q->num_rows() > 0) {
$return = $Q->result();
} else {
$return = 0;
}
$Q->free_result();
return $return;
}
Please help.. thank you
try
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
instead
$page = ($this->uri->segment(2)) ? $this->uri->segment(3) : 0;

combine get_where and limit in active record codeigniter

I am little confused here.
I have a modal like this :
public function selectRequestPerUser($nama_user, $start_row, $limit) {
$query = $this->db->get_where('tbl_requestfix', array('nama_user' => $nama_user), $start_row, $limit);
return $query->result_array();
}
So, I use this modal to create a pagination in CI like this :
$nama = $this->session->userdata('nama');
$start_row = $this->uri->segment(2);
$per_page = 3;
if(trim($start_row) == ''){
$start_row = 0;
};
$this->load->library('pagination');
$config['base_url'] = base_url().'control_closing/';
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$request = $this->model_request->selectRequestPerUser($nama, $start_row, $per_page);
$data['data_request'] = $request;
$this->load->view('view_closing', $data);
in view, just :
<?php echo pagination ?>
It just give me a blank page. I think in my modal get_where is the problem. Anybody can help ?
The syntax of get_where() is
$query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);
You need to change this line in your model (alter $start_row and $limit) to make it work,
$query = $this->db->get_where('tbl_requestfix', array('nama_user' => $nama_user), $limit, $start_row);

codeigniter pagination url and display issue

i have a problem with my pagination in CI.
my url for the first page: http://example.com/products/page
second page: http://example.com/products.page/12 than 24 and so on +12
Controller:
$config = array();
$config["base_url"] = base_url() . "shop/page";
$config["total_rows"] = $this->shop_model->products_count();
$config["per_page"] = 12;
$config["uri_segment"] = 3;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = round($choice);
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['products'] = $this->shop_model->
getProducts($config["per_page"], $page);
and if i insert and:
$config['use_page_numbers'] = TRUE;
Model:
function getProducts($limit, $start)
{
$this->db->select('*');
$this->db->from('products');
$this->db->join('products_img', 'products_img.i_product_id = products.id', 'left');
$this->db->order_by("products.id", "desc");
$this->db->limit($limit, $start);
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
it shows me the url corectly, but it displays almost the same products that were on the first page.
I checked and other posts on this kind of problems, but i couldn't solve it. Please help! Thank you!
Limit doesn't care about the page, it cares about the result to start with:
function getProducts($limit, $start)
{
$this->db->select('*');
$this->db->from('products');
$this->db->join('products_img', 'products_img.i_product_id = products.id', 'left');
$this->db->order_by("products.id", "desc");
$this->db->limit($limit, ($start * $limit));
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
It displayed "almost the same" results because you were only offsetting by 1, or 2, or 3 if the page number were 1, 2, or 3..
The above assumes that page 1 is actually 0 though, page 2 is 1, etc etc.. Did that because you default to zero. If that isn't the case you should adjust to reflect what you represent page numbers as.

codeigniter pagination showing all query results in one page

hi every one i am trying to display mysql database records using codeigniter pagination..for that i written the following below codes..and when i run this it shows me all the results in one page while the page numbers are displayed right according to the given per_page limit..
Model:
function get_records($user_name, $limit, $start) {
$this->db->limit($limit, $start);
$this->db->select('GROUP_CONCAT(cid) AS cIDs');
$this->db->where('user_name', $user_name);
$this->db->group_by("company_user");
$this->db->from('company_records');
$query = $this->db->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
function count_records($user_name) {
$this->db->select('GROUP_CONCAT(cid) AS cIDs');
$this->db->where('user_name', $user_name);
$this->db->group_by("company_user");
return $this->db->count_all('company_records');
}
Controller:
$this->load->model('Records', 'Records', TRUE);
$this->load->library('pagination');
$config['base_url'] = base_url() . 'records/index';
$config['total_rows'] = $this->Records->count_records($this->session->userdata('user'));
$config['per_page'] = 10;
$config["uri_segment"] = 4;
$config['use_page_numbers'] = TRUE;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
/* $start = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0; */
if ($this->uri->segment(4) > 0) {
$offset = ($this->uri->segment(4) + 0) * $config['per_page'] - $config['per_page'];
} else {
$offset = $this->uri->segment(4);
}
$data['recordsDetails'] = $this->Records->get_records($this->session->userdata('user'), $config["per_page"], $offset);
$data['pages'] = $this->pagination->create_links();
i have tried google search and stackoverflow.. but not yet found any solutions to this..may be i have issue with the query or i dont know..
please help me...
I used this code for pagination.
In the model
//Search for results
public function search($id, $table, $pageNumber) {
//Get the number of pages
$numOfPage = $this->findResults($id, $table, RETURN_NUM_OF_PAGES, $pageNumber);
if ($numOfPage < 1) {
return false; //If there are no search results return false
} else {
$row = array();
$res = $this->findResults($id, $table, RETURN_RESULTS, $pageNumber);
for ($j = 0; $j < $res->num_rows(); $j++) {
$row[$j] = $res->row($j);
}
return $row; //Return the results
}
}
// Find the results from DB and return number of pages/ results
function findResults($id, $table, $returnType, $pageNumber) {
$this->db->where('id', $id);
$this->db->order_by('reputation', 'desc'); //Order the users by reputation
$itemsPerPage = 4;
$startingItem = ($pageNumber - 1) * $itemsPerPage;
if ($returnType == RETURN_RESULTS) {
$res = $this->db->get($table, $itemsPerPage, $startingItem);
return $res; //Return the results
} else { //Return the number of pages
$res = $this->db->get($table);
$count = $res->num_rows(); //Get the total number of results
$numofPages = (int) ($count / $itemsPerPage);
if ($count % $itemsPerPage != 0)
$numofPages = $numofPages + 1; //If the number of items < $itemsPerPage there should be 1 page
return $numofPages; //Return the number of pages
}
}
So from the controller you have to call the function search($id, $table, $pageNumber).
Also the constants were defined in config/constants.php file as follows.
define('RETURN_NUM_OF_PAGES', 0);
define('RETURN_RESULTS', 1);

CodeIgniter $this->pagination->create_links(); returning blank string

public function viewconsultant($id1='')
{
$id1 = $id1 ? $id1 : 1;
$pagenum=5;
$start=($id1-1)*$pagenum;
$users = $this->db->get_where('connections', array('user_id'=>97));
$pageall = count($users);
$config['total_rows']=$pageall;
$config['per_page']=$pagenum;
$config['num_links']=4;
$config['base_url']="localhost/~chrisfu/linkedin2/index.php/connections/viewconsultant";
$config['use_page_numbers']=true;
$this->load->library('pagination');
$this->pagination->initialize($config);
$query = $this->db->get_where('connections', array('user_id'=>97), $pagenum, $start);
$data['connections'] = $query->result();
$this->load->view('profile_view',$data);
}
The create_links() does not work well, it just displays a blank string. I tried this localhost/~chrisfu/linkedin2/index.php/connections/viewconsultant/2, it can jump to second page, but not pagination links created blow the table. please help!!
The problem is that you are not creating the links..
$this->load->library('pagination');
$this->pagination->initialize($config);
// add the line below to your code
$data['links'] = $this->pagination->create_links();
// debug
// echo $data['links'];
$query = $this->db->get_where('connections', array('user_id'=>97), $pagenum, $start);
$data['connections'] = $query->result();
$this->load->view('profile_view',$data);
Also, you can omit the first line in your method by doing the following:
public function viewconsultant($id1=1)
{
// line below no longer necessary
//$id1 = $id1 ? $id1 : 1;
}
Try this:
function viewconsultant($id1=1, $offset = 0){
$this->load->library('pagination');
$pagenum=5;
//$users = $this->db->where('connections', array('user_id'=>97))->limit('20', $offset);
$total = $this->db->where('connections', array('user_id'=>97));
$pageall = $total->num_rows();
//$users = $this->db->get_where('connections', array('user_id'=>97));
//$start=($id1-1)*$pagenum;
//$pageall = count($users);
$config['total_rows'] = $pageall;
$config['per_page'] = $pagenum;
$config["uri_segment"] = 3;
$config['num_links'] = 4;
$config['base_url'] = base_url()."/index.php/connections/viewconsultant/";
$config['use_page_numbers'] = true;
$this->load->library('pagination');
$this->pagination->initialize($config);
$query = $this->db->get_where('connections', array('user_id'=>97), $pagenum, $this->uri->segment(3));
$data['connections'] = $query->result();
$data['links'] = $this->pagination->create_links();
$this->load->view('profile_view',$data);
}
Now, just echo $links in your view file to get the pagination.

Categories