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; ?>
Related
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
I am trying to use pagination for blog articles on my homepage:
Controller:
public function index()
{
$data['view'] = "home";
/** Pagination **/
$config['base_url'] = base_url().'home/';
$config['total_rows'] = $this->Blog_model->count_articles();
$config['per_page'] = 2;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$data['paginate'] = $this->pagination->create_links();
$data['articles'] = $this->Blog_model->get_articles($config['per_page'],$this->uri->segment(2));
$this->load->view('template', $data);
}
Everything seems to work ok with the information retrieval and the pagination however, when I click on the number links or next links I get a 404 Not Found error.
I am assuming that this has something to do with the URI segment?
The URL that is being loaded is http://www.example.com/home/2 for the second page.
You can also add a routing rule like:
$route['home/(:num)'] = 'yourhomecontroller';
then you can use it without index or any method, the num tells it to route any url with a number after home/ to index of home
Codeigniter pages are of the form http://domain.com/controller/method/arguments.
If you leave out the method the default one will load, but if you need to pass an argument, you'll have to put the method as it comes before.
http://www.example.com/home/index/2
controller code
public function index()
{
$home=$this->uri->segment(2);
$limit_ti=2;
if(!$home):
offset_ti = 0;
else:
$offset_ti = $home;
endif;
$this->load->model('Blog_model');// call model
$this->load->library('pagination'); // call library
$query=$this->Blog_model->get_articles($limit_ti,$offset_ti); // geting aan article
$total_page = $this->Blog_model->count_articles(); // count row
/** Pagination **/
$config['base_url'] = base_url().'index.php/home/'; // assuming you doesn't have htaccess
$config['total_rows'] =$total_page->num_rows();
$config['per_page'] = 2;
$config['uri_segment'] = 2;
$this->pagination->initialize($config);
$data = array('query' => $query,'page'=>$home);
$data['view'] = "home";
$this->load->view('template', $data);
}
model code
function get_articles($limit,$ofset){
$query=$this->db->query("select * from *table* order by id ASC LIMIT $ofset,$limit");
return $query;
}
function count_articles(){
$query=$this->db->query("select * from table");
return $query;
}
in view
<?php echo $this->pagination->create_links(); // call the pagnation?>
I am using CI 2.1.0 with HMVC. I am going to implement pagination in view part. But I get 'Undefined table data' error. It displays paging number (1,2,3,..) but does not display records.
My code is :
included library for pagination :
$this->load->library('table');
controller
$data['title'] = "Test Events";
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/myproject/index.php/events/test/';
$config['total_rows'] = $this->eventModule->countRow();
$config['per_page'] = '3';
$this->pagination->initialize($config);
$config['records'] = objectsIntoArray($this->eventModule->get_record($config['per_page'], $this->uri->segment(3)));
$data['main_content'] = 'test';
$this->load->view('event/template', $data);
view
<?php echo $this->table->generate('records'); ?>
<?php echo $this->pagination->create_links(); ?>
Thank you for support.
Finally I get the solution. It's silly mistake at
$config['records'] = objectsIntoArray($this->eventModule->get_record($config['per_page'], $this->uri->segment(3)));
It should be $data instead of $config
$data['records'] = objectsIntoArray($this->eventModule->get_record($config['per_page'], $this->uri->segment(3)));
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";
Okay, I am pretty new in CI and I am stuck on pagination. I am performing this pagination on a record set that is result of a query. Now everything seems to be working fine. But there's some problem probably with the link. I am displaying 10 results per page. Now if the results are less than 10 then it's fine. Or If I pull up the entire records in the table it works fine. But in case the result is more than 10 rows, then the first 10 is perfectly displayed, and when I click on the pagination link to get to the next page the next page displays the rest of the results from the query as well as, other records in the table. ??? I am confused.. Any help??
Here's the model code I am using ....
function getTeesLike($field,$param)
{
$this->db->like($field,$param);
$this->db->limit(10, $this->uri->segment(3));
$query=$this->db->get('shirt');
if($query->num_rows()>0){
return $query->result_array();
}
}
function getNumTeesfromQ($field,$param)
{
$this->db->like($field,$param);
$query=$this->db->get('shirt');
return $query->num_rows();
}
And here's the controller code ....
$KW=$this->input->post('searchstr');
$this->load->library('pagination');
$config['base_url']='http://localhost/cit/index.php/tees/show/';
$config['total_rows']=$this->T->getNumTeesfromQ('Title',$KW);
$config['per_page']='10';
$this->pagination->initialize($config);
$data['tees']=$this->T->getTeesLike('Title',$KW);
$data['title']='Displaying Tees data';
$data['header']='Tees List';
$data['links']=$this->pagination->create_links();
$this->load->view('tee_res', $data);
What am I doing wrong here ???? Pls help ...
I guess the problem is with the $KW=$this->input->post('searchstr'); ..
Because if I hard code a value for $KW it works fine. May be I should use POST differently ..but how do I pass the value from the form without POSTING it , its CI so not GET ... ??????
In Controller
<?php
$KW=$this->input->post('searchstr');
$this->load->library('pagination');
$count = $this->model_name->getNumTeesfromQ($KW);//Count
$config['base_url']= base_url().'index.php/tees/show/';
$config['total_rows']= $count;
$config['per_page']='10';
$config['uri_segment'] = 4;
$limit = $config['per_page'];
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$data['links'] = $this->pagination->create_links();
$data['tees']=$this->model_name->getTeesLike($KW,$limit,$page);
$data['title']='Displaying Tees data';
$data['header']='Tees List';
$this->load->view('tee_res', $data);
In Model
public function getNumTeesfromQ($KW)
{
$query = $this->db->query("SELECT * FROM title WHERE table_field='$KW'");
$result = $query->result_array();
$count = count($result);
return $count;
}
public function getTeesLike($KW,$limit,$page)
{
$query = $this->db->query("SELECT * FROM title WHERE table_field='$KW' LIMIT $page, $limit");
$result = $query->result_array();
return $result;
}
in view
echo all data using foreach
then at bottom of page use <?php echo $links; ?>this will show your Pagination