Hi I have implemented Pagination in PHP Code But it is not Working while clicking on the pagination links.It is displaying the same data for all the pages.Here is the code.
Controller:
class Testimonial extends CI_Controller {
function __construct() {
parent::__construct();
//here we will autoload the pagination library
$this->load->library('pagination');
}
public function index()
{
$this->load->model('testimonial_model');
$config = array();
$config["base_url"] = base_url('testimonial/index');
$config['total_rows'] = $this->db->count_all("testimonials");//here we will count all the data from the table
$config['per_page'] = 6;//number of data to be shown on single page
$config["uri_segment"] = 2;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 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;
}
}
}
View:
<div class="pagination"><?php echo $links; ?></div>
Try following will may help you,
public function index()
{
$this->load->model('testimonial_model');
$config = array();
$config["base_url"] = base_url('testimonial/index');
$config['total_rows'] = $this->db->count_all("testimonials");//here we will count all the data from the table
$config['per_page'] = 6;//number of data to be shown on single page
$config["uri_segment"] = 2;
$this->pagination->initialize($config);
$page = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
$data["records2"] = $this->testimonial_model->get_all_testimonials($config["per_page"], (($page-1)*$config["per_page"]));
$data["links"] = $this->pagination->create_links();//create the link for pagination
$data['mainpage'] = "testimonial";
$this->load->view('templates/template',$data);
}
I cant comment so I just make this an answer,
Here
http://bootsnipp.com/snippets/featured/rounded-pagination
This is what I use in making my pagination! and there is alot more of it! I also use CI as my framework!
Related
I have a paginated list of users where there are total 2 records with a pagination of 2. This means I should get only 1 link in pagination. But I am getting 2 links where 2nd link (i.e 2) should not be rendered.
Below is my Controller and Model code:
class Users extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('users_model');
$this->load->model('roles_model');
$this->load->model('branches_model');
}
public function index()
{
$this->data['page_title'] = 'Users';
$config["base_url"] = base_url() . "admin/users";
$config["total_rows"] = $this->users_model->record_count();
$config["per_page"] = 2;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment($config["uri_segment"])) ? $this->uri->segment($config["uri_segment"]) : 0;
$this->data['users'] = $this->users_model->get_users(NULL,$config["per_page"], $page);
$this->data["links"] = $this->pagination->create_links();
$this->render('admin/list_users_view');
}
}
class Users_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database();
}
public function record_count() {
return $this->db->count_all("users");
}
public function get_users($UserId = FALSE, $limit = FALSE, $start = FALSE) {
$this->db->select('users.*,roles.RoleName,branches.BranchName');
$this->db->from('users');
$this->db->join('roles', 'users.RoleId = roles.RoleId', 'inner');
$this->db->join('branches', 'users.BranchId = branches.BranchId', 'inner');
$this->db->where('users.BranchId',2);
$filtered_count = $this->db->count_all_results('', false);
$this->db->limit($limit, $start);
$query = $this->db->get()->result();
$data_array = array('total_records' => $filtered_count, 'records' => $query );
return $data_array;
}
}
pagination
Try to reordering the variables on the controllers and use the returned total_records from the model :
public function index()
{
$this->data['page_title'] = 'Users';
$config["base_url"] = base_url() . "admin/users";
$config["per_page"] = 2;
$config["uri_segment"] = 3;
$page = ($this->uri->segment($config["uri_segment"])) ? $this->uri->segment($config["uri_segment"]) : 0;
$this->data['users'] = $this->users_model->get_users(NULL,$config["per_page"], $page);
$config["total_rows"] = $this->data['users']['total_records'];
$this->pagination->initialize($config);
$this->data["links"] = $this->pagination->create_links();
$this->render('admin/list_users_view');
}
My pagination of search results is not working. It's showing result but pagination making it random. Please help me. Search is working but can't apply pagination in it, it's getting complicated
search_products_model
<?php
class search_products_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function get_results($search_term,$limit, $offset)
{
// Use the Active Record class for safer queries.
$this->db->like('name',$search_term);
// Execute the query.
$query = $this->db->get('tbl_products',$limit, $offset);
if ( $query->num_rows() > 0 )
{
return $query->result_array();
}
else
{
return FALSE;
}
// Return the results.
}
function get_products_id($id)
{
$this->db->where('prod_id',$id);
$query = $this->db->get('tbl_products');
return $query->result();
}
}
?>
controller
<?php
class search_products extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation', 'email','pagination'));
$this->load->database();
$this->load->model('search_products_model');
}
function index()
{
$this->search_products();
$this->load->view('search_products_view');
}
function display_registration_form()
{
$this->load->view('search_products_view');
}
function execute_search($offset = 0)
{
$config['base_url'] = '/mehmaa/index.php/search_products/execute_search/';
$config['total_rows'] = $this->db->count_all_results('tbl_products');
$config['per_page'] = 6;
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
// Retrieve the posted search term.
$id = $this->input->post('id');
$search_term = $this->input->post('term');
// Use a model to retrieve the results.
$data['results'] = $this->search_products_model->get_results($search_term,$config['per_page'], $offset);
if ( empty($data['results']) )
{
$this->load->view('search_products_view');
$this->session->set_flashdata('message', 'No results found');
}
else{
// Pass the results to`enter code here` the view.
$this->load->view('search_results',$data);
}
}
hi in this link you are not checking the search condition
$config['total_rows'] = $this->db->count_all_results('tbl_products');
change this line to
$config['total_rows'] = $this->db->count_all_results($search_term);
and write the proper query
$config['total_rows'] = $this->db->count_all_results('tbl_products');
Problem in the this files, u need write another query which will be collect result by search without offsets and limits.
For example
function search($text, $offset, $limits){
//this u do search
}
function count_search($text){
//This same logic with search but return Integer value for example
//return $this->db->query()->num_rows();
}
I have been trying to add pagination into my code igniter project and despite the fact that the code igniter reference material says it is "easy" I cannot get it to work. I have spent time googling and see similar stuff and I do everything that I find and still nothing works. One of my friends, who is also using codeigniter, tried to help me as well and could not get it to work. I think that I am very close and probably just forgetting everything. Anyways, I am working on a blog and everything works well with the blog except the pagination! I currently have the pagination code in my controller under my index method as such:
public function index()
{
$data['blog'] = $this->Blog_model->get_blog();
$data['title'] = 'Blog archive';
//pagination code
$this->load->library('Pagination');
$config['base_url'] = site_url('blog');
$config['total_rows'] = 1;
$config['per_page'] = 1;
$this->pagination->initialize($config);
$data['Pagination'] = $this->pagination->create_links();
//echo $this->pagination->create_links();
//END Pagination
$this->load->view('templates/header', $data);
$this->load->view('blog/index', $data);
$this->load->view('templates/footer');
}
The whole entire controller looks like this:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blog extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Blog_model');
$this->load->helper('url_helper');
}
public function index()
{
$data['blog'] = $this->Blog_model->get_blog();
$data['title'] = 'Blog archive';
//pagination code
$this->load->library('Pagination');
$config['base_url'] = site_url('blog');
$config['total_rows'] = 1;
$config['per_page'] = 1;
$this->pagination->initialize($config);
$data['Pagination'] = $this->pagination->create_links();
//echo $this->pagination->create_links();
//END Pagination
$this->load->view('templates/header', $data);
$this->load->view('blog/index', $data);
$this->load->view('templates/footer');
}
public function view($slug = NULL)
{
$data['blog_item'] = $this->Blog_model->get_blog($slug);
if (empty($data['blog_item']))
{
show_404();
}
$data['title'] = $data['blog_item']['title'];
$this->load->view('templates/header', $data);
$this->load->view('blog/view', $data);
$this->load->view('templates/footer');
}
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('blog/create');
$this->load->view('templates/footer');
}
else
{
$this->Blog_model->set_blog();
redirect('blog');
//$this->index();
//$this->load->view('templates/header');
//$this->load->view('../../blog/index', $data);
//$this->load->view('templates/footer');
}
}
}
Over on my views, I have this:
<h1>Create A blog Entry</h1>
<?php echo $Pagination; ?>
<div class="blog">
<h2><?php echo "Blog"; ?></h2>
<div class="row">
<?php foreach ($blog as $blog_item): ?>
<div class="col-md-6">
<div class='panel panel-primary'>
<div class='panel-heading'>
<?php echo $blog_item['title']; ?>
</div>
<div class="panel-body">
<?php echo $blog_item['body']; ?>
</div>
<div class="panel-footer">
<?php echo $blog_item['entry_date']; ?>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
finally, my config/autoload.php file has this:
$autoload['libraries'] = array('database','Pagination');
I do not have anything on my model. If one wants to see the full code it is as my github page under LAMPCAMP_Project and my name there is ravenusmc. Thank you for any help with this and have a great day!
I have added the model code:
<?php
class Blog_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_blog($slug = FALSE)
{
if ($slug === FALSE)
{
$query = $this->db->order_by('entry_date', 'desc')->get('blog');
return $query->result_array();
}
$query = $this->db->get_where('blog', array('slug' => $slug));
return $query->row_array();
}
public function set_blog()
{
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'body' => $this->input->post('body')
);
return $this->db->insert('blog', $data);
}
}
I would autoload url helper instead of loading it in model. And remove $this->load->database(); from model if you have already autoload it.
$autoload['helper'] = array('url');
$autoload['libraries'] = array('database','pagination');
Second I would recommend creating a count function for total rows
public function count_total() {
$query = $this->db->get($this->db->dbprefix . 'blog');
return $query->num_rows();
}
For your get function you need a get and limit and offset variable.
public function fetch_blogs($limit, $start, $slug) {
if ($slug == FALSE) {
$this->db->limit($limit, $start);
$this->db->order_by('entry_date', 'desc');
if ($query->num_rows() > 0 {
return $query->result_array();
} else {
return FALSE;
}
}
$this->db->limit($limit, $start);
$query = $this->db->get_where($this->db->dbprefix . 'blog', array('slug' => $slug));
if ($query->num_rows() > 0 {
return $query->result_array();
} else {
return FALSE;
}
}
application/config/routes.php with you blog routes I would think you would need to create I route for pagination in the blog.
$route['blog'] = 'blog/index';
$route['blog/(:any)'] = 'blog/index/$1';
Blog Controller
First off use base_url() insead of site_url()
<?php
class Blog extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('blog_model');
}
public function index() {
$config["base_url"] = base_url('blog');
$config["total_rows"] = $this->blog_model->count_total();
$config["per_page"] = 5; // Change limit to suit what you would like
$config["uri_segment"] = 2;
$this->pagination->initialize($config);
$data['Pagination'] = $this->pagination->create_links();
$start = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
// Fetch Blogs
$slug = '' // What ever you need slug to be example uri segment()
$data['blog'] = $this->blog_model->fetch_blogs($config["total_rows"], $start, $slug);
$this->load->view('templates/header', $data);
$this->load->view('blog/blog_view', $data); // change index to different view name
$this->load->view('templates/footer');
}
}
Here are the user guide links for CI2 And CI3
CI3: http://www.codeigniter.com/user_guide/libraries/pagination.html
CI2: http://www.codeigniter.com/userguide2/libraries/pagination.html
I m using codeigniter pagination class, it works fine when a controller first load, on the next load it not change the data.
Here is my controller:
class test extends CI_Controller{
public function __construct() {
parent:: __construct();
$this->load->model("getdata");
$this->load->library("pagination");
}
public function read($page=0){
$this->load->view('header');
$config['base_url'] = base_url()."index.php/test/read";
$config["total_rows"] = $this->getdata->record_count();
$config["per_page"] = 5;
$this->pagination->initialize($config);
$data["results"] = $this->getdata->basic($config["per_page"],$page);
$data["links"] = $this->pagination->create_links();
$this->load->view('readarea',$data);
$this->load->view('footer');
}
};
?>
Here is my Model:
<?php
class getdata extends CI_Model{
public function __construct() {
parent::__construct();
}
public function record_count() {
return $this->db->count_all("table1");
}
public function basic($limit,$start){
$this->db->limit($limit,$start);
$query = $this->db->query("SELECT table1.id,table1.post_id,table1.author,table1.quest,table1.first_name,table2.last_name,table2.pic,table2.userid FROM table1,table2 WHERE table1.post_id = table2.id ORDER BY table1.id DESC");
if($query->num_rows() > 0) {
return $query->result();
}
return FALSE;
}
};
?>
NOTE: Links are created, but not get the next data from database...
Try below query in model.
public function basic($limit,$start){
$query = $this->db->query("SELECT table1.id,table1.post_id,table1.author,table1.quest,table1.first_name,table2.last_name,table2.pic,table2.userid FROM table1,table2 WHERE table1.post_id = table2.id ORDER BY table1.id DESC LIMIT $start, $limit");
if($query->num_rows() > 0) {
return $query->result();
}
return FALSE;
}
change this function to this
public function read($page = null){
$this->load->view('header');
$config['base_url'] = base_url()."index.php/test/read";
$config["total_rows"] = $this->getdata->record_count();
$config["per_page"] = 5;
$this->pagination->cur_page = $page; // add this line into it i hope this work
$this->pagination->initialize($config);
$data["results"] = $this->getdata->basic($config["per_page"],$page);
$data["links"] = $this->pagination->create_links();
$this->load->view('readarea',$data);
$this->load->view('footer');
}
};
I have a little problem with pagination class of codeigniter
Here is my controller
function index()
{
$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/books/index/';
$config['total_rows'] = $this->db->count_all('books');
$config['per_page'] = '5';
$config['full_tag_open'] = '<p>';
$config['full_tag_close'] = '</p>';
$this->pagination->initialize($config);
echo "Here";
//load the model and get results
$this->load->model('books_model');
$data['results'] = $this->books_model->get_books($config['per_page'],$this->uri->segment(3));
// load the HTML Table Class
$this->load->library('table');
$this->table->set_heading('Title');
// load the view
$this->load->view('books_view', $data);
}
and here is the view
<body>
<h1>Books</h1>
<?php echo $this->table->generate($results); ?>
<?php echo $this->pagination->create_links(); ?>
</body>
and the model
function get_books($num, $offset) {
$query = $this->db->get('books', $num, $offset);
return $query;
}
I setup my database, and routes (default->books) already but the page doesn't show anything. My database table is books.
I don't know if $config is a variable of your own class. If it is the global config, maybe your should set its parameters using $this->config->set_item function.
Furthermore, to avoid troubles like #Henesnarfel mentioned before, try to echo the number of results when you execute the query.
function index()
{
$this->load->library('pagination');
$this->config->set_item('base_url',base_url().'index.php/books/index/');
$this->config->set_item('total_rows',$this->db->count_all('books'));
// ECHO to make me sure that the query is succesfully done
echo("Found ".$this->db->count_all('books')." books");
$this->config->set_item('per_page','5');
$this->config->set_item('full_tag_open','<p>');
$this->config->set_item('full_tag_close','</p>');
$this->pagination->initialize($config);
echo "Here";
//load the model and get results
$this->load->model('books_model');
$data['results'] = $this->books_model->get_books($config['per_page'],$this->uri->segment(3));
// load the HTML Table Class
$this->load->library('table');
$this->table->set_heading('Title');
// load the view
$this->load->view('books_view', $data);
}