How to make pagination query string in codeigniter - codeigniter

I have url like this:
myproject/search?s=a
How to make pagination
Here my code in controller :
public function index()
{
$keyword = $this->input->get('s');
$config['per_page'] = 2;
$config['uri_segment'] = 2;
$config['full_tag_open'] = '<nav class="pagination"><ul>';
$config['full_tag_close'] = '</ul></nav>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="'.site_url(uri_string()).'">';
$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['first_link'] = '«';
$config['prev_link'] = '‹';
$config['last_link'] = '»';
$config['next_link'] = '›';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['base_url'] = site_url('search');
$config['total_rows'] = $this->m_home->count_search_produk($keyword);
$this->pagination->initialize($config);
$data['results'] = $this->m_home->search_produk($keyword, $config['per_page'], 0);
$data['pagination'] = $this->pagination->create_links();
$data['total'] = $this->m_home->count_search_produk($keyword);
$this->theme->frontend('f_home/search', $data);
}
in this line :
$data['results'] = $this->m_home->search_produk($keyword, $config['per_page'], 0);
I set offset 0, I want to set dinamicly, using uri_segment, but not work, how to do that?
when I click pagination, the url like this:
myproject/search?s=a&page=3

Its working very well
public function search()
{
$keyword = $this->input->get('s');
$data['title'] = 'Search Results';
$config['per_page'] = 2;
$config['uri_segment'] = 2;
$config['full_tag_open'] = '<nav class="pagination"><ul>';
$config['full_tag_close'] = '</ul></nav>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="'.site_url(uri_string()).'">';
$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['first_link'] = '«';
$config['prev_link'] = '‹';
$config['last_link'] = '»';
$config['next_link'] = '›';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['page_query_string'] = TRUE;
$config['query_string_segment']= 'page';
$config['base_url'] = site_url('search?s='.$keyword);
$config['total_rows'] = $this->m_home->count_search_produk($keyword);
$this->pagination->initialize($config);
$page = (empty($_GET['page'])) ? 0 : $_GET['page'];
$data['results'] = $this->m_home->search_produk($keyword, $config['per_page'], $page);
$data['pagination'] = $this->pagination->create_links();
$data['total'] = $this->m_home->count_search_produk($keyword);
$this->theme->frontend('f_home/search', $data);
}

Related

CodeIgniter Pagination Links removed after like query executes

`
Controller :
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$config = array();
$config['base_url'] = base_url().'tr_admin/billmanagement/';
$config['total_rows'] = $this->bill_model->count_all_bill($this->session->userdata('searchBillRec'));
$config['per_page'] = 100;
$config['use_page_numbers'] = TRUE;
$config["uri_segment"] = 3;
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['use_page_numbers'] = FALSE;
$config['next_link'] = 'Next';
$config['next_tag_open'] = '<li class="next page">';
$config['next_tag_close'] = '</li>';
$config['prev_link'] = ' Previous';
$config['prev_tag_open'] = '<li class="prev page">';
$config['prev_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li class="page">';
$config['num_tag_close'] = '</li>';
$data['recList'] = $this->bill_model->get_bill($config["per_page"],$page,$this->session->userdata('searchBillRec'));
$this->pagination->initialize($config);
$data['main_content'] = 'tr_admin/billmanagement';
$this->load->view('tr_admin/includes/template', $data);
Model:
if($filterData['bill_to']!=''){
$this->db->like('bill_to', $filterData['bill_to'],'both');
}
$this->db->order_by("bill_id","DESC");
$this->db->limit($per_page,$page);
return $this->db->get()->result_array();
`
Pagination links only show when there's more results than the current offset you set. So if there's only 5 records and your offset is 10 then there's no pagination to do.

Codeigniter Pagination Links are not working with boostrap pagination class

Pagination links created with bootstrap class "pagination" are not working. Links created without adding bootstrap class working fine. Here is my controller Code
$config ['base_url'] = base_url () .'adv/index';
$config ['total_rows'] = $result->num_rows();
//$config ['uri_segment'] = $segment;
$config ['per_page'] = 5;
$config ['num_links'] = 5;
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '<li class="page-item">';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '&laquo';
$config['prev_tag_open'] = '<li class="page-item prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '&raquo';
$config['next_tag_open'] = '<liclass="page-item">';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<liclass="page-item">';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="page-item active"><a href="page-link">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li class="page-item">';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$data['links']=$this->pagination->create_links();
$category=$this->adv_model->adv_get_paginated_data($segment,5);
$data['category']=$category;
$this->global['pageTitle'] = 'Media : Communiation';
$this->loadViews("adv_view", $this->global,$data, NULL);
[View Image][1]
[1]: https://i.stack.imgur.com/JDcVX.png

How to access pagination url without query string in codeigniter?

I want to access pagination URL without query string in CodeIgniter like
http://127.0.0.1/hmvc/business_list/20
Now I am getting URL like
http://127.0.0.1/hmvc/business_list/?p=2
By this I am unable to read the URL segment and pagination is not working fine. It should return 0-10 records on the first page and 10-20 on second page and so on. But in my case, it returns 0-10 on the first page then 2-12 on the second page on the basis of URL segment.
Here is my code:
public function index()
{
$query = $this->db->get('categories','10',$this->uri->segment(2));
$data['results'] = $query->result();
$data['seg'] = $this->uri->segment(2);
$query2 = $this->db->get('categories');
$config['base_url'] = base_url().'business_list/';
$config['total_rows'] = $query2->num_rows();
$config['per_page'] = 10;
// $config['uri_segment'] = $this->uri->segment(2);
// $config['page_query_string'] = false;
$config['page_query_string'] = true;
$config['full_tag_open'] = "<ul class='pagination pagination-right margin-none'>";
$config['full_tag_close'] = '</ul>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = '<li>';
$config['next_tagl_close'] = '</li>';
$config['prev_tag_open'] = '<li>';
$config['prev_tagl_close'] = '</li>';
$config['first_tag_open'] = '<li>';
$config['first_tagl_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tagl_close'] = '</li>';
$this->pagination->initialize($config);
$data['links'] = $this->pagination->create_links();
$data['title'] = "Business List";
$this->load->view('categories',$data);
}
How do I rectify this problem?
Use below configuration for pagination
$config['per_page'] = 10;
$config['uri_segment'] = 2;
$config['full_tag_open'] = '<ul class="pagination pagination-right margin-none">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '&laquo';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '&raquo';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class='disabled'><li class='active'><a href='#'>';
$config['cur_tag_close'] = '<span class='sr-only'></span></a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['enable_query_strings'] = TRUE;

Pagination in CodeIgniter doesn't work with passing parameters

Creating pagination in CodeIgniter doesn't seem to work for me when using URL parameters, when I navigate to page 2 it tells me "page not found"
In the controller
// pagination settings
$config["total_rows"] = $this->product_model->hitungdata($itemid);
$config['base_url'] = base_url().'pagination/'.$itemid.'?offset='.$offset;
$config['uri_segment'] = '3';
$config['use_page_numbers'] = TRUE;
$config['per_page'] = 25;
// integrate bootstrap pagination
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '«';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '»';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$data['product'] = $this->product_model->tarikdata($config["per_page"], $offset, $itemid);
$data['links'] = $this->pagination->create_links();
In the model
function tarikdata($limit, $offset, $itemid)
{
$sql = "select * from allproduct where itemid = '$itemid' LIMIT $offset, $limit";
$query = $this->db->query($sql);
return $query->result();
}
function hitungdata($itemid)
{
$sql = "select * from allproduct where itemid = '$itemid'";
$query = $this->db->query($sql);
return $query->num_rows();
}

CodeIgniter: pagination shows "per_page" twice?

I'm using CI's pagination library. Here's my config:
$this->load->library('pagination');
$config = array();
$config['base_url'] = 'http://localhost/index.php/search/';
$config['total_rows'] = 200;
$config['per_page'] = 10;
$config['num_links'] = 4;
$config['full_tag_open'] = '<ol>';
$config['full_tag_close'] = '</ol>';
$config['first_link'] = 'First';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['last_link'] = 'Last';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['next_link'] = 'Next';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['prev_link'] = 'Previous';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active">';
$config['cur_tag_close'] = '</li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
echo htmlentities($this->pagination->create_links());
echo ($this->pagination->create_links());
The resulting links has per_page twice, such as yo see here:
http://localhost/index.php/search/&per_page=&per_page=10. Any ideas why?
You are echoing it two times in the two last lines of the code
Replace:
echo htmlentities($this->pagination->create_links());
echo ($this->pagination->create_links());
To:
echo $this->pagination->create_links();
And you should pass your variables to the views instead of echoing it from the controller.

Resources