I am working on Codeigniter Pagination and following this link :
http://www.technicalkeeda.com/codeigniter-tutorials/pagination-using-php-codeigniter
but when i click on further links i.e. 1,2 and so on then the next sequence of records does not loads up only the initial (from 1 to 5 shows up) i have seen other links on stackoverflow : CodeIgniter Pagination Page Links Not Working but it did not work. My code is as follows, please help to solve my problem:
Controller: home.php
<?php
class Home extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('MovieModel');
$this->load->library('pagination');
}
public function index($offset=0){
$config['total_rows'] = $this->MovieModel->totalMovies();
$config['base_url'] = base_url().'/home/index';
$config['per_page'] = 5;
$config['uri_segment'] = '2';
$config['full_tag_open'] = '<div class="pagination"><ul>';
$config['full_tag_close'] = '</ul></div>';
$config['first_link'] = '« First';
$config['first_tag_open'] = '<li class="prev page">';
$config['first_tag_close'] = '</li>';
$config['last_link'] = 'Last »';
$config['last_tag_open'] = '<li class="next page">';
$config['last_tag_close'] = '</li>';
$config['next_link'] = 'Next →';
$config['next_tag_open'] = '<li class="next page">';
$config['next_tag_close'] = '</li>';
$config['prev_link'] = '← Previous';
$config['prev_tag_open'] = '<li class="prev page">';
$config['prev_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li class="page">';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$query = $this->MovieModel->getMovies(5,$this->uri->segment(2));
$data['MOVIES'] = null;
if($query){
$data['MOVIES'] = $query;
}
$this->load->view('index1.php', $data);
}
}
?>
MovieModel.php
<?php
class MovieModel extends CI_Model {
function getMovies($limit=null,$offset=NULL){
$this->db->select("MOVIE_ID,FILM_NAME,DIRECTOR,RELEASE_YEAR");
$this->db->from('trn_movies');
$this->db->limit($limit, $offset);
$query = $this->db->get();
return $query->result();
}
function totalMovies(){
return $this->db->count_all_results('trn_movies');
}
}
?>
View: index1.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Codeigniter Pagination Example</title>
<link href="<?= base_url();?>css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="row">
<h4>Movies List</h4>
<table class="table table-striped table-bordered table-condensed">
<tr><td><strong>Movie Id</strong></td><td><strong>Film Name</strong></td><td><strong>Director</strong></td><td><strong>Release Year</strong></td></tr>
<?php
if(is_array($MOVIES) && count($MOVIES) ) {
foreach($MOVIES as $movie){
?>
<tr><td><?=$movie->MOVIE_ID;?></td><td><?=$movie->FILM_NAME;?></td><td><?=$movie->DIRECTOR;?></td><td><?=$movie->RELEASE_YEAR;?></td></tr>
<?php
}
}?>
</table>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="row"><?php echo $this->pagination->create_links(); ?></div>
</div>
</div>
</div>
</body>
</html>
Use the below coding
if($this->uri->segment(3))
$offset = $this->uri->segment(3);
else
$offset = 0;
$query = $this->MovieModel->getMovies(5,$offset);
Related
I am trying to create a simple pagination in my CodeIgniter blog, here is my code, I am using auto loader for these
$autoload['libraries'] = array('database','session','pagination');
$autoload['helper'] = array('form','url');
My Controller;
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Blog extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('BlogModel');
}
public function index($offset=0)
{
$this->load->library('table');
$config["base_url"] = base_url('blog');
//$config["total_rows"] = $this->BlogModel->get_Count(); // There are currently 36 Posts
$config["total_rows"] = 36; // There are currently 36 Posts
$config["per_page"] = 5; // Change limit to suit what you would like
$config['use_page_numbers'] = TRUE;
$limit = $config['per_page'];
$this->pagination->initialize($config);
$blog_pagination = $this->pagination->create_links();
$Posts = $this->BlogModel->get_Posts($limit, $offset);
$assignData=array('data'=>$Posts,'blog_pagination'=>$blog_pagination);
$this->load->view('header');
$this->load->view('blog', $assignData);
$this->load->view('footer');
}
}
I Have this in my Model;
<?php
class BlogModel extends CI_Model {
public function __construct(){
// Call the Model constructor
parent::__construct();
$this->load->library('memcached_library');
}
public function get_Posts($limit, $offset){
$this->db->select('*');
$this->db->where('post_status','published');
$query= $this->db->get('cms_posts',$limit, $offset);
$data=$query->result_array();
return $data;
}
public function get_Count() {
//$query = $this->db->get($this->db->dbprefix . 'blog');
$this->db->select('*')->from('cms_posts');
$this->db->where('post_status','published');
$query= $this->db->get();
//$data=$query->result_array();
return $query->num_rows();
}
}
?>
I am using this in my View;
<div id="primary" class="left-column">
<?php
if(!empty($data)){
foreach($data as $row){
?>
<main id="main" role="main">
<article id="<?php echo $row['post_id']; ?>" class="post-item">
<div class="right">
<div class="post-thumbnail">
<img src="<?php echo base_url()."assets/images/uploads/test_n-150x150.jpg";?>" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="" sizes="(max-width: 320px) 100vw, 320px" width="320" height="143">
</div>
<!-- END .post-thumbnail -->
</div>
<div class="left">
<h2 class="post-title">
<?php echo $row['post_title']; ?>
</h2>
<div class="post-meta">
<span class="vcard author">
<?php echo ucwords($row['post_author']); ?>
</span>
<time class="entry-date published" datetime="<?php echo $row['post_date']; ?>"><?php echo date("d M Y", strtotime($row['post_date'])); ?></time>
</div>
<div class="post-excerpt">
<p>
<?php
echo(substr($row['post_content'], 0, 250));
<a class="read-more" href="<?php echo base_url()."blog/".$row['post_url']; ?>">Read More »</a>
</p>
</div>
<!-- END .post-excerpt -->
</div>
<!-- END .left -->
</article>
<!-- END .post-item -->
</main>
<?php
}
}
?>
<!-- END .site-main -->
<div class="pagination-wrap">
<?php echo $blog_pagination; ?>
</div>
</div>
EDIT:
My Route is as under:
$route['default_controller'] = 'index';
$route['blog/:any'] = 'blog/post/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I don't know that what and where are I am missing something, please lead me or correct me in this code. thnk you
Update
My Blog show now 5 Post on First Page and create Pagination but on only 123>Last› pagination is created and there is page blank on offset 2 (/blog/2).
maybee you can use my methode use uri segment to $offset, like this
public function house()
{
$config['base_url'] = site_url().'/user/house/';
$config['total_rows'] = $this->houses->select_row_house_design();
$config['per_page'] = 12;
$config['cur_tag_open'] = '<li><a><b>';
$config['cur_tag_close'] = '</li></a></b>';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$this->pagination->initialize($config);
$from = $this->uri->segment('3');
$data['design'] = $this->houses->select_all_house_design($config['per_page'],$from);
$title['menu'] = 'house design';
$this->template('user/house',$data,$title);
}
set controller like this,
this is also simple
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Blog extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('BlogModel');
$this->load->library('table');
}
public function index($offset=0)
{
$config['base_url'] = site_url('blog/index');
$config["total_rows"] = $this->BlogModel->get_Count(); // There are currently 36 Posts
$config["per_page"] = 5; // Change limit to suit what you would like
$config['num_links'] = 10;
/*use this code for bootstrap
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a class="">';
$config['cur_tag_close'] = '</a></li>';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '<li>';
$config['next_tag_open'] = '<li class="next">';
$config['next_tag_close'] = '</li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['first_link'] = '<<';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['last_link'] = '>>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
*/
$this->pagination->initialize($config);
$data['blog_pagination'] = $this->pagination->create_links();
$data['posts'] = $this->BlogModel->get_Posts($config['per_page'],$this->uri->segment(3));
$this->load->view('header');
$this->load->view('blog', $data);
$this->load->view('footer');
}
}
Listing doctors information using pagination. But listing all doctors information from Database in one page.
Controller
public function doctor_userlist_view($offset = 0)
{
$config["base_url"] = base_url() . "User/doctor_userlist_view/";
$config['total_rows'] = $this->db->count_all_results('tbl_doctor');
$config['per_page'] = 4;
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = 'Prev';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = 'Next';
$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['docList'] = $this->User_model->get_all_userdoctor($config['per_page'], $offset);
$data['get_specs'] = $this->specialisation_model->get_specialisation();
$data['docsideList'] = $this->User_model->get_all_userdocside();
$data['get_specs'] = $this->specialisation_model->specialisation_get();
$this->load->view('customer/header', $data);
$this->load->view('customer/side_view', $data);
$this->load->view('customer/doctor', $data);
$this->load->view('customer/footer');
}
My view
<div class="list">
<ul>
<?php
if(isset($docList)):
foreach ($docList->result() as $row):
?>
<li><div class="imgt"><img src="<?php echo base_url("./resources/images/"); if($row->dr_img) echo $row->dr_img; else echo "no-img.jpg"; ?>" height="90px" width="82px"></div><div class="text"><h3><b>Dr. <?php echo $row->dr_name;?></b><br></h3><p><?php echo $row->spec_specialise; ?><br><?php echo $row->district;?><br><?php echo $row->state;?></p></div><div class="text"></div><div class="link"><i class="ace-icon fa fa-eye sym"></i>View</div></li>
<?php endforeach;
endif;
?>
</ul>
<div class="space">
</div>
</div></div>
<div class="pdt_rightt">
<center>
<?php
echo $this->pagination->create_links();
?>
</center>
</div>
Pagination is not working properly in all links showing the same data. I use same code for listing hospitals in that pagination is working.
You are defining $data['get_specs'] twice, like so...
// This instance of $data['get_specs'] is never being used as it's over written by the one below.
$data['get_specs'] = $this->specialisation_model->get_specialisation();
$data['docsideList'] = $this->User_model->get_all_userdocside();
$data['get_specs'] = $this->specialisation_model->specialisation_get();
So which one is it?
public function get_all_userdoctor($limit, $offset)
{
$this->db->order_by ("id", "desc");
$this->db->select('tbl_doctor.*, tbl_specialisation.*');
$this->db->join('tbl_specialisation', 'tbl_specialisation.spec_id = tbl_doctor.spec_id');
$query = $this->db->get('tbl_doctor', $limit, $offset);
return $query;
}
use this model.Pagination will work properly
I have 3 sections in my view, there is one section of a review I want that part of a view to change when user selects another page link that must refresh only the paging div. My code for simple Pagination in Codeigniter is as follows and works perfectly.
Controller: home.php
<?php
class Home extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('MovieModel');
$this->load->library('pagination');
}
public function index($offset=0){
$config['total_rows'] = $this->MovieModel->totalMovies();
$config['base_url'] = base_url().'/home/index';
$config['per_page'] = 5;
$config['uri_segment'] = '3';
$config['full_tag_open'] = '<div class="pagination"><ul>';
$config['full_tag_close'] = '</ul></div>';
$config['first_link'] = '« First';
$config['first_tag_open'] = '<li class="prev page">';
$config['first_tag_close'] = '</li>';
$config['last_link'] = 'Last »';
$config['last_tag_open'] = '<li class="next page">';
$config['last_tag_close'] = '</li>';
$config['next_link'] = 'Next →';
$config['next_tag_open'] = '<li class="next page">';
$config['next_tag_close'] = '</li>';
$config['prev_link'] = '← Previous';
$config['prev_tag_open'] = '<li class="prev page">';
$config['prev_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li class="page">';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
if($this->uri->segment(3))
{
$offset = $this->uri->segment(3);
}
else
{
$offset = 0;
}
$query = $this->MovieModel->getMovies(5,$offset);
//$query = $this->MovieModel->getMovies(5,$this->uri->segment(3));
$data['MOVIES'] = null;
if($query){
$data['MOVIES'] = $query;
}
$this->load->view('index1.php', $data);
}
}
?>
MovieModel.php
<?php
class MovieModel extends CI_Model {
function getMovies($limit=null,$offset=NULL){
$this->db->select("MOVIE_ID,FILM_NAME,DIRECTOR,RELEASE_YEAR");
$this->db->from('trn_movies');
$this->db->limit($limit, $offset);
$query = $this->db->get();
return $query->result();
}
function totalMovies(){
return $this->db->count_all_results('trn_movies');
}
}
?>
index1.php
<!DOCTYPE html>
<html lang="en">
<head>
<title>Codeigniter Pagination Example</title>
<link href="<?=base_url();?>css/bootstrap.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="row">
<h4>Movies List</h4>
<table class="table table-striped table-bordered table-condensed">
<tr><td><strong>Movie Id</strong></td><td><strong>Film Name</strong></td><td><strong>Director</strong></td><td><strong>Release Year</strong></td></tr>
<?php
if(is_array($MOVIES) && count($MOVIES) ) {
foreach($MOVIES as $movie){
?>
<tr><td><?=$movie->MOVIE_ID;?></td><td><?=$movie->FILM_NAME;?></td><td><?=$movie->DIRECTOR;?></td><td><?=$movie->RELEASE_YEAR;?></td></tr>
<?php
}
}?>
</table>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="row"><?php echo $this->pagination->create_links(); ?></div>
</div>
</div>
</div>
</body>
</html>
This cannot be done in PHP alone.
You will have to use AJAX to update the section on your page.
The suggested approach would be:
use jQuery to detect the button click.
Send a request to your Codeigniter backend.
jQuery detects on success, using the data printed by the backend to update the container in your frontend.
See: http://blog.teamtreehouse.com/beginners-guide-to-ajax-development-with-php
I build a catalog website. The catalog location is in http://localhost/bookstore/site/catalog. Then if i click one of book the url change to http://localhost/bookstore/site/detail/100016 and it show the detail of book.
And now i click the pagination, the url change to http://localhost/bookstore/site/catalog/12, its work fine. But if i click one of book, its doesnt show detail of book because the url change to http://localhost/bookstore/site/catalog/detail/100005.
Can you see what different? yes, the url still save method catalog so detail of book cant be shown. What should i do?
This my controller
public function catalog()
{
$config['full_tag_open'] = '<div class="pagination pagination-colory"><ul>';
$config['full_tag_close'] = '</ul></div>';
$config['first_link'] = '« First';
$config['first_tag_open'] = '<li class="prev page">';
$config['first_tag_close'] = '</li>';
$config['last_link'] = 'Last »';
$config['last_tag_open'] = '<li class="next page">';
$config['last_tag_close'] = '</li>';
$config['next_link'] = 'Next →';
$config['next_tag_open'] = '<li class="next page">';
$config['next_tag_close'] = '</li>';
$config['prev_link'] = '← Previous';
$config['prev_tag_open'] = '<li class="prev page">';
$config['prev_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li class="page">';
$config['num_tag_close'] = '</li>';
$config['base_url'] = base_url('site/catalog');
$config['total_rows'] = $this->db->get('buku')->num_rows();
$config['per_page'] = 12;
//$config['uri_segment'] = 3;
$dari = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['databuku'] = $this->m_site->show_buku($config['per_page'],$dari);
$this->pagination->initialize($config);
$this->load->view('v_catalog_buku', $data);
}
Model
function show_buku($limit, $offset)
{
$this->db->order_by("id_buku", "desc");
$query = $this->db->get("buku", $limit, $offset);
return $query->result();
}
View
<table>
<div class="row">
<?php foreach($databuku as $row ):?>
<a href="<?php base_url(); ?>detail/<?php echo $row->id_buku; ?>"><div class="span3" style="height:450px;" >
<div class="icons-box">
<div class="row" style="height:100px;">
<div class="body"><h3><?php echo $row->judul; ?></h3></div>
</div>
<div class="row" style="height:200px;">
<img src="<?php echo base_url('uploads/'.$row->img);?>" height="120px" width="120px"/>
</div>
<div class="row">
<div><h3>Rp.<?php echo number_format($row->harga,2,",",".");?></h3></div>
</div>
</div>
</div></a>
<?php endforeach; ?>
</div>
</table>
<div class="box-content" align="center">
<?php echo $this->pagination->create_links(); ?>
</div>
<!-- end: Row -->
</div>
It so nightmare when using pagination on codeigniter. It work to move another page when click next or number after. But, the 'cur_tag_open' is not move. It still in first link of pagination. So let say, I clicked 3 on [1] [2] [3] , the class active still in one not in three. I can not find the solution of this problem, and this is my code :
CONTROLLER
class Control_direksi extends CI_Controller {
public function index() {
$this->show();
}
public function show() {
/** ======================================================= Inisialisasi table 1 =========================================================* */
$start_row = $this->uri->segment(4);
$per_page = 2;
if (trim($start_row) == '') {
$start_row = 0;
};
$total_rows = $this->model_request->hitungRequestMasukMD();
$this->load->library('pagination');
if ($total_rows != 0) {
$config['base_url'] = base_url() . 'direksi/control_direksi/show/';
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$config['full_tag_open'] = '<div class="pagination pagination-centered" id="pagination1"><ul>';
$config['full_tag_close'] = '</ul></div><!--pagination-->';
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = 'Prev';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = 'Next';
$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['pagination'] = $this->pagination->create_links();
}
$data['level'] = $this->session->userdata('level');
$data['pengguna'] = $this->model_user->get_username($this->session->userdata('username'));
$data['data_request_done'] = $this->model_request->get_data_request_belum_terkoreksi($per_page, $start_row);
$data['hitung_Request_Masuk_MD'] = $total_rows;
$this->load->view('direksi/view_direksi', $data);
}
VIEW
<body>
<!-- start: Header -->
<?php $this->load->view('/direksi/include/header.php') ?>
<!-- start: Header -->
<div class="container-fluid-full">
<div class="row-fluid">
<noscript>
<div class="alert alert-block span12">
<h4 class="alert-heading">Warning!</h4>
<p>You need to have JavaScript enabled to use this site.</p>
</div>
</noscript>
<!-- start: Content -->
<marquee>Jumlah Request Masuk ke MD: <?php echo $hitung_Request_Masuk_MD . ' request' ?></marquee>
<div class="box-header">
<h2><i class="halflings-icon align-justify"></i><span class="break"></span>Request masuk ke IT</h2>
<div class="box-icon">
<i class="halflings-icon chevron-up"></i>
</div>
</div>
<div class="box-content" id="things_table">
<?php $this->load->view('direksi/view_direksi_table'); ?>
</div>
</div>
</div>
<!-- end: Content -->
<?php $this->load->view('direksi/view_direksi_table_modal', TRUE); ?>
<div class="clearfix"></div>
<?php $this->load->view('/include/js.html', TRUE); ?>
</body>
Any help, it so appreciated
change 01
$start_row = $this->uri->segment(4);//remove this
and post after
$config['base_url'] = base_url() . 'direksi/control_direksi/show/';
$config['total_rows'] = $total_rows;
$config['per_page'] = 2;
$config['uri_segment'] = 3;//Uri_Segment
Change 02
$data['pagination'] = $this->pagination->create_links();
modify like this
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;//Add this line
$data['links'] = $this->pagination->create_links();
Change 03
in view.
where you want show your pagination list just use
<?php
echo $links;
?>
Important load this library in controller as well
$this->load->library('pagination');