I'm working on the pagination for codeigniter. I was able to set it on my page but when I click the next page it will say
Object not found! Error 404.
My is correctly placed when I click the next, which is http://localhost/contents/index/2. Also another thing I noticed is that $config['per_page'] = 2; yet when I load the page all the data that was looped from a database is shown in 1 page instead of 2 per page.
Controller
//Pagenation below
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/contents/index';
$config['total_rows'] = 200;
$config['per_page'] = 2;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
$data['contents'] = $this->content_model->get_content();
$data['title'] = 'Contents';
$this->load->view('template/header', $data);
$this->load->view('pages/index', $data);
$this->load->view('template/footer');
try this
function index($offset=null){
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/contents/index/';
$config['total_rows'] = 200;
$config['per_page'] = 2;
$this->pagination->cur_page = $offset;
$this->pagination->initialize($config);
$data['contents'] = $this->content_model->get_content($config['per_page'],$offset);
$data['title'] = 'Contents';
$this->load->view('template/header', $data);
$this->load->view('pages/index', $data);
$this->load->view('template/footer');
}
whole data showing in 1 page because you are not passing limit in model function.you getting 404 because you forgot / at the end of the base_url
in model
function get_content($limit,$offset) {
$query = $this->db->get('content',$limit,$offset);
return $query->result();
}
in view
echo $this->pagination->create_links(); //put this in view not in controller
use have to write the routes to get the values of the page number . You have to add this line to your routes.php in application/config folder
CODE: Routes.php
$route['index/(:num)'] = 'index/$1';
index function in index.php :
function index($current_page){
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/contents/index';
$config['total_rows'] = 200;
$config['per_page'] = 2;
$this->pagination->initialize($config);
$this->pagination->cur_page = $current_page;
echo $this->pagination->create_links();
$data['contents'] = $this->content_model->get_content();
$data['title'] = 'Contents';
$this->load->view('template/header', $data);
$this->load->view('pages/index', $data);
$this->load->view('template/footer');
}
Related
I am using codeigniter pagination. But I'm facing a problem while using codeingiter pagination. Here is the issue which I am facing.
Ex: If there are more than 10 records for a page and here I am displaying 5 records per page. If I click on the second page it is displaying the data correctly but if I need to go back for the first page it is not working. Here is my code:
Controller:
class Testimonial extends CI_Controller {
function __construct() {
parent::__construct();
//here we will autoload the pagination library
$this->load->model('testimonial_model');
$this->load->library('pagination');
}
public function index()
{
$config = array();
$config["base_url"] = base_url('testimonial/index');
$config['total_rows'] = $this->testimonial_model->record_count();//here we will count all the data from the table
$config['per_page'] = 2;//number of data to be shown on single page
$config["uri_segment"] = 2;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["records2"] = $this->testimonial_model->get_all_testimonials($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();//create the link for pagination
$data['mainpage'] = "testimonial";
$this->load->view('templates/template',$data);
}
}
Model:
class Testimonial_model extends CI_Model
{
function get_all_testimonials($limit, $start)
{
$this->db->limit($limit, $start);
$this->db->select('T.*');
$this->db->from('testimonials AS T');
$this->db->where(array('T.status'=>1));
$q = $this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
function record_count()
{
return $this->db->count_all("testimonials");
}
}
use this answer please read the code once to understand it
public function index()
{
$config = array();
//$config["base_url"] = base_url('testimonial/index');
$get_vars = $this->input->get();
if(is_array($get_vars))
$config['suffix'] = '?'.http_build_query($get_vars,'', "&");
//$config['prefix'] = '?'.http_build_query($get_vars,'', "&");
$config['base_url'] = base_url().$this->router->class.'/index';
$config['first_url'] = $config['base_url'] . (isset($config['suffix'])?$config['suffix']:'');
$config['total_rows'] = $this->testimonial_model->record_count();//here we will count all the data from the table
$config['per_page'] = 2;//number of data to be shown on single page
//this line should be according to url to fetch actual value
$config["uri_segment"] = ($this->uri->segment('2')?$this->uri->segment('2'):0); // 2 or 3 based on ur url
$this->pagination->initialize($config);
$page = $config["uri_segment"];
$data["records2"] = $this->testimonial_model->get_all_testimonials($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();//create the link for pagination
$data['mainpage'] = "testimonial";
$this->load->view('templates/template',$data);
}
I am listing the registered employee from the db and showing 10 employee per page. I'm using codeigniter pagination for the purpose.
If I deleted an employee in page 2, after deleting I need to go to page 2 showing rest of the employees in page 2.
View:
<a href="<?php echo $path;?>welcome/delete_employee/employeeid/<?php echo $row->empID;?>/pageNum/<?php echo $currentPage; ?>" onClick="return delAlert()";><img src="<?php echo $path;?>/img/delete.png" height="30px" width="30px" /></a>
Controller:
public function delete_employee()
{
$session_id = $this->session->userdata('logged_in');
if($session_id) {
$array = $this->uri->uri_to_assoc(3);
$count=$array['pageNum']-1;
$i=$count*10;
$this->load->model('welcomemodel','m',true);
$this->m->deleteemployee($array['employeeid']);
$config = array();
$config["base_url"] = base_url() . "welcome/employee";
$config["total_rows"] =$this->m->employee_count();
$config["per_page"] = 10;
$config["uri_segment"] = $array['pageNum'];
$data['showData'] = $this->m->getEmployee($config["per_page"], $i);
$this->pagination->initialize($config);
$data["links"] = $this->pagination->create_links();
$data["currentPage"] =$array['pageNum'];
$this->load->view('header');
$this->load->view('employee',$data);
} else {
$this->load->view('session_expired');
}
}
Model:
public function deleteemployee($employeeid)
{
$this->db->where('empID',$employeeid);
$this->db->delete('employee');
return $this->db->affected_rows();
}
public function getEmployee($limit, $start)
{
$this->db->limit($limit, $start);
$this->db->select()
->from('employee')
->order_by('emp_fname');
$this->db->join('service', 'employee.serviceID = service.serviceID','left');
$query=$this->db->get();
return $query;
}
public function employee_count()
{
return $this->db->count_all("employee");
}
But now the pagination link shows first page's link,even though contents of second page is displayed....
model method
public function employee_count()
{
$result = $this->db->get('employee');
if ($result->num_rows() > 0) {
//employee record available
return true;
}
else
{
// employee record not available
return false;
}
}
View
<?php
$getData = $this->[your model name]->getEmployee($limit, $start);
?>
<?php if ($getData): ?>
//Your Pagination Part Here.....
<?php else: ?>
// else Nothing
<?php endif ?>
My Pagination not work , In this case simple print users article to match the rc code of users its work perfectly url is ../project/article/user_article/?rc=4715422 , its work but pagintion url not print any article, pagination url is ../project/article/user_article/?rc=4715422/1,
here is my code: controller code is -
public function user_article()
{
$rc=$_GET['rc'];
$data['title'] = "User Article";
$config = array();
$config["base_url"] = base_url() . 'article/user_article/?rc='. $rc ;
$config["total_rows"] = $this->article_m->record_count_uarticles($_GET);
$config["per_page"] = 1;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->article_m->fetch_result_uarticles($_GET, $config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
if ($this->session->userdata ('is_logged_in')){
$data['profile']=$this->model_users->profilefetch();
$this->load->view('header',$data);
$this->load->view('user_article', $data);
$this->load->view('sfooter', $data);
} else {
redirect('signin');
}
}
and my modal is :
public function record_count_uarticles($rc) {
$query=$this->db->select('email,writer')->where('rc',$rc['rc'])->get('users');
$result=$query->result_array();
if ($query->num_rows() > 0) {
$row = $query->row_array();
$array = array ('email' => $row['email'], 'status' => '1');
return $this->db->where($array)->count_all_results("articles");
}
}
public function fetch_result_uarticles($rc, $limit, $start) {
$query=$this->db->select('email,writer')->where('rc',$rc['rc'])->get('users');
$result=$query->result_array();
if ($query->num_rows() > 0) {
$row = $query->row_array();
$this->db->limit($limit, $start);
$array = array ('u.email' => $row['email'], 'a.status' => '1', 'u.writer' => '1');
$query1=$this->db->select('a.id,title,a.status,description,image,a.email,tags,postdate,firstname,lastname,rc')->join('users u','u.email = a.email','left')->where($array)->order_by('a.id', 'DESC')->get('articles a');
if ($query1->num_rows() > 0) {
foreach ($query1->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
Check this code, pagination show properly, first page article print correctly but after click on pagination link then no article print in scnd page
This Link work but default link is not work , is there any query to change the url
../project/article/user_article/?rc=4715422/1 -- Default Link Not Work
../project/article/user_article/1/?rc=4715422 -- Custom Edited Link work
tried doing this on my own but had no luck =/
My pagination is on the main page, I have some sort of blog system there,
the only other view on that controller is login, which after enabling the pagination just disappeared and now instead of URL/login it displays the main page.
My controller:
public function view($page = 'index')
{
if ( !file_exists('application/views/pages/'.$page.'.php') || $page == 'header' || $page == 'footer')
{
// Whoops, we don't have a page for that!
show_404();
}
if( $page = 'index' ) {
$this->load->library('pagination');
$config['base_url'] = base_url('posts');
$config['total_rows'] = $this->db->order_by("id", "desc")->get('Updates')->num_rows();
$config['per_page'] = 5;
$config['uri_segment'] = 3;
$config['full_tag_open'] = '<div id="pages">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data['records'] = $this->db->order_by("id", "desc")->get('Updates', $config['per_page'], $this->uri->segment(2));
}
$this->load->model('panel_model');
$data['math_captcha_question'] = $this->mathcaptcha->get_question();
$this->load->view('header');
$this->load->view('pages/'.$page, $data);
$this->load->view('footer');
}
routes.php:
$route['login'] = "pages/view/login";
I'd be really glad if any of you guys could give me a hand :)
Thanks!
base_url = assets, files and directories.
site_url = URL of your site, because it does append $config['index_page'].
Try replace base_url('posts') with site_url('posts');
I'm trying to learn CodeIgniter and I'm having trouble with the pagination. I'm a complete newbie so I apologise if I've missed anything.
The pagination displays on my views fine, generates everything it should, but the pages give me a 404. I suspect it might have something to do with the routes or the uri_segment (though I've changed it to a whole bunch of numbers and nothing did the trick), but I'm not sure.
The URLs each page generates are /music3/music/20, /music3/music/40, etc.
Let me know if you need anything I haven't included.
Controller:
$config['base_url'] = 'http://moefoster.com/music3/music/';
$config['total_rows'] = $this->db->count_all('Music');
$config['per_page'] = '20';
$config['num_links'] = '5';
$config['uri_segment'] = '2';
$this->pagination->initialize($config);
$data['music'] = $this->music_model->get_music($config['per_page'], $config['uri_segment']);
Model:
public function get_music($num = 20, $offset = 0, $slug = FALSE) {
if ($slug === FALSE) {
return $this->db->select('*')->from('Music')->limit($num, $offset)->order_by('Release', 'desc')->get()->result_array();
}
$query = $this->db->get_where('Music', $num, $offset, array('Track' => $slug));
return $query->row_array();
}
Routes:
$route['music'] = 'music';
$route['default_controller'] = "music";
$route['(:any)'] = 'pages/view/$1';
you use standard pagination :/ nevermind, pagination generated like this:
class / function / pagination num
URL BASE:
$config['base_url'] = 'http://moefoster.com/music/';
ROUTES:
$route['music'] = 'music';
$route['music/(:num)'] = 'music/$1';
$route['default_controller'] = "music";
$route['(:any)'] = 'pages/view/$1';
but I use in backend, \libraries\My_Pagination.php
<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Pagination extends CI_Pagination
{
var $offset = 0;
var $pagination_selector = 'page';
var $index_page;
function MY_Pagination ()
{
parent::__construct();
log_message('debug', "MY_Pagination Class Initialized");
$this->index_page = config_item('index_page') != '' ? config_item('index_page') . '/' : '';
$this->_set_pagination_offset();
}
function _set_pagination_offset ()
{
$CI = & get_instance();
if (strstr($CI->uri->uri_string(), $this->pagination_selector)) {
$segments = $CI->uri->segment_array();
foreach ($segments as $key => $value) {
if ($value == $this->pagination_selector) {
$this->offset = $CI->uri->segment($key + 1);
$this->uri_segment = $key + 1;
$uri = $CI->uri->uri_string();
$pos = strpos($uri, $this->pagination_selector);
$this->base_url = '/'.$this->index_page . substr($uri, 0, $pos + strlen($this->pagination_selector.'/'));
}
}
}
else {
$this->offset = 0;
$this->uri_segment = 0;
$this->base_url = '/'.$this->index_page . $CI->uri->uri_string() . '/' . $this->pagination_selector.'/';
}
}
}
Example controller
function somefunction($somevariable){
$config['total_rows'] = Model_Admin::getCountClankyCategory($somevariable);
$config['per_page'] = 25;
$config['cur_tag_open'] = '#START SOME HTML#';
$config['cur_tag_close'] = '#END SOME HTML#';
$config['full_tag_open'] = '#START SOME HTML PAGINATION#';
$config['full_tag_close'] = '#END SOME HTML PAGINATION#';
$this->pagination->initialize($config);
$data['content'] = Model_Admin::getAllClankyCategory($somevariable,$config['per_page'],$this->pagination->offset);
.....
}
some example model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Model_Admin extends CI_Model {
private static $db;
function __construct()
{
parent::__construct();
self::$db = &get_instance()->db;
}
static function getAllClankyCategory($cat,$num,$offset) {
self::$db->where('#MYTYPE#',$cat);
return self::$db->get('#SOMETABLE#',$num,$offset)->result_array();
}
static function getCountClankyCategory($cat) {
return self::$db->count_all_results('#SOMETABLE#');
}
then if u use "some hard code routing", routes like, other u use without these routes
$route['admin/articles/cat/(:any)'] = "admin/cat/$1";
$route['admin/articles/cat/(:any)/(:num)'] = "admin/cat/$1/$2";
You can check one my previous answers on Pagination which explains very well how to do pagination and how to take care urls perfectly.....