Page number in codeigniter - codeigniter-2

I am new user of codeigniter.
I want to show my output of my data from database as on different pages.
If total 200 entries 50 in one page and then next and so on .
Please help.
Any suggestion?

Codeigniter supports pagination by default.
/* Controller */
$this->load->library('pagination');
$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = 200;
$config['per_page'] = 50;
$this->pagination->initialize($config);
/* View */
// Print it in the view file
echo $this->pagination->create_links();
More info: ellislab documentation

Related

Pagination on category home page

i have a blog where posts are shown in under different category id. In category home page i want to show 10 posts per page. But my pagination is not working. Can anybody help please?
//////////Controller///////
public function categoryDetails($category_id)
{
$data = array();
$this->load->library('pagination');
$config['base_url'] = base_url() . 'Welcome/categoryDetails/'.$category_id;
$data['total_rows'] = $this->WelcomeModel->select_number_of_published_blog_by_category_id($category_id);
$config['per_page'] = 2;
$this->pagination->initialize($config);
$data['allPost'] = $this->SuperEditorModel->select_all_blog_post_with_category($category_id,$config['per_page'], $this->uri->segment(3));
$this->load->view('frontend/master', $data);
You have to use create_links() function to load the pagination links. You can add it to your data array and pass it to the view
$data["pagination_links"] = $this->pagination->create_links();
$this->load->view('frontend/master', $data);
From the documentation:
create_links()
Returns: HTML-formatted pagination
Return type: string
Returns a “pagination” bar, containing the generated links or an empty
string if there’s just a single page.
add this in your controller to create pagination link
$data["links"] = $this->pagination->create_links();
And your view page where are you want to view pagination link
<?php echo $link; ?>

How to fix codeigniter pagination and route

I have problem whit codeignitet pagination and route!
I set the route my function class like this:
$route['admin/panel/students/new-stuedents-list/:num'] = "admin/newStuedentsList/$1";
then in my controller, I create a function that call newStuedentsList and load pagination library, every things work fine but pagination nav... :(
the page loaded successfully...
and the data is correct...
bat when I click for example page 2 , the page 2 loaded successfully but the pagination nav show page i button ...!
when I call page 4 form url (http:// localhost/d/index.php/admin/panel/students/new-stuedents-list/30) , again the data is correctly ... but the pagination nav show page 1 button and the number of page don't change!
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/d/index.php/admin/panel/students/new-stuedents-list/';
$config['total_rows'] = $this->db->get('new_contest')->num_rows();
$config['pre_page'] = 10;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$adminInfo = $this->admin_model->adminInfo();
$newStudentsList['students'] = $this->admin_model->newStudentsList($config['pre_page'],$this->uri->segment(5));
$data = array_merge($adminInfo,$newStudentsList);
$this->load->view('admin/newStudentsList',$data);
but when the newStuedentsList is:
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/d/index.php/admin/newStuedentsList/';
$config['total_rows'] = $this->db->get('new_contest')->num_rows();
$config['pre_page'] = 10;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$adminInfo = $this->admin_model->adminInfo();
$newStudentsList['students'] = $this->admin_model->newStudentsList($config['pre_page'],$this->uri->segment(3));
$data = array_merge($adminInfo,$newStudentsList);
$this->load->view('admin/newStudentsList',$data);
every things work fine!
How do I fix this problem ...?
You forgot one configuration:
$config['uri_segment'] = 5;
To determines which segment of your URI contains the page number.
And replace pre_page by per_page

Pagination past limit in codeigniter

The code listing below is in a function in a message controller. I want that a page to only have 5 records at a time on a page. Using pagination class in codeignuter i have set the limits meaning that if a page has only three records then in the pagination link in the view only one is shown e.g < 1 and not < 1 2. This also happens with the last page, if the pages are 8 it shows 9 thus <... 8 9. What am i missing
$data['message_available'] = $this->getCheckForMessages();
$config["base_url"] = base_url() . "messages/inbox/";
$config['total_rows'] = $this->getMessageCount();
$config['per_page'] = 5;
$config["num_links"] = 10;
$config["full_tag_open"] = '<div id="pagination">';
$config["full_tag_close"] = '</div>';
$this->pagination->initialize($config);
$data['messages'] = $this->getMessages($config['per_page'], $this->uri->segment(3));
$data['links'] = $this->pagination->create_links();
You have to check deeply this :
$config['total_rows'] = $this->getMessageCount();
maybe is returning some field more than the Results query getMessages();
please post also your getMessages() and getMessageCount(); queries to be more accurated
also please change this:
$config["base_url"] = base_url() . "messages/inbox/";
to this:
$config["base_url"] = site_url('messages/inbox');

Pagination in codeigniter

Help me my url in pagination ! :(
$data['total'] = $this->news_model->all_list();
$this->load->library('pagination');
$config['base_url'] = site_url().'admin/news/page';
$config['total_rows'] = count($data['total']);
$config['per_page'] = '5';
$this->pagination->initialize($config);
$data['columns'] = array('Tile', 'Date', 'User', 'Active', 'Edit', 'Delete');
$data['list'] = $this->tintuc_model->all_list($config['per_page'],$this->uri->segment(3));
$data['pagination'] = $this->pagination->create_links();
$this->smarty->view( 'admin/news.tpl', $data );
That's my code. List of pages showed ok. But, when all links are wrong.
eg: when i click the 2 page link => http://localhost/mysite/admin/news/page/5 ?
Why it is 5 instead of 2 ?
you can change
$config['per_page'] = '5';
to
$config['per page'] = '2';
in order to get 2 in your uri segment, but it indicates the limit of data to be displayed in your page not the clicked pagination number.
It's not the page number - it's page number * items per page
In your case $config['per_page'] = '5'; So everything seems to be okay.
You need to set:
$config['use_page_numbers'] = TRUE;
http://ellislab.com/codeigniter/user-guide/libraries/pagination.html

CodeIgniter routes and pagination adding “/page/” to all links

I’ve implemented pagination like the following:
$this->load->library('pagination');
$perpage=10;
$config['base_url'] = site_url().'news/page';
$config['total_rows'] = $this->news_model->getnews(array('count' => true));
$config['per_page'] = $perpage;
$config['uri_segment'] = 3;
$config['num_links'] = 8;
$news = $this->news_model->getnews(array('limit' => $perpage,'offset'=>$offset));
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['news'] = $news;
$data['page'] = "news";
$this->load->view('index', $data);
I’m also using the following routes:
$route["news"] = "news/news_list";
$route["news/page"] = "news/news_list";
$route["news/page/(:num)"] = "news/news_list/$1";
$route["news/detail/(:any)"] = "news/news_detail/$1";
The problem that I’m facing is that although the pagination is working fine when i go to the second page or any other page after clicking on the pagination links - all of my other links on the page get the /page/ added in front of them like -> /page/detail/aaaaaa so that my route $route["news/detail/(:any)"] = "news/news_detail/$1"; can not identify it as the detail link.
Why is the /page/ added to all of the links? Do i need any routes for Pagination?
Your $config['base_url'] is news/page, that’s why /page is added to all your links.
I don’t think you need these routes for pagination, but if you want them, you should use these routes in $config['base_url'].
$route["news/page/(:num)"] = "news/news_list/$2";
$route["news/detail/(:any)"] = "news/news_detail/$1";

Resources