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();
}
}
Related
Using codeigniter 3 I have setup pagination. When I click on the next link I get the same results minus the first post, I cannot seem to find what is causing this.
I tried removing the offset and not allowing numbers, and I still have no change.
controller
public function index($offset = 0){
//Pagination
$config['base_url'] = base_url().'posts/index/';
$config['total_rows'] = $this->db->count_all('posts');
$config['uri_segment'] = 3;
$config['use_page_numbers'] = TRUE;
$config['per_page'] = 5;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$config['display_pages'] = FALSE;
$this->pagination->initialize($config);
$data['links'] = $this->pagination->create_links();
$data['title'] = 'Newest';
$data['posts'] = $this->post_model->get_posts(FALSE,$config['per_page'],$offset);
$this->load->view('templates/header');
$this->load->view('posts/index', $data);
$this->load->view('templates/footer');
}
model
public function get_posts($slug = FALSE, $limit=FALSE,$offset = FALSE){
if($limit){
$this->db->limit($limit,$offset);
}
if($slug === FALSE){
$this->db->order_by('created_time','DESC');
$this->db->join('categories','categories.id = posts.category_id');
$query = $this->db->get('posts');
return $query->result_array();
}
$query = $this->db->get_where('posts',array('slug'=>$slug));
return $query->row_array();
}
view
<ul class="pagination">
<?php echo $links; ?>
</ul>
///////////////////UPDATE/////////////
So this is how my code looks now after updating as your said.
public function index($offset=0){
//Pagination
$config['base_url'] = base_url().'posts/index/';
$config['total_rows'] = $this->db->count_all('posts');
$config['uri_segment'] = 3;
$config['num_links'] = 10;
$config['per_page']=3;
$limit = $config['per_page'];
$offset = ($offset) * ($config['per_page']);
this->pagination->initialize($config);
$data['posts'] = $this->post_model->get_posts(FALSE,$limit,$offset);
in the view I have it set to
<?php echo $this->pagination->create_links(); ?>
So now the issue is, it shows 3 rows(results) and when I click next it shows nothing and that's it, so now I am only getting 3 results only on the first page and none on the next pages. What is wrong?
Index Function Not Working Segment Use Query String
if $offset is current_page_number starting on 0, definition should be:
//number of rows to get
$limit = $config['per_page'];
//where start to get it
$offset = ($offset) * ($config['per_page']);
$data['posts'] = this->post_model->get_posts(FALSE,$limit,$offset);
If you define offset as current_page, starting on 1.
public function index($offset = 1){
......
//number of rows to get
$limit = $config['per_page'];
//where start to get it
$offset = ($offset - 1) * ($config['per_page']);
$data['posts'] = this->post_model->get_posts(FALSE,$limit,$offset);
Talking about "Index Function Not Working Segment Use Query String"...
If you have $config['enable_query_strings'] = TRUE you should GET it
public function index() {
$page = $this->input->get('page');
...
$offset = ($page - 1) * ($config['per_page']);
...
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;
$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
}
What i have right now is like 100 divs styled to be 4 in a row, want to add pagination to it, and i succeeded with this code:
public function func() {
$this->load->library('pagination');
$this->load->database('default');
$this->load->model('s_model');
$data['all_rows_s'] = $this->s_model->count_all_s();
$data['$total_s'] = $config['total_s'] = $this->s_model->count_all_ss();
$config['base_url'] = 'http://example.com/display/s/';
$config['total_rows'] = $data['all_rows_s'];
$config['per_page'] = 12;
$config['num_links'] = 5;
$config['full_tag_open'] = '<div class="page_row">';
$config['full_tag_close'] = '</div>';
$data['row'] = $this->db->get('s_data',$config['per_page'], $this->uri->segment(3))->result();
$this->pagination->initialize($config);
$this->template->set_theme(Settings_model::$db_config['active_theme']);
$this->template->set_layout('main');
$this->template->title($this->lang->line('s'));
$this->process_partial('header', '/header');
$this->process_partial('footer', '/footer');
$this->process_template_build('s_view', $data);
}
But, this is working because WITHOUT table because of this line:
$data['row'] = $this->db->get('s_data',$config['per_page'], $this->uri->segment(3))->result();
but i need to make more filters into it, like order by id and where date > NOW(), how is this possible to do ? what ca i replace this get and the code to still work, and again the view is WITHOUT table, is divs within a foreach.
Thanks!
Try this:
Controller Function
function func() {
$this->load->library('pagination');
$this->load->database('default');
$this->load->model('s_model');
$data['all_rows_s'] = $this->s_model->count_all_s();
$data['$total_s'] = $config['total_s'] = $this->s_model->count_all_ss();
/*Pagination*/
$config['base_url'] = 'http://example.com/display/s/';
$config['total_rows'] = $data['all_rows_s'];
$config['per_page'] = 12;
$config['num_links'] = 5;
$config['full_tag_open'] = '<div class="page_row">';
$config['full_tag_close'] = '</div>';
/*Pagination*/
//$data['row'] = $this->db->get('s_data',$config['per_page'], $this->uri->segment(3))->result();
$data['row'] = $this->s_model->get_records($config['per_page'], $this->uri->segment(3));
$this->pagination->initialize($config);
$this->template->set_theme(Settings_model::$db_config['active_theme']);
$this->template->set_layout('main');
$this->template->title($this->lang->line('s'));
$this->process_partial('header', '/header');
$this->process_partial('footer', '/footer');
$this->process_template_build('s_view', $data);
}
Model Function
function get_records($limit, $offset = 0){
return $this->db->where('date > NOW()')->order_by('id', 'asc')->get('s_data', $limit, $offset)->result();
}
public function viewconsultant($id1='')
{
$id1 = $id1 ? $id1 : 1;
$pagenum=5;
$start=($id1-1)*$pagenum;
$users = $this->db->get_where('connections', array('user_id'=>97));
$pageall = count($users);
$config['total_rows']=$pageall;
$config['per_page']=$pagenum;
$config['num_links']=4;
$config['base_url']="localhost/~chrisfu/linkedin2/index.php/connections/viewconsultant";
$config['use_page_numbers']=true;
$this->load->library('pagination');
$this->pagination->initialize($config);
$query = $this->db->get_where('connections', array('user_id'=>97), $pagenum, $start);
$data['connections'] = $query->result();
$this->load->view('profile_view',$data);
}
The create_links() does not work well, it just displays a blank string. I tried this localhost/~chrisfu/linkedin2/index.php/connections/viewconsultant/2, it can jump to second page, but not pagination links created blow the table. please help!!
The problem is that you are not creating the links..
$this->load->library('pagination');
$this->pagination->initialize($config);
// add the line below to your code
$data['links'] = $this->pagination->create_links();
// debug
// echo $data['links'];
$query = $this->db->get_where('connections', array('user_id'=>97), $pagenum, $start);
$data['connections'] = $query->result();
$this->load->view('profile_view',$data);
Also, you can omit the first line in your method by doing the following:
public function viewconsultant($id1=1)
{
// line below no longer necessary
//$id1 = $id1 ? $id1 : 1;
}
Try this:
function viewconsultant($id1=1, $offset = 0){
$this->load->library('pagination');
$pagenum=5;
//$users = $this->db->where('connections', array('user_id'=>97))->limit('20', $offset);
$total = $this->db->where('connections', array('user_id'=>97));
$pageall = $total->num_rows();
//$users = $this->db->get_where('connections', array('user_id'=>97));
//$start=($id1-1)*$pagenum;
//$pageall = count($users);
$config['total_rows'] = $pageall;
$config['per_page'] = $pagenum;
$config["uri_segment"] = 3;
$config['num_links'] = 4;
$config['base_url'] = base_url()."/index.php/connections/viewconsultant/";
$config['use_page_numbers'] = true;
$this->load->library('pagination');
$this->pagination->initialize($config);
$query = $this->db->get_where('connections', array('user_id'=>97), $pagenum, $this->uri->segment(3));
$data['connections'] = $query->result();
$data['links'] = $this->pagination->create_links();
$this->load->view('profile_view',$data);
}
Now, just echo $links in your view file to get the pagination.