Unable to configure pagination properly in Codeigniter - php

I'm new to Codeigniter pagination so i'm not able to configure it properly, that why i need your help guys.
This is my code so far
public function nearbay_get($idNearBay = null)
{
$config['base_url'] = 'http://mypage.si/rest/api/nearbay/nearbay';
$config['total_rows'] = $this->Near_bays->count();
$config["per_page"] = 5;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['suffix'] = '?'.http_build_query($_GET, '', "&");
$config['use_page_numbers'] = TRUE;
$config['enable_query_strings'] = 'page=';
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = round($choice);
$this->pagination->initialize($config);
$page = ($this->uri->segment(6))? $this->uri->segment(6) : 0;
$results = $this->Near_bays->get(null, [null, null], [null, null], '', $config["per_page"], $page);
foreach($results as $data) {
echo $data->id_near_bay . " ------- " . $data->name . "<br>";
}
echo $this->pagination->create_links();
}
And this is the result if i visit http://mypage.si/rest/api/nearbay/nearbay
And if i click on any page number i get blank page and my url changes to http://mypage.si/rest/api/nearbay/nearbay?
So my question are. How to get pagination working? How to properly config pagination so i will be able to fetch results via ?page and ?per_page values trough url.. My final links should look like this http://mypage.si/rest/api/nearbay/nearbay?page=1&per_page=20
If you need any additional informations please let me know and i will provide... Thank you!

Here is answer, Hope it will help and url should be like this: http://mypage.si/rest/api/nearbay/nearbay/1
public function nearbay_get($idNearBay = null)
{
$config['base_url'] = 'http://mypage.si/rest/api/nearbay/nearbay';
$config["per_page"] = 5;
$config["num_links"] = 3;
$config["uri_segment"] = 6;
$config['use_page_numbers'] = TRUE;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['suffix'] = '?'.http_build_query($this->input->get(), '', "&");
$config['first_url'] = $config['base_url'] . $config['suffix'];
$page = ($this->uri->segment(6))? $this->uri->segment(6) : 0;
$offset = ($page == 0 ? 0 : ($page - 1) * $config["per_page"]);
$config['total_rows'] = $this->Near_bays->count();
$this->pagination->initialize($config);
$results = $this->Near_bays->get(null, [null, null], [null, null], '', $config["per_page"], $offset);
foreach($results as $data) {
echo $data->id_near_bay . " ------- " . $data->name . "<br>";
}
//$current_page = $page == 0 ? 1 : $page;
//$start = $page == 0 ? 1 : (($page - 1) * $config["per_page"] + 1);
//$end = ($start + count($results) - 1);
//$total_page = ceil($config['total_rows'] / $config['per_page']);
echo $this->pagination->create_links();
}
For check uri_segment check the number. Currently is 6 try further number if you are not getting.

Related

pagination submit get value codeigniter

I using GET method for searching records. now when i have applied pagination i can't understand how to submit data to next pagination page link.
I have tried this answer. but it is just stacking page number in link segments.
example:
From
localhost/phc/search/results/0?s=ai&product_cat=all&post_type=product
To
localhost/phc/search/results/?s=ai&product_cat=all&post_type=product/12/24
so i didn't used that code.
And now my code is:
public function results() {
//pagination
$offset = $this->uri->segment(3);
$limit = 12;
//$offset = 0;
$data['limit'] = $limit;
$config['base_url'] = base_url() . 'search/results/';
$config['first_url'] = base_url() .'search/results/0';
$data['total'] = $this->All_data_model->countSearchProducts($_GET);
$config['total_rows'] = $data['total'];
$config['per_page'] = $limit;
$config['uri_segment'] = 3;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['offset'] = $offset;
$data['total_rows'] = $config['total_rows'];
$data['page'] = 'search';
$data['navbar'] = $this->All_data_model->navbarContent();
$data['products'] = $this->All_data_model->searchResults($_GET , $offset , $limit);
$this->template->write_view('content', 'products/search', $data);
$this->template->render();
}
Tell what the best way to submit data in this case if not GET and how can i access this data.
Put offset in your url
localhost/phc/search/results?s=ai&product_cat=all&post_type=product&offset=123
and set these configs.
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'offset';
$config['reuse_query_string'] = TRUE;
I changed your code but it's not tested at all.
public function results() {
//pagination
$offset = $this->input->get('offset') ? $this->input->get('offset') : 0;
$limit = 12;
$data['limit'] = $limit;
$config['base_url'] = base_url() . 'search/results/';
$data['total'] = $this->All_data_model->countSearchProducts($_GET);
$config['total_rows'] = $data['total'];
$config['per_page'] = $limit;
// set these mostly...
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'offset';
$config['reuse_query_string'] = TRUE;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['offset'] = $offset;
$data['total_rows'] = $config['total_rows'];
$data['page'] = 'search';
$data['navbar'] = $this->All_data_model->navbarContent();
$data['products'] = $this->All_data_model->searchResults($_GET , $offset , $limit);
$this->template->write_view('content', 'products/search', $data);
$this->template->render();
}

pagination is not working in codeigniter

I am working in CodeIgniter. I want to create pagination for dynamic record. So I have write code like below:
$config = array();
$config["base_url"] = base_url() . "employer/search_user";
$config["per_page"] = 3;
$config['use_page_numbers'] = TRUE;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
if($this->uri->segment(3)){
$page = ($this->uri->segment(3)) ;
}
else{
$page = 1;
}
$query = "select sn.*,u.first_name,u.last_name,u.email,u.phone,group_concat(DISTINCT s.skill) as sk,group_concat(DISTINCT c.name) as ct from
users as u
left join snapshot as sn on sn.user_id = u.user_id
left join industry as ind on ind.ind_id = sn.industry
left join city c ON(FIND_IN_SET(c.city_id, sn.current_location) > 0)
left join skill s ON(FIND_IN_SET(s.skill_id, sn.skill) > 0)
where ".$where." u.group_id = 3 group by u.user_id limit ".$config["per_page"];
$data['users'] = $this->admin_model->query($query);
$row = count($data['users']);
$config["total_rows"] = $row;
$config['num_links'] = $row;
$this->pagination->initialize($config);
$data["links"] = $this->pagination->create_links();
$data['main_content']="employer/search_user";
$this->load->view('employer/template', $data);
Here, I got 3 records in page but pagination link is not display. I have include pagination library in constructor. I did not get anuthing in $data["links"] this variable.
So where I have to correct my code? What is wrong in my code?
You need to use LIMIT and OFFSET in your query. For example
$config['per_page'] = 3;
$config['uri_segment'] = 4; // This is the current page
$offset = 0;
if ($this->uri->segment($config['uri_segment']) != "") {
$offset = $this->uri->segment($config['uri_segment']);
}
$limit = $config['per_page'];
$sql = "SELECT `field_name` FROM `table_name` WHERE `table_name`.field_name = ?";
if($limit != "") {
$sql .= "LIMIT $limit ";
}
if($offset != "") {
$sql .= "OFFSET $offset";
}
$result = $this->db->query($sql, array($field_data));
Hope this helps
For $config["total_rows"] = $row;
You get only 3. Because You are setting limit in the query.
Kindly write another query for total rows without limit.I think this will solve Your problem.
Below is my pagination code
$config = array();
$config["base_url"] = base_url() . "crm/crm/contactmgmt/modid/" . $modid;
if($this->input->post('Search')=='Search'){
$data['pn']=$data1['pn']=$this->input->post('sel_prod');
//echo "hello";
}
$config["total_rows"] = $this->Crm_model->record_count_unsubinst();
$config["per_page"] = 6;
$this->load->config('pagination');
$config["uri_segment"] = 6;
$config['enable_query_strings']='true';
$data['activeTab'] = "View";
$this->pagination->initialize($config);
$page = ($this->uri->segment(6)) ? $this->uri->segment(6) : 0;
$data['query'] = $this->Crm_model->listUnsubscribedInstn($config["per_page"], $page);
$data["link"] = $this->pagination->create_links();
$data["links"]=$data["link"];
$data["cnt"]=$config["total_rows"];
$data["sno"]=$this->uri->segment(6);

Codeigniter pagination not worrking returns 404

Hi I'd like some help please. I 'm having two controllers: movies.php and actors.php where I list all actors and all movies in them.
For example this is the method for listing all my movies
public function index() {
// count all movies
$total = $this->movie_model->count();
// set up pagination
$per_page = 10;
if ($total > $per_page) {
$this->load->library('pagination');
$config['base_url'] = site_url($this->uri->segment(1) . '/');
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
$offset = $this->uri->segment(2);
}
else {
$this->data['pagination'] = '';
$offset = 0;
}
// fetch the movies
$this->db->limit($per_page, $offset);
$this->data['movies'] = $this->movie_model->get();
// load the view
$this->view('movies/index');
}
and for listing all actors
public function index() {
// count all actors
$total = $this->actor_model->count();
// set up pagination
$per_page = 10;
if ($total > $per_page) {
$this->load->library('pagination');
$config['base_url'] = site_url($this->uri->segment(1) . '/');
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$this->data['pagination'] = $this->pagination->create_links();
$offset = $this->uri->segment(2);
}
else {
$this->data['pagination'] = '';
$offset = 0;
}
// fetch the movies
$this->db->limit($per_page, $offset);
$this->data['actors'] = $this->actor_model->get();
// load the view
$this->view('actors/index');
}
I have set my routes like this
$route['default_controller'] = "movies";
$route['404_override'] = '';
$route['movies'] = 'movies/index';
$route['actors'] = 'actors/index';
And the urls are like this
h**p: //localhost/www/task/public/ // for movies (default controller)
h**p: //localhost/www/task/public/actors // for actors controller
The problem is when I try to click a panination link to get the next records in each controller I get a 404 error. I have tried to change my config settings in pagination but no luck.
Any help would be appreciated.
I have link here very help full for pagination and sortable table links this may help.
http://forum.codeigniter.com/thread-1198.html
Change $config['base_url'] = site_url($this->uri->segment(1) . '/'); to,
$config['base_url'] = base_url().'movies/index';
and
$config['base_url'] = base_url().'actors/index';
And $config['uri_segment'] = 3;

codeigniter pagination get url

I have a site developed in codeigniter and in a page I want to use pagination.
The problem is. my url now is smoething like that:
http://site/index.php/tee/view_tee/?id=2
This is my code into the controller:
$data['tee'] = $this->Tee_model->getTeeByUserId($this->input->get('id', TRUE));
$data['tee_like'] = $this->Tee_model->getLikeTeeByUserId($this->input->get('id', TRUE));
$data['user'] = $this->User_model->getUserById($this->session->userdata('id'));
$this->load->library('pagination');
$config['base_url'] = site_url().'/tee/view_tee/?id='.$this->input->get('id', TRUE);
$config['total_rows'] = count($data['tee']);
$config['per_page'] = 6;
$config['uri_segment'] = 3;
if (count($_GET) > 0) $config['suffix'] = '?' . http_build_query($_GET, '', "&");
$config['first_url'] = $config['base_url'].'?'.http_build_query($_GET);
$data['page_links']=$this->pagination->create_links();
$this->pagination->initialize($config);
$this->load->view('view_tee_view',$data);
With this code when I click into my pagination link I have this url:
http://site/index.php/tee/view_tee/?id=2/6?id=2
If I click again I have this:
http://site/index.php/tee/view_tee/?id=2/6?id=2/6?id=2
This is my html:
echo $this->pagination->create_links();
foreach($tee as $t){
// I have three div per lines
}
add this...
$config['page_query_string'] = TRUE;
remove this...
if (count($_GET) > 0) $config['suffix'] = '?' . http_build_query($_GET, '', "&");
$config['first_url'] = $config['base_url'].'?'.http_build_query($_GET);
[EDIT] WORKING EXAMPLE
$this->load->library('pagination');
$config['page_query_string'] = TRUE;
$config['base_url'] = site_url().'/tee/view_tee/?id='.$this->input->get('id', TRUE);
$config['total_rows'] = 200;
$config['per_page'] = 6;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
echo $this->pagination->create_links();

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