Codeigniter 3 pagination links and single post link throw 404 - codeigniter

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/';

Related

Page number url pagination CodeIgniter

I use pagination CodeIgniter. I want to remove page number from url or show page number 1,2,3 not 5,10,15. How can I do that please help me I'm beginner at CodeIgniter.
Thanks
Here is my action inside my controller
class Wiki_egypt extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper("url");
$this->load->model('Tours_model');
$this->load->model('Wiki_egypt_model');
$this->load->model('Meta_model');
$this->load->library("pagination");
}
public function Wikipedia_egypt() {
$config = array();
$config["base_url"] = base_url() . "/wikipedia-egypt";
$config["total_rows"] = $this->Wiki_egypt_model->record_count();
$config["per_page"] = 5;
$config["uri_segment"] = 2;
$config['num_links'] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$data["wikis"] = $this->Wiki_egypt_model->
fetch_departments($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$data['meta_tags'] = $this->Meta_model->meta_wikis_eg();
$this->load->view("Header", $data);
$this->load->view("Nav", $data);
$this->load->view("wiki_egypt/wikieg", $data);
$this->load->view("Footer");
}
}
Here is my action inside my Model
public function record_count() {
return $this->db->count_all("wiki_egypt");
}
public function fetch_departments($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->order_by('id', 'DESC')->get("wiki_egypt");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
Here is my action inside my View
<?php echo $links; ?>
<?php
foreach($wikis as $row)
{
?>
<?php
} ?>
First you need another config there:
$config['use_page_numbers'] = TRUE;
Then you need to change the way your page variable works.
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 1;
$page = $config['per_page'] * $page;
So now you're saying that your offset will be equal to the url page times the offset.
All the rest should work just the same.

ci pagination shows same pages on all links

I am having the pagination kept in my codeigniter project. The links are all shown in the view page. But whichever link that I clicked is getting me to the same page...
The following is my controller code..
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"] = 1;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = $total_row;
$config['cur_tag_open'] = ' <a class="current">';
$config['cur_tag_close'] = '</a>';
$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');
$data['rm_newsletter'] = $this->newsletter_model->get_rm_newsletter();
$this->load->view('newsletter/newsletter',$data);
}
The following is my view code:
foreach ($lt_newsletter as $letter) {
echo $newsletter['nl_newsletter']; }
<div id="pagination">
<ul class="pagination">
<?php foreach ($links as $link)
{
echo "<li>". $link."</li>";
} ?>
</ul>
</div>
And finally in my model:
public function fetch_data($limit, $id)
{
$this->db->limit($limit);
$this->db->where('nl_id', $id);
$query = $this->db->get("ins_newsletter");
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data[] = $row;
}
}
}
public function record_count()
{
return $this->db->count_all("ins_newsletter");
}
public function get_rm_newsletter()
{
$data = $this->db->query('SELECT nl.* FROM ins_newsletter nl ORDER BY nl.nl_id DESC');
return $data->result_array();
}
In my browser all the links(1,2,3,4) are shown clearly, but after I click on every link(1,2,3,4), it is showing the same page with all the records. For presently, I have 4 records in my database and it lists all. Kindly point me where am i going wrong...! Thanks in Advance.
Maybe the error is in your model, I see that you have a
$this->db->where('nl_id', $id);
but in the function in the controller you pass two parameters
$data["results"] = $this->newsletter_model->fetch_data($config["per_page"], $page);
this are the pagination values, one for quantity of record,(not variable), and the other for the page you are passing (1,2,3,etc.)
So, you have to add another parameter to make the where. See this link in pagination does not work and if you have some other problem can tell me.

Link Pagination does'nt shown on 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

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