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.
Related
I am having a pagination page in my controller, i am kind of puzzled in listing 2 data per page on my view. The controller code which i am using is as follows:
public function newsletter()
{
$this->load->library('pagination');
$config = array();
$config["base_url"] = base_url() . "index.php/welcome/newsletter";
$this->load->model('newsletter_model');
$total_row = $this->newsletter_model->record_count();
$config["total_rows"] = $total_row;
$config["per_page"] = 2; // per page 2 records
$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'] = FALSE;
$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;
}
$this->load->model('newsletter_model');
$data["results"] = $this->newsletter_model->fetch_data($config["per_page"], $page);
$str_links = $this->pagination->create_links();
$data["links"] = explode(' ',$str_links );
$this->load->model('newsletter_model');
$this->load->view('newsletter/newsletter',$data);
}
With the above code i am getting 2 records per page as i intended, but i could not understand the logic behind the pagination. can anyone explain me the logic worked in codeigniter pagination, with my work itself as it would be handy for me to understand it.
My Model code is as follows:
public function fetch_data($limit, $id)
{
$this->db->select('*');
$this->db->from('ins_newsletter');
$this->db->order_by('nl_id', 'desc');
$this->db->limit($limit,$id);
$query = $this->db->get();
return $query->result_array();
}
public function record_count()
{
return $this->db->count_all("ins_newsletter");
}
CI's Paginator class only generates pagination links for you.
The "record pagination" logic happens here:
$this->db->limit($limit, $offset);
$limit is the number of records to fetch, $offset is the offset of the first row to return.
To fetch the first two records, we will set $limit = 2, $offset = 0
CI translates into SQL like:
SELECT ...... FROM ins_newsletter LIMIT 0, 2;
To fetch record number 3 and 4, we will set $limit = 2, $offset = 2
CI translates into SQL like:
SELECT ...... FROM ins_newsletter LIMIT 2, 2;
Edit:
There is a small bug in your code.
The offset should be 0 for page 1, 2 for page 2, 4 for page 3, 6 for page 4, 8 for page 5...and so on.
public function fetch_data($limit, $page)
{
$offset = ($page - 1) * $limit;
...
}
And I would suggest you to change the second param to $page instead of $id.
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
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.
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;
There is a bit of problem in my code which i am not able to solve.
I m using CI 1.7.2.
I have implemented the CI Pagination into the system correctly.
The results are displayed fine but the links in the pagination are not rendering correctly.
For eg. If i click on the page 2 then the results are displayed as per the 2nd Page but the current link at pagination numbers remains 1 which should change to 2.
Here is the code that has been implemented
$total = $this->bmodel->countResultsBanner();
$data['total'] = $total;
$uri_segment = $this->uri->segment(4);
if($uri_segment == 0 || empty($uri_segment)){
$uri_segment = 0;
}
$perPage = 5;
$config['base_url'] = base_url()."index.php/modules/banner/index";
$config['total_rows'] = $total;
$config['per_page'] = $perPage;
$config['num_links'] = 4;
//$config['cur_tag_open'] = '<b><span class="current_page">';
//$config['cur_tag_close'] = '</span></b>';
$this->pagination->initialize($config);
$result = $this->bmodel->getAllBanners($perPage,$uri_segment);
$data['result'] = $result;
thanks in advance.
J
Heyy,
I also faced the same problem. In the end, solution turned out to be very simple. :)
by default CI assumes that uri segment used for pagination is (3). Which in your case, for you (i am assuming shamelessly) is incorrect.
$config['base_url'] = base_url()."index.php/modules/banner/index";
$config['total_rows'] = $total;
$config['per_page'] = $perPage;
$config['num_links'] = 4;
$config['uri_segment'] = 3; /* segment of your uri which contains the page number */
$this->pagination->initialize($config);
Hope this solves your problem
ok... try this...
$total = $this->bmodel->countResultsBanner();
$data['total'] = $total;
/* Comment out this part
$uri_segment = $this->uri->segment(4);
if($uri_segment == 0 || empty($uri_segment)){
$uri_segment = 0;
}
*/
$perPage = 5;
$config['base_url'] = base_url()."index.php/modules/banner/index";
$config['total_rows'] = $total;
$config['per_page'] = $perPage;
$config['num_links'] = 4;
//$config['cur_tag_open'] = '<b><span class="current_page">';
//$config['cur_tag_close'] = '</span></b>';
$this->pagination->initialize($config);
/*Change the following line*/
$result = $this->bmodel->getAllBanners($perPage,$this->uri->segment(5));
$data['result'] = $result;
$this->load->library('pagination');
$config['base_url']="http://localhost/CodeIgniter/pagination";
$config['per_page']=2;
$config['total_rows']= $this->db->get('record')->num_rows();
$this->pagination->initialize($config);
$data['query']= $this->db->get('record',$config['per_page'], $this->uri->segment(3));
$this->load->view('pagination',$data);
$config['uri_segment'] = num; /* where num is the uri segment where you have page number */
Try this, it might help.
class Admin_model extends CI_Model {
public function __construct() {
parent::__construct();
}
public function get_product($search, $page, $perpage) {
$page = $page - 1;
$page < 0 ? $page = 0 : $page = $page;
$from = $page * $perpage;
$query = $this->db
->select('*')
->from('tbl_product')
->limit($perpage, $from)
->get();
return $query->result();
}
}