How to create pagination with 3 argument in url using codeigniter? - php

I'm using Codeigniter 2.2 and I try to build pagination ask Codeigniter guide and it is work perfectly for me when I used as url below
$pages_num: is the amount of pages for view.
http://localhost/Codeigniter2.1.4/public_html/page/$pages_num
But I get error when I add one argument for url
as code below I try to find it 3 weeks ago But I have not solution Please
Notes: number 2 is id categories and number 4 is amount of pagination view
http://localhost/Codeigniter2.1.4/public_html/cat/2/4
Here is my code in controller
$this->load->model('frontend/categories_m');
$count = $this->db->count_all_results('job');
$perpage = 2;
if ($count > $perpage) {
$this->load->library('pagination');
$config['base_url'] = site_url('cat/2');
$config['total_rows'] = $count;
$config['per_page'] = $perpage;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
$offset = $this->uri->segment(3);
} else {
$this->data['pagination'] = '';
$offset = 0;
}
$this->db->limit($perpage, $offset);
$this->data['job_cat'] = $this->job_m->get_job();
$this->data['subview'] = 'cat';
$this->load->view('_main_layout', $this->data);
Here is for view:
<?PHP if ($pagination): ?>
<section class="pagination">
<?PHP echo $pagination; ?>
</section>
<?PHP endif; ?>
Please help
Sorry I can't post images here

With codeigniter pagination, the default link structure is as below
http://website/pages/getPagesViaAjax/3/5
Here pages is controller, getPagesViaAjax is function inside it and 3 is a parameter for category id and 5 will be the pagination count(i.e. offset)
in this case my pagination config is something like
$config['base_url'] = site_url('getPagesViaAjax/3');
$offset = $this->uri->segment(4);
So basically your offset will be the last argument and i.e. 5(available at segment 4) in this case. So prior to firing your query to model, please check the offset.

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 pagination do not work properly

I use codeigntier 3 pagination library.
It works, but not exactly how should.
First I will post my code.
controller setting
$config['base_url'] = base_url('items');
$config['total_rows'] = $this->ItemsM->countAll();
$config['per_page'] = 5;
$config['use_page_numbers'] = TRUE;
$config['nex_link'] = "Next";
$limit = $config['per_page'];
$offset = $this->uri->segment(2);
$this->pagination->initialize($config);
$data = array(
'musicians' => $this->ItemsM->allItems($limit, $offset),
'pagination' => $this->pagination->create_links()
);
$this->load->view('public/header');
$this->load->view('public/item-list', $data);
$this->load->view("public/footer");
Model
class ItemsM extends CI_MODEL{
function allUsers($limit="", $offset ="")
{
$query = $this->db->select('*')
->from('items')
->limit($limit)
->offset($offset)
->order_by('creation_date', 'desc')
->get();
return $query->result();
}
public function countAll(){
return $this->db->count_all_results('items');
}
Route
$route['items/(:any)'] = 'items';
This pagination works, but the results Im getting are not correct.
On the first page/tab it shows items with this order
item 1 item 2 item 3 item 4 item 5 // ok
but when open page 2
I get next 5 results this way
item 3
item 4
item 5
item 6
item 7
Honestly I do not understand where is the problem, I someone know, please help. Thank you.
It seems your problem is with varialble $offset as you are passing $offset as your page number for eg. if your page number is 2 then it will skip 2 results but you need to skip 5 results. To solve this replace this code with you controller setting.
$config['base_url'] = base_url('items');
$config['total_rows'] = $this->ItemsM->countAll();
$config['per_page'] = 5;
$config['use_page_numbers'] = TRUE;
$config['nex_link'] = "Next";
$limit = $config['per_page'];
$page = $this->uri->segment(2);
if($page > 1){
$offset = ($page-1) * $limit;
}else{
$offset = $page;
// Or you can set $offset = 0;
}
$this->pagination->initialize($config);
$data = array(
'musicians' => $this->ItemsM->allItems($limit, $offset),
'pagination' => $this->pagination->create_links()
);
$this->load->view('public/header');
$this->load->view('public/item-list', $data);
$this->load->view("public/footer");
Here i edited your variable $offset. Now you will get results as expected.

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

CodeIgniter Paging class, active link not working

I am using the paging class of the codeigniter for the first time. Everything else is working fine, but whichever page I am going, the active link is always the [1] and it doesn't change. Also the next button is always linked to the first page. Culdn't figure out why ! Please help !
Controller
public function unverified_images()
{
$data['title'] = 'Choose from the below Images to verify them >>';
$data['total_rows'] = $this->admin_model->all_unverified_images();
$config['base_url'] = base_url().'index.php/admin/admin/unverified_images';
$config['total_rows'] = $data['total_rows']->num_rows();
$config['per_page'] = 1;
$config['num_links'] = 8;
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data['query'] = $this->db->where('image_status', 0)
->get('tbl_img', $config['per_page'], $this->uri->segment(4));
$data['links']=$this->pagination->create_links();
$this->load->view('admin/image_verify', $data);
}
public function verify_image()
{
$data['title'] = 'Choose from the below Images to verify them >>';
$data['msg'] = $this->admin_model->verify_image();
$data['query'] = $this->admin_model->all_unverified_images();
redirect('admin/admin/unverified_images');
}
Model
function all_unverified_images()
{
$this->db->where('image_status', 0);
$query = $this->db->get('tbl_img');
return $query;
}
View
<?php echo $links; ?>
Sound like that CI can't figure out what page you supposed to be on.
Try adding the uri_segment or the cur_page for the $config array.
The uri_segment should tell CI what part of the url holds the current page number, from your example code it seem to be 4 (from the query) while CI's default is 3.

Categories