codeigniter pagination showing all query results in one page - php

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

Related

Pagination Shows same results

Using codeigniter 3 I have setup pagination. When I click on the next link I get the same results minus the first post, I cannot seem to find what is causing this.
I tried removing the offset and not allowing numbers, and I still have no change.
controller
public function index($offset = 0){
//Pagination
$config['base_url'] = base_url().'posts/index/';
$config['total_rows'] = $this->db->count_all('posts');
$config['uri_segment'] = 3;
$config['use_page_numbers'] = TRUE;
$config['per_page'] = 5;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$config['display_pages'] = FALSE;
$this->pagination->initialize($config);
$data['links'] = $this->pagination->create_links();
$data['title'] = 'Newest';
$data['posts'] = $this->post_model->get_posts(FALSE,$config['per_page'],$offset);
$this->load->view('templates/header');
$this->load->view('posts/index', $data);
$this->load->view('templates/footer');
}
model
public function get_posts($slug = FALSE, $limit=FALSE,$offset = FALSE){
if($limit){
$this->db->limit($limit,$offset);
}
if($slug === FALSE){
$this->db->order_by('created_time','DESC');
$this->db->join('categories','categories.id = posts.category_id');
$query = $this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts',array('slug'=>$slug));
return $query->row_array();
}
view
<ul class="pagination">
<?php echo $links; ?>
</ul>
///////////////////UPDATE/////////////
So this is how my code looks now after updating as your said.
public function index($offset=0){
//Pagination
$config['base_url'] = base_url().'posts/index/';
$config['total_rows'] = $this->db->count_all('posts');
$config['uri_segment'] = 3;
$config['num_links'] = 10;
$config['per_page']=3;
$limit = $config['per_page'];
$offset = ($offset) * ($config['per_page']);
this->pagination->initialize($config);
$data['posts'] = $this->post_model->get_posts(FALSE,$limit,$offset);
in the view I have it set to
<?php echo $this->pagination->create_links(); ?>
So now the issue is, it shows 3 rows(results) and when I click next it shows nothing and that's it, so now I am only getting 3 results only on the first page and none on the next pages. What is wrong?
Index Function Not Working Segment Use Query String
if $offset is current_page_number starting on 0, definition should be:
//number of rows to get
$limit = $config['per_page'];
//where start to get it
$offset = ($offset) * ($config['per_page']);
$data['posts'] = this->post_model->get_posts(FALSE,$limit,$offset);
If you define offset as current_page, starting on 1.
public function index($offset = 1){
......
//number of rows to get
$limit = $config['per_page'];
//where start to get it
$offset = ($offset - 1) * ($config['per_page']);
$data['posts'] = this->post_model->get_posts(FALSE,$limit,$offset);
Talking about "Index Function Not Working Segment Use Query String"...
If you have $config['enable_query_strings'] = TRUE you should GET it
public function index() {
$page = $this->input->get('page');
...
$offset = ($page - 1) * ($config['per_page']);
...

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;

Error in generating page number links using Codeigniter

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

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.

pagination error in codeigniter

i have error in codeigniter when i want to search month "april" and year "2014" in pagination page 2 back to month "mei"
this is controller
function index($nama='', $bln = 0, $thn = 0)
{
if (!$nama AND $this->input->post('nama'))
{
$nama = $this->input->post('nama');
}
else if(!$nama)
{
$nama = 0;
}
if (!$bln AND $this->input->post('bulan'))
{
$bln = $this->input->post('bulan');
}
else if(!$bln)
{
$bln = 0;
}
if (!$thn AND $this->input->post('tahun'))
{
$thn = $this->input->post('tahun');
}
else if(!$thn)
{
$thn = 0;
}
$count = $this->miuran->get_count_iuran($nama, $bln, $thn);
// Set up pagination
$offset = 0;
$perpage = 5;
$uri_segment = 7;
if ($count > $perpage) {
$this->load->library('pagination');
$this->load->config('pagination');
$config = $this->config->item('pag');
$config['base_url'] = site_url('pelatih/iuran/'.$nama.'/'.$bln.'/'.$thn);
$config['total_rows'] = $count;
$config['per_page'] = $perpage;
$config['uri_segment'] = $uri_segment;
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
if($this->uri->segment($uri_segment)){
$offset = $this->uri->segment($uri_segment);
}
}
else
{
$this->data['pagination'] = '';
$offset = 0;
}
$this->data['anggota'] = $this->miuran->get_daftar_iuran($bln, $thn,$nama,$perpage,$offset);
$this->data['title'] ='UKM Taekwondo | iuran';
$this->data['orang'] = $this->mlogin->dataPengguna($this->session->userdata('username'));
$this->data['contents'] = $this->load->view('pelatih/iuran/view_iuran', $this->data, true);
$this->load->view('template/wrapper/pelatih/wrapper_anggota',$this->data);
}
when i switch to page 2 data back to month "mei".
please help me what to do.
thank you.
Have you tried loading the date helper http://ellislab.com/codeigniter/user-guide/helpers/date_helper.html

Categories