Replace get parameters in Codeigniter pagination? - php

The following piece of code for my pagination function like following:-
public function news()
{
$this->load->library('pagination');
$config = array();
$config["base_url"] = base_url() . "index.php/welcome/news";
$this->load->model('news_model');
$total_row = $this->news_model->record_count();
$config["total_rows"] = $total_row;
$config["per_page"] = 1;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = $total_row;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$config['page_query_string'] = TRUE;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$config['first_url'] = $config['base_url'].'?'.http_build_query($_GET);
$this->pagination->initialize($config);
if($this->uri->segment(3)){
$page = ($this->uri->segment(3)) ;
}
else{
$page = 1;
} //echo $config["per_page"].'/'.$page; exit();
$this->load->model('news_model');
$data["results"] = $this->news_model->fetch_data($config["per_page"], $page);
$str_links = $this->pagination->create_links();
$data["links"] = explode(' ',$str_links );
$this->load->model('news_model');
$data['lt_news'] = $this->news_model->get_lt_newsletter();
$data['rm_news'] = $this->news_model->get_rm_newsletter();
$this->load->view('newsletter/newsletter',$data);
}
From the above code, the url browser shows like the following:-
http://localhost/ins/index.php/welcome/news?per_page=2
I am like jammed as to how to change it to be look like the following:
http://localhost/ins/index.php/welcome/news/2
Is there a way to do it..? I am newbie to the pagination in codeigniter so, i do not know if there is a necessity to change the url parameters to be looking like the above..?

Set $config['page_query_string'] to false.
From the doc https://www.codeigniter.com/userguide3/libraries/pagination.html#customizing-the-pagination:
By default, the pagination library assume you are using URI Segments,
and constructs your links something like:
http://example.com/index.php/test/page/20 If you have
$config['enable_query_strings'] set to TRUE your links will
automatically be re-written using Query Strings. This option can also
be explicitly set. Using $config['page_query_string'] set to TRUE, the
pagination link will become:
http://example.com/index.php?c=test&m=page&per_page=20

Make the page a parameter of the function, like so:
public function news($pageNum)
{
...
$page = $pageNum
...
}
Then you should be able to access it via:
http://localhost/ins/index.php/welcome/news/2

Related

Cannot access protected property CI_Pagination

Cannot access protected property `CI_Pagination
Trying to add submit button on last page of pagination currently it's on every page by default...on line 49 i have condition to check whether i am in last page or not, I want a submit button at the end of the pagination page
Code on which error is showing..please explain how to implement
if($this->pagination->cur_page >= ceil($this->pagination->total_rows / $this->pagination->per_page))
{
$isLastPage = true;
}else{
$isLastPage = false;
}
Pagination code
public function quizdisplay()
{
//echo $this->pagination->create_links();
//$this->load->library('pagination');
$config = array();
$config['base_url'] = 'http://localhost/xampp/cii/index.php/Questions/quizdisplay';
//$config['total_rows'] = 10;
$total_row = $this->quizmodel->record_count();
$config["total_rows"] = $total_row;
$config['per_page'] = 1;
$config["uri_segment"] = 3;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = $total_row;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
if($this->uri->segment(3)){
$page = ($this->uri->segment(3)) ;
}else{
$page = 1;
}
$data["results"] = $this->quizmodel->getQuestions($config["per_page"], $page);
$this->pagination->create_links();
$data["links"] = $this->pagination->create_links();
View data according to array.
$this->load->view("play_quiz", $data);
}
Both the codes are in same function

CodeIgniter search input and pagination URL

I want to create a URL containing string from search form and page from pagination
For example, my base URL is example.com/search
When showing the search result it will be example.com/search/?s=keyword and when showing the search result from next page it will be example.com/search/?s=term&p=2 for second page and so on
How to make it like that in CodeIgniter?
try this. here used page instead of p in URL
$keyword = trim($this->input->get('s', TRUE));
$this->load->library('pagination');
$config['total_rows'] = $this->db->get('table_name')->num_rows();
$config['per_page'] = 10;
$config['num_links'] = 5;
$config['enable_query_strings'] = TRUE;
$config['use_page_numbers'] = TRUE;
$config['query_string_segment'] = 'page';
$config['page_query_string'] = TRUE;
$config['base_url'] = site_url('search/index/?s=' . $keyword);
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
if ($this->input->get('page')) {
$sgm = (int) trim($this->input->get('page'));
$segment = $config['per_page'] * ($sgm - 1);
} else {
$segment = 0;
}
$this->pagination->initialize($config);
// your query
$query = $this->db->select('*')->from('table_name')->limit($config['per_page'], $segment)->get();
now your URL will be same as you mention in your question

codeigniter pagination related questions

$this->load->library('pagination');
$config['base_url'] = base_URL().'admin/issues/list_issues/';
$config['total_rows'] = $this->db->get('dt_issues')->num_rows();
$config['num_links']=10;
$config['per_page'] = 5;
$config['uri_segment'] = 3;
Codeigniter pagination was working properly. But when click on first link it is not working and the remaining links will work. Please help me.
$offset is the variable in which u can get value which page user is reqesting
function list_issues($offset = null)
{
$this->load->library('pagination');
........
// Pagination config
$config['base_url'] = base_URL().'admin/issues/list_issues/';
$config['total_rows'] = $this->provider_model->countprovider();
$config['per_page'] = 3;
// $config['creat_link'] = 1; <-- This is not a valid option
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
// Init config
$this->pagination->initialize($config);
// If the pagination library doesn't recognize the current page add:
$this->pagination->cur_page = $offset;
.....
// further your code
}

how to route pagination next page in uri->segment(1) eg (example.com/1)

I have pagination in frontend index page example.com when I click page two of pagination I want to load page like eg example.com/1.
<?php
function index()
{
$e = $this->employer_model->count_jobs_with_employer();
//print_r($e) ;
$config['base_url'] = $this->config->site_url(); //set the base url for pagination
$config['total_rows'] = $e; //total rows
$config['per_page'] = '10'; //the number of per page for pagination
$config['uri_segment'] = 1; //see from base_url. 3 for this case
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config); //initialize pagination
$page = ($this->uri->segment(1)) ? $this->uri->segment(1) : 0;
$joblist['listemp'] = $this->employer_model->employerbyjob($config['per_page'], $page);
foreach($joblist['listemp'] as $emp)
{
$joblist['jobs'][$emp->u_id] = $this->employer_model->get_ByTbl_Col('employer_post_job', 'e_id', $emp->u_id,'dead_line', date('Y-m-d'));
}
$c['job_list'] = $this->load->view('front/blocks/employer/job_list', $joblist, TRUE);
}
?>
Something like this should work:
application/config/routes.php:
//default routes:
$route['default_controller'] = "yourcontroller";
$route['404_override'] = '';
// your custom route:
$route['(:num)'] = "yourcontroller/index/$1";
application/controllers/Yourcontroller.php
function index($page = null)
{
}

pagination with codeigniter, other then index function

I have set rout as
$route['dog/(:any)'] = "dog/index/$1"; /// for single dog info
$route['dog/list'] = "dog/listing"; /// for dog list, display all dogs.
$route['dog/list/(:num)'] = "dog/listing/$1"; /// for pagination
single dog url is like dog/dogName-4.html
my controller is as
public function index()
{
$dogInfo = $this->uri->segment(2);
if ($dogInfo != "")
{
$dogDetails = explode('-', $dogInfo);
$this->load->view('common/header',$header);
$this->load->view('dog/dog_info', $content);
$this->load->view('common/footer', $footer);
}
else
redirect('welcome', 'location', 301);
}
public function listing()
{
$this->load->library("pagination");
$breed = $this->input->get('breed');
$gender = $this->input->get('gender');
$state = $this->input->get('state');
$seller = $this->input->get('seller');
$config = array();
$config["base_url"] = base_url() . "dog/list/";
$config["total_rows"] = $this->dogs->get_dog_list_count($breed, $gender, $state, $seller);
$config["per_page"] = 5;
$config["uri_segment"] = 2;
$config['use_page_numbers'] = TRUE;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$content['details'] = $this->dogs->get_dog_list($config["per_page"], $page,$breed, $gender, $state, $seller);
$content['paginatonLinks'] = $this->pagination->create_links();
$content['total_dogs'] = $config['total_rows'];
$content['cur_page'] = $page + 1;
$content['total_pages'] = ceil($config["total_rows"] / $config["per_page"]);
$this->load->view('common/header');
$this->load->view('dog/dog_list', $content);
$this->load->view('common/footer', $footer);
}
The controller's index function is to display information about one dog, and listing function is for all the dogs,
I have to set pagination for listing function i did set all the required variables for pagination, pagination displaying the result as well
Found [134] Ads :: Page 1 of 27 1 2 3 > Last ›
but when i click on the pagination 1 2 3 else on page it brings me to the index page.
I need to be on the listing function of the controller. please any one help me.
here is the updated code
the rout code is
$route['dog/list'] = "dog/listing";
$route['dog/list/(:num)'] = "dog/listing/$1";
$route['dog/(:any)'] = "dog/index/$1";
and here is the updated controller code
$config = array();
$config["base_url"] = base_url() . "dog/list/";
$config['suffix'] = '?'.http_build_query($_GET, '', "&");
$config["total_rows"] = $this->dogs->get_dog_list_count($breed, $gender, $state, $seller);
$config["uri_segment"] = 3;
$config["per_page"] = 5;
$config['use_page_numbers'] = TRUE;
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$config['cur_tag_open'] = '<a href="#" class="page_selected">';
$config['cur_tag_close'] = '</a>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$this->load->model('DogsListing_model', 'dogs');
$this->load->model('Breed_model', 'breeds');
$content['breeds'] = $this->breeds->get_all_id_title();
$content['details'] = $this->dogs->get_dog_list($config["per_page"], $page,$breed, $gender, $state, $seller);
$content['search'] = array('breed' => $breed, 'gender' => $gender, 'state' => $state, 'seller' => $seller);
$content['paginatonLinks'] = $this->pagination->create_links();
$content['total_dogs'] = $config['total_rows'];
$content['cur_page'] = $page + 1;
$content['total_pages'] = ceil($config["total_rows"] / $config["per_page"]);
i have found the solution of my question
first i have use $this->uri->segment(3) and $config["uri_segment"] = 3; i was using a wrong uri->segment for pagination. this solve my problem related to pagination.
my next part is i have to set variables for seller or search results with the pagination. for example i have the url doglist.html/?seller=29 i need to embed pagination with this pagination. so that the result will be like dog/list/3?breed=&gender=female&state=&submit2=Fetch i have solve this issue with this line of code $config['suffix'] = '?'.http_build_query($_GET, '', "&"); now my pagination with variables works fine.
Thanks to #jtheman and #Venkat, but mostly #jtheman for their contributings.
If you have any doubt related to pagination check out this generalised function which i wrote as answer to another question.

Categories