Link Pagination does'nt shown on codeigniter - codeigniter

I have to add pagination to my site and the link does not appear / shown
I Have an controller like this :
$data['a'] = 9;
$data['title'] = 'Articles';
$data['content'] = 'home/articles';
$this->load->library('pagination');
$q = $this->db->get_where('articles',array('article_category_id' => 18));
$config['uri_segment'] = 2;
$config['total_rows'] = $q->num_rows();
$config['per_page'] = 3;
$config['base_url'] = base_url() . 'article/';
$this->pagination->initialize($config);
$this->db->order_by('id','desc');
$this->db->limit($config['per_page'],$this->uri->segment(2));
$q = $this->db->get_where('articles',array('article_category_id' => 18));
$data['blogs'] = $q;
$data['pagination'] = $this->pagination->create_links();
$this->load->view('home/index', $data);
and view like this :
//Body of the site
Halaman : <?php echo $pagination; ?>
Site View

Related

Why is my pagination in codeigniter not working? (404 not found)

I already follow the step and tutorial. But it's not working.
I already follow tutorial from internet step by step. But it always show 404 page not found when I go to second page
This is my controller:
public function index()
{
$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/blog';
$config['uri_segment'] = 2;
$config['per_page'] = 2;
$config['total_rows'] = $this->mod_blog->getAllData()->num_rows();
$this->pagination->initialize($config);
// $page = ($this->uri->segment(2)) ? ($this->uri->segment(2) - 1) : 0;
$page = $this->uri->segment(2);
$data = array(
'data' => $this->mod_blog->getData($config['per_page'],$page)->result(),
'pagination' => $this->pagination->create_links()
);
$this->load->view('layout/header');
$this->load->view('layout/sidebar');
$this->load->view('admin/blog',$data);
$this->load->view('layout/footer');
}
This is my Model:
function getData($limit,$start){
$this->db->select('*');
$this->db->from('berita');
$this->db->join('admin','admin.id_admin = berita.id_admin');
$this->db->limit($limit,$start);
$this->db->order_by('id_berita','desc');
return $this->db->get();
}
function getAllData(){
$this->db->select('*');
$this->db->from('berita');
$this->db->join('admin','admin.id_admin = berita.id_admin');
$this->db->order_by('id_berita','desc');
return $this->db->get();
}
Whoops, I found my mistake.
It's in the
$config['base_url'] = base_url().'index.php/blog';
You must point the config['base_url'] to your baseUrl/controllerName/functionName. So, I changed it into.
$config['base_url'] = base_url().'index.php/blog/index';
And then I adjust my Uri segment into 3
$config['uri_segment'] = 3;
$page = $this->uri->segment(3);

Codeigniter 3 pagination links and single post link throw 404

I have read other articles on this from here but could not solve this problem. My problems are 2.
Pagination shows but clicking links of pagination generates 404.
I have list of articles in db that are being displayed but when I
click single article link for single article page, it shows 404.
I am on localhost wamp. Here is my code. The commented code is what I have been trying but did not work for me.
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
routes.php
$route['default_controller'] = 'home';
$route['404_override'] = 'not_found';
$route['translate_uri_dashes'] = FALSE;
$route['articles/(:any)'] = 'articles/$1';
//$route['articles'] = 'articles';
config / baseurl
$config['base_url'] = 'http://localhost/practice/project-code-igniter/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
Controller - Articles.php
class Articles extends CI_Controller {
public function index($start=0)
{
// $this->output->cache('86400');
$this->load->view('header');
//load model
// $this->load->model('articles_model');
// load 'get_articles' function from 'articles_model' model and store it in data
$this->load->library('pagination');
$data['articles']=$this->articles_model->get_articles(5,$start);
$config['base_url'] = base_url().'articles/';
$config['total_rows'] = $this->db->get('articles')->num_rows();
$config['per_page'] = 5;
$config["uri_segment"] = 3;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = 2; //NUMBER OF LINKS BEFORE AND AFTER CURRENT PAGE IF ON PAGE ONE WILL SHOW 4 PAGES AFTERWARDS IF YOU HAVE ENOUGH RESULTS TO FILL THAT MANY
//config for bootstrap pagination class integration
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = "<< First";
$config['last_link'] = "Last >>";
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '&laquo';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '&raquo';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
//$data['pages'] = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
//$page = ($this->uri->segment(3))? $this->uri->segment(3) : 0;
// //call the model function to get the department data
//$data['pages'] = $this->articles_model->get_articles($config["per_page"], $data['page']);
$data['pages'] = $this->pagination->create_links();
//$this->load->view('page_articles');
// load view and load model data with it
$this->load->view('page_articles',$data);
$this->load->view('footer');
}
// single article
// function post($perma){
// $this->load->view('header');
// $this->load->model('articles_model');
// $data['articles']=$this->articles_model->get_single_article($perma);
// $this->load->view('page_article',$data);
// $this->load->view('footer');
// }
}
Model / Articles_model
class Articles_model extends CI_Model {
// function get_articles() {
public function get_articles($limit, $start) {
// $query = $this->db->query("SELECT * FROM articles");
// $this->db->select()->from('articles');
// $this->db->select()->from('articles')->where('active',1)->order_by('date_added','desc')->limit(0,20);
$this->db->select()->from('articles')->where('status','1')->order_by('date_added','desc')->limit($limit, $start);
$query = $this->db->get();
// return object
// return $query->result();
// return array
return $query->result_array();
}
function get_articles_count(){
$this->db->select('id')->from('articles')->where('status',1);
$query = $this->db->get();
return $query->num_rows();
}
// for single article
// function get_single_article($perma){
// $this->db->select()->from('articles')->where(array('status'=>1,'permalink'=>$perma));
// $query = $this->db->get();
// return $query->first_row('array');
// }
}
View / page_articles
<?php
if(!isset($articles)) { ?> <div class="alert alert-info">No records</div> <?php } else
{
?>
<ol>
<?php
foreach($articles as $row)
{ ?>
<li>
<h4><a href="<?php echo base_url(); ?>articles/<?php echo $row['id']; ?>/<?php echo $row['permalink']; ?>/">
<?php echo $row['name']; ?>
</a></h4>
<p><?php echo substr(strip_tags($row['detail']),0,100).".."; ?>
<br>
Read more
</p>
<br>
</li>
<?php } ?>
</ol>
<?php
}
?>
</p>
<div>
<?php echo $pages; ?>
</div>
One problem...
public function index($start=0)
you seem to use $start for your page number and again for your offset value...
$data['articles']=$this->articles_model->get_articles(5,$start);
If $start is representing your page number, then you cannot also use it for your offset value.
Your offset needs to be calculated...
$offset = ($start - 1) * 5;
$data['articles']=$this->articles_model->get_articles(5, $offset);
Example:
With 5 records per page, page 4 would start with record #16. So 15 is the correct offset to get records #16, 17, 18, 19, & 20 for page 4.
Another potential issue...
$config["uri_segment"] = 3;
You've set the page number as segment 3, but your route is showing the page number at segment 2...
$route['articles/(:any)'] = 'articles/$1';
It also does not match this setting, which would also put your page number at segment 2...
$config['base_url'] = base_url().'articles/';
Based on your comment,
waow. What I did just now... I changed $config['base_url'] = base_url().'articles/'; to $config['base_url'] = base_url().'articles/index/'; and the pagination links show the page now but records are not changing. The 404 goes away and page is changing - records are first 5 on all pages....
then the route should be this...
$route['articles/index/(:any)'] = 'articles/$1';
So it matches the new base_url you describe...
$config['base_url'] = base_url().'articles/index/';

pagination in codeigniter - weird working

My pagination not working properly.
For example my $count = 3, $limit = 1 so i should have 3 pages
And this pagination look like this: 1 2 3 >
1 - Its not clickable and always bold (as active)
2 - When i click on it my uri segment(4) change on '1', dont change on bold (as active)
3 - When i click on it my uri segment(4) change on '2', dont change on bold (as active)
' > ' - Working only when i dont have uri segment(4) and when i click segment(4) change on '1' but when i click again its not working. When segment(4) = '2' and click it change on '1'.
' < ' - doesnt show at all
Controller:
$value = $this->uri->segment(3);
$this->load->library('pagination');
$limit = 1;
$offset = $this->uri->segment(4);
$this->db->limit($limit, $offset);
$this->db->where('when_data >='. date('Y-m-d'));
$this->db->select('*');
$this->db->from('meeting');
$this->db->join('category', 'category.id = meeting.id_cat');
$this->db->join('users', 'users.id = meeting.id_p');
$data['meeting'] = $query = $this->db->get();
$count = $query->num_rows();
$config['base_url'] = base_url() . 'main/kategoria/'. $value;
$config['total_rows'] = $count;
$config['per_page'] = $limit;
$config['first_link'] = 'First';
$config['last_link'] = 'Last';
$config['cur_tag_open'] = '<b>';
$config['cur_tag_close'] = '</b>';
$config['next_link'] = '>';
$config['prev_link'] = '<' ;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('spotkania/kategoria', $data);
View:
<?php echo $pagination; ?>
Any idea?
you missed to add this config
$config['uri_segment'] = 4;//your case its 4.

Clicking on CI Pagination links produces wrong queries

I'm currently having problem when clicking on they pagination link of my site using CodeIgniter. The search function works fine, however, when I click on any of the pagination links, it seems like it does the filter again and again.
For example:
I choose "Rent" as my choice to search for:
Then click search I get 27 results. After that I click on the "2" pagination button at the bottom:
I get another 73 results and so on and so forth.
My Controller:
$this->load->library('pagination');
$search['base_url'] = base_url() . 'page_search/';
$search['per_page'] = 5;
$search['num_links'] = 5;
$search['num_tag_open'] = '<li>';
$search['num_tag_close'] = '</li>';
$search['first_link'] = 'First';
$search['first_tag_open'] = '<li class="first">';
$search['first_tag_close'] = '</li>';
$search['last_tag_open'] = '<li class="last">';
$search['last_tag_close'] = '</li>';
$search['next_tag_open'] = '<li>';
$search['next_tag_close'] = '</li>';
$search['prev_tag_open'] = '<li>';
$search['prev_tag_close'] = '</li>';
$search['last_link'] = 'Last';
$search['next_link'] = '»';
$search['prev_link'] = '«';
$search['full_tag_open'] = '<div class="pagination pagination-centered"><ul>';
$search['full_tag_close'] = '</ul></div>';
$search['cur_tag_open'] = '<li class="active"><a href="#">';
$search['cur_tag_close'] = '</a></li>';
$search['total_rows'] = $this->db->get_where( 'listing', $where )->num_rows();
$search['uri_segment'] = 2;
$config['use_page_numbers'] = TRUE;
$this->pagination->initialize( $search );
$this->db->order_by("id", "desc");
$this->db->where( $where );
$data['results'] = $this->db->get( 'listing', $search['per_page'], $this->uri->segment(2);
$data['rows'] = $this->db->get_where( 'listing', $where )->num_rows();
$data['content'] = 'website/page-search';
$this->load->view('template/website/theme', $data);
Which $where is the query which I am sure it works fine.
UPDATE:
Also I have this in my route:
$route['page_search/(:num)'] = "page_search/index/$1";
Here's the link to the live site http://angkor21.com/
You are posting your search form and making your $where statement, thats cool, but in page 2, is your $where statement working correctly? I think this is where you need to watch out for. Print your generated query in the second page like this:
$this->db->last_query();
You need to somehow pass on these search parameter(s) to the next page to correctly generate the $where condition. For example:
if( $this->input->post(null) ){ #if the form is submitted
$saleType = $this->input->post('sale_type');
$propType = $this->input->post('prop_type');
$city = $this->input->post('city');
$district = $this->input->post('district');
$commune = $this->input->post('commune');
}else{
$saleType = $this->uri->segment(3);
$propType = $this->uri->segment(5);
$city = $this->uri->segment(7);
$district = $this->uri->segment(9);
$commune = $this->uri->segment(11);
}
$search['base_url'] = base_url() . 'page_search/sale_type/'.$saleType.'/prop_type/'.$propType.'/city/'.$city.'/district/'.$district.'/commune/'.$commune.'/page/';
$search['uri_segment'] = 13;

pagination codeigniter always starts at page 2 and active link is always at page 2

i'm new to codeigniter and i'm using codeigniter pagination by getting query result from Category Id, but the page always starts at page 2 .
how do i fix the pagination to set the start page at number page 1?
public function kategori($kat=''){
$config['base_url'] = base_url().'index.php/lowongan/kategori/'.$kat;
$data['db']=$this->query->katPostingan($kat);
$config['total_rows'] = $data['db']->num_rows();
$config['per_page'] = 1;
$config['uri_segment'] = 4;
$this->pagination->initialize($config);
$data2['paging'] = $this->pagination->create_links();
$page = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 0;
$data2['records'] = $this->query->katPos($kat, $config['per_page'], $page);
$this->load->view( 'user/hal_kategori',$data2);
}
and my model
public function katPos($kat='',$limit,$start){
$this->db->where("id_kat", $kat);
$this->db->limit($limit,$start);
$query = $this->db->get("showKat");
return $query;
}
public function katPostingan($kat=''){
$this->db->where("id_kat", $kat);
$query = $this->db->get("showKat");
return $query;
}
try a different way of solution,
controller(index())
$this->load->library('pagination');
$config['base_url'] = site_url().'classname/index';
$config['per_page'] = $this->config->item('pagination_number');
$config['uri_segment'] = 4;
$data['count']=$this->example_model->count();
$config['total_rows'] =$data['count'];
$data['classname']=$this->example_model->getall($this->config->item('pagination_number'),$this->uri->segment(4));
$this->pagination->initialize($config);
$data['page_link']=$this->pagination->create_links();
$this->load->view('users/classname.php',$data);
model
function getall($num,$offset)
{
$this->db->from('table_name');
$this->db->limit($num,$offset);
$query = $this->db->get();
return $query->result_array();
}
function count()
{
$this->db->from('table_name');
$query = $this->db->get();
$rowcount = $query->num_rows();
return( $rowcount);
}
View
<div class="pagination"> <?php echo $page_link;?> </div>

Resources