codeigniter pagination error 404 - codeigniter

I have Admin controller in codeigniter
class Admin extends CI_Controller {
function __construct() {
parent::__construct();
if (!$this->tank_auth->is_logged_in()) redirect('login');
$this->load->library('pagination');
}
function index() {
$offset = $this->uri->segment(2);
$config['per_page'] = 3;
$data['sitetitle'] = 'Výpis jobů';
$data['listings'] = $this->Jobs_model->get_listings(0,$user_id = FALSE,$config['per_page'],$offset);
$config['uri_segment'] = 2;
$config['base_url'] = base_url().'admin/';
$config['total_rows'] = $this->db->count_all_results('jobs');
$this->pagination->initialize($config);
$this->template->set('title', 'Domovská stránka');
$this->template->load('template', 'site', $data);
}
}
and Jobs_model
function get_listings($category, $user_id = false, $limit = 0, $offset = 0) {
$data = array();
$this->db->order_by('id', 'desc');
$q = $this->db->get('jobs');
if ($category) {
$options = array('category' => $category);
$this->db->order_by('id', 'desc');
$this->db->where('category', $category);
$q = $this->db->get('jobs', $limit, $offset);
}
else {
$query = $this->db->order_by('id', 'desc');
if ($user_id) $query = $query->where('user_id', $user_id);
$q = $query->get('jobs',$limit, $offset);
}
if ($q->num_rows() > 0) {
foreach ($q->result_array() as $row) {
$data[] = $row;
}
}
$q->free_result();
return $data;
}
first page in paginatiom obtain data, but links generating in pagination localhost/sitename/admin/3 produce 404 error.
Where is problem in my script

You need to change:
$config['base_url'] = base_url().'admin/';
To:
$config['base_url'] = base_url().'admin/index/';
If you need the url to be like admin/3, you can use a route or _remap.
Side note: consider using this to get your page number rather than the URI class:
function index($offset = 0) {
// your code
}
It will do the same thing, but it's convenient to use the controller method arguments when possible.

You need to do some change.
$config['base_url']=base_url().'admin/index';
change
$config['uri_segment'] = 3;
check if you are using .htaccess or not. If you aren't, then $config['base_url'] in above should be
$config['base_url']=base_url().'index.php/admin/index';

If you get 404 error on codigniter paggination please check follwing things :
1 ) Check your controller it's index or somthing else if it's index then base_url should be :
base_url().'admin/index/pages' and if it's somethings else it should be :
base_url().'admin/somethingelse/pages'
2 ) Check 'uri_segment' by $this->uri->segment(4) so by this you got the page number then else will do the codiegniter.

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.

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);

Two lots of pagination with codeigniter on one page

I have to lots of paginations one lot is for my user's results and the other is for my question results.
When I am on link 3 on my question results it also switches the results on users list pagination.
Question if I click on the questions pagination links how can I make sure it does not affect the results of the user's list.
I have tried this Best way to do multiple pagination on one page in codeigniter does not work
As you can see in image below because I am on questions list page 3 it has effected the users list
<?php
class Dashboard extends MY_Controller {
public $data = array();
public function __construct() {
parent::__construct();
$this->load->model('user/user_model');
$this->load->model('forum/question_model');
$this->load->library('pagination');
}
public function index() {
$this->data['title'] = 'Dashboard';
$this->data['is_logged'] = $this->session->userdata('is_logged');
$config1['base_url'] = base_url('dashboard/');
$config1['total_rows'] = $this->user_model->total_users();
$config1['per_page'] = 2;
$config1['uri_segment'] = 2;
$config1['num_links'] = 200;
$config1['use_page_numbers'] = FALSE;
$config1['prefix'] = 'u_';
$pagination1 = new CI_Pagination();
$pagination1->initialize($config1);
$this->data['pagination1'] = $pagination1->create_links();
$page_segment1 = explode('_', $this->uri->segment(2));
$page1 = ($this->uri->segment(2)) ? $page_segment1[1] : 0;
$this->data['users'] = array();
$users = $this->user_model->get_users($config1['per_page'], $page1);
foreach ($users as $user) {
$this->data['users'][] = array(
'user_id' => $user['user_id'],
'username' => $user['username'],
'status' => ($user['status']) ? 'Enabled' : 'Disabled',
'warning' => '0' . '%',
'date' => date('d-m-Y H:i:s A', $user['date_created_on']),
'href' => site_url('user/profile/') . $user['user_id']
);
}
// Questions Pagination & Results
$config2['base_url'] = base_url('dashboard/');
$config2['total_rows'] = $this->question_model->total_questions();
$config2['per_page'] = 2;
$config2['uri_segment'] = 2;
$config2['num_links'] = 200;
$config2['use_page_numbers'] = FALSE;
$config2['prefix'] = 'q_';
$pagination2 = new CI_Pagination();
$pagination2->initialize($config2);
$this->data['pagination2'] = $pagination2->create_links();
$page_segment2 = explode('_', $this->uri->segment(2));
$page2 = ($this->uri->segment(2)) ? $page_segment2[1] : 0;
$this->data['questions'] = array();
$questions = $this->question_model->get_questions($config2['per_page'], $page2);
foreach ($questions as $question) {
$this->data['questions'][] = array(
'user_id' => $question['user_id'],
'title' => $question['title']
);
}
$this->data['navbar'] = $this->load->view('common/navbar', $this->data, TRUE);
$this->data['header'] = $this->load->view('common/header', $this->data, TRUE);
$this->data['footer'] = $this->load->view('common/footer', '', TRUE);
$this->load->view('common/dashboard', $this->data);
}
}
Question Model
<?php
class Question_model extends CI_Model {
public function get_questions($limit, $start) {
$this->db->select('*');
$this->db->from('questions q');
$this->db->join('users u', 'u.user_id = q.user_id', 'left');
$this->db->limit($limit, $start);
$query = $this->db->get();
return $query->result_array();
}
public function total_questions() {
return $this->db->count_all("questions");
}
}
Users Model
<?php
class User_model extends CI_Model {
public function get_users($limit, $start) {
$this->db->select('u.status, u.date_created_on, ud.*');
$this->db->from('users u', 'left');
$this->db->join('users_userdata ud', 'ud.user_id = u.user_id', 'left');
$this->db->limit($limit, $start);
$query = $this->db->get();
return $query->result_array();
}
public function total_users() {
return $this->db->count_all("users");
}
}

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>

codeigniter pagination Numbers not displayed correcly

Codeigniter pagination Numbers not displayed correcly in result page,.
Plz help me.
/controller page code/
function search_all()
{
$this->load->view(‘prd’);
$config = array();
$config[‘base_url’] = base_url().’/product_search/search_all’;
$config[“total_rows”] = $this->search_product_model->record_count();
$config[“per_page”] = 8;
$config[“uri_segment”] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data[“results”] = $this->search_product_model->
get_search($config[“per_page”], $page);
$data[“links”] = $this->pagination->create_links();
//$this->load->view(“example1”, $data);
$this->load->view(‘asd’, $data);
}
I think there is an problem with this
$config[“total_rows”] = $this->search_product_model->record_count();
For this you are not passing the search keys such that it will count all the data which it contains.If you get the record count based on search key then it will be fine.Like this
$config[“total_rows”] = $this->search_product_model->record_count('//search parameters');
and in your model:
Public function record_count($search_params)
{
$this->db->where('product',$search_params);
return $this->db->count_all(“products”);
}
You can modify as your needs like pass "color","type" to your record_count() function and based on that,you suggested to get "count_all".I hope it will works for you
Try this
function search_all()
{
$config[‘base_url’] = base_url(’product_search/search_all’);
$config[“total_rows”] = $this->search_product_model->record_count();//2 in your case
$config[“per_page”] = 8;
$config[“uri_segment”] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data[“results”] = $this->search_product_model->
get_search($config[“per_page”], $page);
$data[“links”] = $this->pagination->create_links();
//$this->load->view(“example1”, $data);
$this->load->view(‘asd’, $data);
}
and at your model:
public function get_search($limit, $page)
{
//Add this...???
$this->db->limit($limit, $page); //or try $this->db->limit($page,$limit)
}
give search key in the record_count() function as parameters //You got 2 na.??
Then in placeof search_params put you same keys which are supplied to the record_count()

Resources