Codeigniter Pagination with Bootstrap 4 - codeigniter

I don't know if I was the only one too stupid to set the pagination of codeigniter correctly with Bootstrap 4. So here's the code so you can save your time
Codeigniter config:
$config['base_url'] = 'http://yoururl.com/';
$config['total_rows'] = 200;
$config['per_page'] = 10;
$config['num_links'] = 2;
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = FALSE;
$config['last_link'] = FALSE;
$config['next_link'] = '»';
$config['next_tag_open'] = '<li class="page-item">';
$config['next_tag_close'] = '</li>';
$config['prev_link'] = '«';
$config['prev_tag_open'] = '<li class="page-item">';
$config['prev_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="page-item active" aria-current="page"> <a class="page-link">';
$config['cur_tag_close'] = '</a><span class="sr-only">(current)</span></li>';
$config['num_tag_open'] = '<li class="page-item">';
$config['num_tag_close'] = '</li>';
$config['attributes'] = array('class' => 'page-link');
$this->pagination->initialize($config);
Outpou in view:
<nav aria-label="Page navigation example">
<?php echo $this->pagination->create_links(); ?>
</nav>

Related

Solutions for pagination with search functionality

I have a page with search functionality.
In order to continue the search parameters when pagination is required, I have to pass the search term to the pagination links.
$config['reuse_query_string'] = FALSE;
By default your Query String arguments (nothing to do with other query string options) will be ignored. Setting this config to TRUE will add existing query string arguments back into the URL after the URI segment and before the suffix.:
http://example.com/index.php/test/page/20?query=search%term
This helps you mix together normal URI Segments as well as query string arguments, which until 3.0 was not possible.
But setting this to TRUE does not automatically add the search query after the URI offset segment (after a search is performed).
However, I did manage to use
$config['suffix'] = "?my_search_input=$my_query_value";
to create pagination links that look something like /method/controller/20/?my_search_input=hello
But my question is...
Is this the best way to do it, and why didn't $config['reuse_query_string'] = TRUE; append this back to the URL automatically?
Am I not understanding the documentation correctly?
Thanks.
try this change code with your requirment and your table
set controller this way
<?php
class pagination extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
$this->load->database();
$this->load->library('pagination');
$this->load->model('pagination_model');
}
public function index()
{
//pagination settings
$config['base_url'] = site_url('pagination/index');
$config['total_rows'] = $this->db->count_all('tbl_books');
$config['per_page'] = "3";
$config["uri_segment"] = 3;
$choice = $config["total_rows"]/$config["per_page"];
$config["num_links"] = floor($choice);
// integrate bootstrap pagination
$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'] = '«';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '»';
$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['page'] = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
// get books list
$data['booklist'] = $this->pagination_model->get_books($config["per_page"], $data['page'], NULL);
$data['pagination'] = $this->pagination->create_links();
// load view
$this->load->view('pagination_view',$data);
}
function search()
{
// get search string
$search = ($this->input->post("book_name"))? $this->input->post("book_name") : "NIL";
$search = ($this->uri->segment(3)) ? $this->uri->segment(3) : $search;
// pagination settings
$config = array();
$config['base_url'] = site_url("pagination/search/$search");
$config['total_rows'] = $this->pagination_model->get_books_count($search);
$config['per_page'] = "5";
$config["uri_segment"] = 4;
$choice = $config["total_rows"]/$config["per_page"];
$config["num_links"] = floor($choice);
// integrate bootstrap pagination
$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['page'] = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
// get books list
$data['booklist'] = $this->pagination_model->get_books($config['per_page'], $data['page'], $search);
$data['pagination'] = $this->pagination->create_links();
//Load view
$this->load->view('pagination_view',$data);
}
}
?>
set model this way
<?php
class pagination_model extends CI_Model{
function __construct()
{
parent::__construct();
}
//fetch books
function get_books($limit, $start, $st = NULL)
{
if ($st == "NIL") $st = "";
$sql = "select * from tbl_books where name like '%$st%' limit " . $start . ", " . $limit;
$query = $this->db->query($sql);
return $query->result();
}
function get_books_count($st = NULL)
{
if ($st == "NIL") $st = "";
$sql = "select * from tbl_books where name like '%$st%'";
$query = $this->db->query($sql);
return $query->num_rows();
}
}
?>
view look like this
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CodeIgniter Pagination Example with Search Query Filter</title>
<link rel="stylesheet" href="<?php echo base_url("bootstrap/css/bootstrap.css"); ?>">
<style type="text/css">
.bg-border {
border: 1px solid #ddd;
border-radius: 4px 4px;
padding: 15px 15px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2 well">
<?php
$attr = array("class" => "form-horizontal", "role" => "form", "id" => "form1", "name" => "form1");
echo form_open("pagination/search", $attr);?>
<div class="form-group">
<div class="col-md-6">
<input class="form-control" id="book_name" name="book_name" placeholder="Search for Book Name..." type="text" value="<?php echo set_value('book_name'); ?>" />
</div>
<div class="col-md-6">
<input id="btn_search" name="btn_search" type="submit" class="btn btn-danger" value="Search" />
Show All
</div>
</div>
<?php echo form_close(); ?>
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-2 bg-border">
<table class="table table-striped table-hover">
<thead>
<tr>
<th>#</th>
<th>Book Name</th>
<th>Author Name</th>
<th>ISBN</th>
</tr>
</thead>
<tbody>
<?php for ($i = 0; $i < count($booklist); ++$i) { ?>
<tr>
<td><?php echo ($page+$i+1); ?></td>
<td><?php echo $booklist[$i]->name; ?></td>
<td><?php echo $booklist[$i]->author; ?></td>
<td><?php echo $booklist[$i]->isbn; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<?php echo $pagination; ?>
</div>
</div>
</div>
</body>
</html>

Codeigniter pagination using semantic UI

I use bellow code for pagination:
$config['full_tag_open'] = "<div class='ui right floated pagination menu'>";
$config['full_tag_close'] ="</div>";
$config['num_tag_open'] = '<a class="item">';
$config['num_tag_close'] = '</a>';
$config['cur_tag_open'] = '<a class="item">';
$config['cur_tag_close'] = "</a>";
$config['next_tag_open'] = '<a class="item">';
$config['next_tagl_close'] = "</a>";
$config['prev_tag_open'] = '<a class="item">';
$config['prev_tagl_close'] = "</a>";
$config['first_tag_open'] = '<a class="item">';
$config['first_tagl_close'] = "</a>";
$config['last_tag_open'] = '<a class="item">';
$config['last_tagl_close'] = "</a>";
Not working the design part. How to use correctly using semantic ui
create a a file name pagination.php in config
and call it from controller or model
include APPPATH.'config/pagination.php';
when your going to pass this config file and below is the full code:
$config = array();
$config["base_url"] = base_url() . 'brands/index';
$config["total_rows"] = $this->brand->brand_count();
include APPPATH.'config/pagination.php';
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->brand->
get_brands($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view('brand/view',$data);
try this
$config['full_tag_open'] = '<div class="ui pagination menu">';
$config['full_tag_close'] ='</div>';
$config['num_tag_open'] = '<a class="item">';
$config['num_tag_close'] = '</a>';
$config['cur_tag_open'] = '<a class="active item">';
$config['cur_tag_close'] = '</a>';
$config['next_tag_open'] = '<a class="item">';
$config['next_tagl_close'] = '</a>';
$config['prev_tag_open'] = '<a class="item">';
$config['prev_tagl_close'] = '</a>';
$config['first_tag_open'] = '<a class="item">';
$config['first_tagl_close'] = '</a>';
$config['last_tag_open'] = '<a class="item">';
$config['last_tagl_close'] = '</a>';
Semantic UI Pagination Stayles
Here is my working code for Bootstrap v4.0 and Codeigniter v3
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] ='</ul>';
$config['num_tag_open'] = '<li class="page-item">';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['next_tag_open'] = '<li class="page-item"><a href="#" aria-label="Next">';
$config['next_tagl_close'] = '</a></li>';
$config['prev_tag_open'] = '<li class="page-item">';
$config['prev_tagl_close'] = '</li>';
$config['first_tag_open'] = '<li class="page-item disabled">';
$config['first_tagl_close'] = '</li>';
$config['last_tag_open'] = '<li class="page-item"><a href="#" aria-label="Next">';
$config['last_tagl_close'] = '</a></li>';
$config['attributes'] = array('class' => 'page-link');

How to use pagination to display one item per page

I want to display one fact per page using pagination but it displays all facts in one page.
Here is the controller am using
$config = array();
$config["base_url"] = base_url() . "Admin/news/index";
$config["total_rows"] = $this->news_model->record_count();
$config["per_page"] = 2;
$config["uri_segment"] = 1;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->news_model->fetch_countries($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
And this is model
public function record_count() {
return $this->db->count_all("knowbank");
}
public function fetch_countries($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("knowbank");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
Finally my view
<?php foreach ($news as $news_item): ?>
<div class="col1"><?php echo $news_item['title'] ?></div>
<img src="<?php echo base_url('image/' .$news_item['picture']);?>" style="max-width:210px; max-height:180px;"><br />
<p><?php echo $news_item['body'] ?></p>
<p>Category: <?php echo $news_item['category']?><br /><br />
Posted on: <?php echo $news_item['date']?>
</p>
View article<br /><br />
<?php endforeach ?>
<?php echo $links; ?>
Change the following and try, your page offset is the last segment of the url, so its 4
$config["per_page"] = 1;
$config["uri_segment"] = 4;
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;

Codeigniter Bootstrap Pagination

I am making a web app with CodeIgniter and Twitter Bootstrap. I found a resource online with a list of $config settings to style the pagination links properly. Is there a way to store this in a library and load it into the controller so I dont have to type it each time?
You can save this as pagination.php in application/config:
$config['per_page'] = 2;
$config['uri_segment'] = 3;
$config['num_links'] = 9;
$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'page';
$config['full_tag_open'] = '<div class="pagination"><ul>';
$config['full_tag_close'] = '</ul></div><!--pagination-->';
$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['anchor_class'] = 'follow_link';
Very easy solution for bootstrap and codeigniter
$this->load->model('reciever');
$this->load->library('uri');
$this->load->library('pagination');
$config['base_url'] = base_url(). 'users_ci/users';
$config['total_rows'] = $this->reciever->getRows();
$config['per_page'] = 4;
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['prev_link'] = '«';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '»';
$config['next_tag_open'] = '<li>';
$config['next_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>';
$config["num_links"] = round( $config["total_rows"] / $config["per_page"] );
$config['users']= $this->reciever->getUsers(4,$this->uri->segment(3));
$this->pagination->initialize($config);
$config['pages'] = $this->pagination->create_links();
$this->load->view('users',$config);
and for view you have
<div><?php echo $pages; ?></div>
You can always simply create a .php file with a settings array and require_once() it in your model and return to the pagination initiator. Or it's better to create a model/library that will return all the settings to you.
Model:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$pagination_config = array(
'full_tag_open' = '<p>',
'full_tag_close' = '</p>',
// ...
);
class Pagiconf extends CI_Model {
public function load() {
$this->load->library('pagination');
$this->pagination->initialize($config);
return $this->pagination->create_links();
}
}
Anywhere you need it:
$this->load->model('pagiconf');
echo $this->pagiconf->load();
The bellow working fine
$config["base_url"] = base_url() . "product/product/index1";
$config["total_rows"] = $count_products;
$config['per_page'] = 2;
$config['uri_segment'] = 4;
$config['num_links'] = 9;
$config['full_tag_open'] = '<div class="pagination"><ul>';
$config['full_tag_close'] = '</ul></div><!--pagination-->';
$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);
Here is my working code for Bootstrap v4.0
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] ='</ul>';
$config['num_tag_open'] = '<li class="page-item">';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['next_tag_open'] = '<li class="page-item"><a href="#" aria-label="Next">';
$config['next_tagl_close'] = '</a></li>';
$config['prev_tag_open'] = '<li class="page-item">';
$config['prev_tagl_close'] = '</li>';
$config['first_tag_open'] = '<li class="page-item disabled">';
$config['first_tagl_close'] = '</li>';
$config['last_tag_open'] = '<li class="page-item"><a href="#" aria-label="Next">';
$config['last_tagl_close'] = '</a></li>';
$config['attributes'] = array('class' => 'page-link');

Pagination Codeigniter

I'm using Codeigniter..and I'm new to this. What I'm trying to do is to use pagination in my code but the problem is the pagination link is not shown at the bottom.
The controller:
function index() {
$this -> load -> library('pagination');
$config['base_url'] = 'Rfetch1_controller/index/';
$total = $this -> db -> count_all('info');
$per_page = 4;
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$this -> pagination -> initialize($config);
$data['pagination'] = $this -> pagination -> create_links();
$data['list'] = $this -> Rfetch1_model -> get_s($config['per_page'], $this -> uri -> segment(3));
if ($data['list'] !== null) {
$this -> load -> view('Rfetch1_view', $data);
} else {
$this -> load -> view('noresult');
}
}
The model :
function get_s($num, $offset) {
$this -> db -> select('subject, id, problem,image'); // field name
$sql = $this -> db -> get('info', $num, $offset); // table name
if ($sql -> num_rows() > 0) {
foreach($sql -> result() as $row) {
$data[$row -> id] = $row -> subject;
}
return $data;
} else {
return null;
}
}
The view :
foreach($list as $id => $title): ?>
<? echo anchor('Rfetch1_controller/get_by_id/'.$id, $title); ?>
<?php endforeach;?>
<?php echo $this->pagination->create_links(); ?>
Why the pagination link is not showing?
You've already created links in your controller with this:
$data['pagination'] = $this->pagination->create_links();
so you just need to echo $pagination in your view:
<?php echo $pagination; ?>
You didn't load the pagination library in your view, just your controller - so you can't use the create_links() method in your view. But since you did use that method in your controller and pass it through to your view in the $data array, you can use that variable in your view.
$this->load->model('reciever');
$this->load->library('uri');
$this->load->library('pagination');
$config['base_url'] = base_url(). 'users_ci/users';
$config['total_rows'] = $this->reciever->getRows();
$config['per_page'] = 4;
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['prev_link'] = '«';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '»';
$config['next_tag_open'] = '<li>';
$config['next_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>';
$config["num_links"] = round( $config["total_rows"] / $config["per_page"] );
$config['users']= $this->reciever->getUsers(4,$this->uri->segment(3));
$this->pagination->initialize($config);
$config['pages'] = $this->pagination->create_links();
$this->load->view('users',$config);
this is the function that i have in my controler!
and in the view i have this one
<div><?php echo $pages; ?></div>
with this you can use bootstrap also for the pagination view.
You must query your database exactly with the per page you want to saw.
Searching Articles in CodeIgniter
Step 1: Create model search function.
public function search($query)
{
$q=$this->db->from('articles')
->like('title',$query)
->get();
return $q->result();
}
Step 2: Create search function in controller.
public function search()
{
$this->load->library('form_validation');
$this->form_validation->set_rules('query','Query','required');
if(! $this->form_validation->run())
$this->index();
$query=$this->input->post('query');
$this->load->model('articlesmodel','articles');
$articles=$this->articles->search($query);
$this->load->view('public/search_results',compact('articles'));
}
Step 3: Create search_results in view.
<?php include ('public_header.php');?>
<div class="container">
<h1>Serach Results</h1>
<table class="table">
<thead>
<tr>
<td>Sr No.</td>
<td>Article Title</td>
<td>Published On</td>
</tr>
</thead>
<tbody>
<tr>
<?php if(count($articles)):?>
<?php $count=$this->uri->segment(3,0);?>
<?php foreach($articles as $article ):?>
<td><?= ++$count?></td>
<td><?= $article->title?></td>
<td><?= "Date" ?></td>
</tr>
<?php endforeach;?>
<?php else: ?>
<tr>
<td colspan="3"> No Records Found.</td>
</tr>
<?php endif;?>
</tbody>
</table>
</div>
<?php include ('public_footer.php');?>

Resources