Solutions for pagination with search functionality - codeigniter

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>

Related

Codeigniter Pagination with Bootstrap 4

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>

codeigniter dynamic menu creation

hi I am trying to create a dynamic menu with submenus. I am almost done but the loop is repeating as you can see in the output image, which should not happen. Error is like 1.Fruits main menu is repeating .[2].fruits sub category are splitting and displaying .
error output
menu.php
$where_array = array('role_id' => $currentRolename , 'active_status' => '1'); //step1
$table = 'bg_assignment';
$query = $this->db->where($where_array)->get($table);
foreach ($query->result() as $perm)
{
$bg = $perm->bg_id;
$temp = '1';
$where_array2 = array('role_id' => $currentRolename , 'permission' => '1'); //step2
$table2 = 'role_permission';
$query2 = $this->db->where($where_array2)->get($table2);
foreach ($query2->result() as $r2)
{
$bg_id = $r2->bg_id;
$where_array3 = array('bg_id' => $bg_id);
$table3 = 'bg_forms';
$query3 = $this->db->where($where_array3)->get($table3);
foreach ($query3->result() as $r3)
{
$where_array4 = array('phpfile_name' => $r3->phpfile_name);
$table4 = 'bg_forms';
$query4 = $this->db->where($where_array4)->get($table4);
foreach ($query4->result() as $r4)
{
?>
<li class="treeview <?php echo menu_li_active($r4->category_name); ?>">
<a href="#">
<i class="fa fa-sitemap"></i>
<span> <?php echo ($r4->category_name); ?></span>
<i class="fa fa-angle-left pull-right"></i>
</a>
<ul class="treeview-menu">
<?php echo menu_link($r4->controller.'/'.$r4->phpfile_name, $r4->displayform_name); ?>
</ul>
<?php } }?>
</li>
</li>
<?php
}
}
?>
I didn't do any test, just throw some ideas here, I think it will be much easier and clearer if you just use inner join in the query. Below code might have a syntax error, I don't have a similar environment as yours to do the tests.
<?php
//Let me just use open query to make this clear.
$sql_query = '
select rp.bg_id,
bf.category_name,
bf.controller,
bf.phpfile_name,
bf.displayform_name
from bg_assignment ba
inner join Role_permission rp
on ba.role_id = rp.role_id
and ba.bg_id = rp.bg_id --You might also want this line
inner join Bg_forms bf
on bg_id = bf.bg_id
where rp.permission = 1
and ba.active_status = 1
order by rp.bg_id, bf.displayform_name
';//I missed this semicolon
//Above sql query will give you two records
//Please test above query in your mysql to see if you can get below two records
/*
bg_id category_name controller phpfile_name displayform_name
5 Fruits Product index Apple
5 Frutis Product sales Orange
*/
$query = $this->db->query($sql_query);
$result = $query->result_array();
if(count($result) > 0){ // We get records!
$html = '';
for($i = 0; $i<count($result); $i++){
$s = $result[$i];
//Make the first li no matter what
if($i === 0){
$html.= '<li class="treeview">'.menu_li_active($s->category_name);
$html.= '<a href="#">';
$html.= '<i class="fa fa-sitemap"></i>';
$html.= '<span>'.$s->category_name.'</span>';
$html.= '<i class="fa fa-angle-left pull-right"></i>';
$html.= '</a>';
$html.= '</li>';
$html.= '<ul class="treeview-menu">';
$html.= menu_link($s->controller.'/'.$s->phpfile_name, $s->displayform_name);
if(count($result)===1){
$html.= '</ul>';
}
}
else{
if($s->category_name === $result[$i-1]->category_name){
//If current category is the same as previous one, we put them in one tab
$html.= menu_link($s->controller.'/'.$s->phpfile_name, $s->displayform_name);
}
else{
//If different, create a new tab
$html.='</ul>';
$html.= '<li class="treeview">'.menu_li_active($s->category_name);
$html.= '<a href="#">';
$html.= '<i class="fa fa-sitemap"></i>';
$html.= '<span>'.$s->category_name.'</span>';
$html.= '<i class="fa fa-angle-left pull-right"></i>';
$html.= '</a>';
$html.= '</li>';
$html.= '<ul class="treeview-menu">';
$html.= menu_link($s->controller.'/'.$s->phpfile_name, $s->displayform_name);
}
}
}
$html.='</li>';
echo $html;
}
?>

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;

Pagination in view does not show last entry on codeigniter

I have this problem:
the links form codeigniter pagination class does not show last entry,
e.g. if the total number of rows returned is 2, it will display on one results and the links to the last entry would be greyed out.
Here is the Controller:
function productlisting () {
$ids=$this->uri->segment(3);
$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/listing_category/productlisting/'.$ids;
$config['per_page'] = '1';
$this->load->model('product_model');
$config['total_rows'] =$this->load->model('product_model')->get_num_product();
$this->pagination->initialize($config);
//load the model and get results
$pages = $this->uri->segment(3);//? $this->uri->segment(4) : 0;
$query['products']=$this->load->model('product_model')->get_specific_product($config['per_page'],$pages);
$query['paginated']=$this->pagination->create_links();
// load the view
echo $config['base_url'].'<br/>';
echo 'total rows:'. $config['total_rows'];
$this->template->build('product_display',$query);
}
Here is my model
//query to get the total number of rows
function get_num_product(){
$listingcategory = $this->uri->segment(3);
$this->db->select('default_listings.listing_id_unique,manufacturer,imagename,item_location,thumbnail,default_listings.item_category_id,product_name,item_serial_number,item_description,size,accessories,othercharacteristics,product_age');
$this->db->where('default_listings.item_category_id',$listingcategory);
$this->db->from('default_listings');
$this->db->join('default_listings_pics', 'default_listings.listing_id_unique=default_listings_pics.listing_id_unique', 'left');
$this->db->distinct('listing_id_unique');
$this->db->group_by('default_listings.listing_id_unique');
$queried= $this->db->get();
$rowcount = $queried->num_rows();
return $rowcount;
}
// Query to get the actual entries
function get_specific_product($num, $offset){
$listingcategory = $this->uri->segment(3);
$this->db->select('default_listings.listing_id_unique,manufacturer,imagename,item_location,thumbnail,default_listings.item_category_id,product_name,item_serial_number,item_description,size,accessories,othercharacteristics,product_age');
$this->db->where('default_listings.item_category_id',$listingcategory);
$this->db->from('default_listings');
$this->db->join('default_listings_pics', 'default_listings.listing_id_unique=default_listings_pics.listing_id_unique', 'left');
$this->db->distinct('listing_id_unique');
$this->db->group_by('default_listings.listing_id_unique');
$this->db->limit($num, $offset);
$query= $this->db->get()->result();
return $query;
}
and finally my view
<div id="catetogorylisitngcover">
<h1>Products</h1>
<?php foreach ( $products as $productdetail):?>
<div class="eachproducts">
<div class="productimage">
<?php if($productdetail->thumbnail):?>
<img src="<?php echo base_url();?>pictures/<?php echo $productdetail->thumbnail;?>"/>
<?php else :?>
<img src="<?php echo base_url();?>img/noimage.png"/>
</div>
<div class="productcover">
<div><span >Category Id:</span><span class="producttext"><?php echo $productdetail->item_category_id ;?></span></div>
<div><span >Product Name:</span><span class="producttext"><?php echo $productdetail->product_name;?></pan></div>
<div><span >Manufacturer :</span><span class="producttext"><?php echo $productdetail->manufacturer;?></span></div>
<div><span >Location :</span><span class="producttext"><?php echo $productdetail->item_location;?></span></div>
<div><span >Listing Id :</span><span class="producttext"><?php echo $productdetail->listing_id_unique;?></span></div>
<div class="learmoreproduct"><img src="<?php echo base_url();?>img/prodreadmore.png"/></div>
</div>
</div>
<?php endforeach;?>
<?php echo $this->pagination->create_links(); ?>
</div>
Help needed!
use it as
$this->load->library('pagination');
$config['base_url'] = your base url without offset in your case // base_url().'index.php/listing_category/productlisting/';
$config['total_rows'] = your total number of rows;
$config['per_page'] = number of rows you want to display;
$config['uri_segment'] = uri segment which will be used as offset;
$this->pagination->initialize($config);

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