Pagination Codeigniter - 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');?>

Related

Show data from multiple checkbox in codeigniter

i hope all of you can help me to solve my application problem
my problem is when i would like to show data from my multiple checkbox , data success to show but just showing my first data that i have check.
example is i check data 1 , data 2 , data 3, but the only data 1 are showing on my page.
my controller :
function comparison()
{
if ($this->input->post('submit')) {
foreach ($id_product = $this->input->post('id_product') as $rm) {
$show_compare = $this->Compare->start_compare($rm);
}
$data['comparison'] = $show_compare;
$data['title'] = "Comparison";
$data['meta_keywords'] = ". . .";
$data['meta_descriptions'] = ". . .";
$this->load->view('theme/comparison',$data);
}
}
My Model :
function start_compare($id_product)
{
$this->db->select('product.id_subcategory,product.type,product.product_name,specificcategory.specificcategory_name,specification_biostar.*');
$this->db->join('specification_biostar', 'specification_biostar.id_product = product.id_product', 'left');
$this->db->join('specificcategory', 'specificcategory.id_specificcategory = product.id_subcategory', 'left');
$this->db->where('product.id_product', $id_product);
$sql = $this->db->get('product')->result_array();
return $sql;
}
my view (option multiple-checkbox) :
<div class="box-body">
<input type="checkbox" name="id_product[]" id="txt" onClick="EnableSubmit3(this)" value="<?php echo $row['id_product']; ?>"><label>Choose</label>
</div>
my view (result data) :
<table class="table">
<?php foreach ($comparison as $row){ ?>
<tr>
<td colspan="2"><?php echo $row['id_product'] ?></td>
</tr>
<?php } ?>
</table>
in your code take $show_compare = array(); before foreach loop and use array_push in foreach.
$show_compare = array();
foreach ($id_product = $this->input->post('id_product') as $rm) {
array_push($show_compare,$this->Compare->start_compare($rm));
}
$data['comparison'] = $show_compare;

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>

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 Not working in my Codeigniter

this is my controller file and its not working
controller name blog
model name blogmodel
view file name loggedin
function getvalues(){
$config['base_url'] = 'http://192.168.1.50/codeigni/index.php/blog/getvalues';
$config['total_rows'] = 200;
$config['per_page'] = 20;
$this->pagination->initialize($config);
echo $this->pagination->create_links();
$this->load->model('blogmodel');
$data['results']=$this->blogmodel->getall();
$this->load->view('Login/loggedin',$data);
}
}
this is my model file and model name 'blogmodel'
function getall(){
$query=$this->db->query("SELECT * FROM user_details");
return $query->result();
}
}
this is my view file i cannot access pagination
<p><?php echo $links; ?></p><?php
echo "<table border='1'>
<tr>
<th>id</th>
<th>username</th>
<th>password</th>
<th>email</th>
</tr>";
foreach($results as $row){
echo "<tr>";
echo "<td>" .$row->id;
echo "<td>". $row->username;
echo "<td>". $row->password;
echo "<td>". $row->email;
echo "</tr>";
}
echo "</table>";
?>
Instead of doing echo $this->pagination->create_links();
do as below
$data["links"] = $this->pagination->create_links();

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);

Resources