I have a problem using codeigniter pagination. The problem is when I open the 2nd page during paging it is showing the error 404 page not found. I do not know why but for the 1st page it is working.
Model:
public function home($limit='',$segment=''){
$data = array();
$this->db->where(array('produk.status_produk'=>'1'))->limit($limit,$segment);
$i=0;
foreach ($this->get_all() as $item){
$data[$i] = $item;
$this->db->where(array('produk_id'=>$item->id_produk,'default'=>'1'));
if($this->db->get('foto_produk')->num_rows() > 0){
$this->db->where(array('produk_id'=>$item->id_produk,'default'=>'1'));
$foto = $this->db->get('foto_produk')->result();
foreach ($foto as $pic) {
$data[$i]->image = $pic->image;
$data[$i]->thumb = $pic->thumb;
}
} else {
$data[$i]->image = '';
$data[$i]->thumb = '';
}
$i++;
}
return $data;
}
Controller:
$cari = $this->input->post('search');
$this->db->like('deskripsi_produk', $cari);
$this->db->or_like('nama_produk', $cari);
$this->db->order_by('id_produk','desc');
$t = $this->produk_m->home();
$this->data->search = $this->input->post('search');
$this->load->library('pagination');
$perPage = 2;
$uri_segment = 2;
$total = count($t);
$config['base_url'] = site_url('search');
$config['total_rows'] = $total;
$config['per_page'] = $perPage;
$config['num_links'] = '2';
$config['uri_segment'] = $uri_segment;
$this->pagination->initialize($config);
$this->data->pagination = $this->pagination->create_links();
$this->db->like('deskripsi_produk', $cari);
$this->db->or_like('nama_produk', $cari);
$this->db->order_by('id_produk','desc');
$this->data->produk = $this->produk_m->home($config['per_page'],$this->uri->segment(2));
$this->template->set_judul('PT Bintang Sriwijaya')
->set_css('style')
->set_parsial('sidebar','sidebar_view',$this->data)
->set_parsial('topmenu','top_view',$this->data)
->render('search',$this->data);
}
View:
<h2>Hasil Pencarian</h2><br />
<?php if($produk):?>
<h3><center><?php echo $pagination;?></center></h3><br />
<?php foreach($produk as $item): ?>
<div class="produk-wrap">
<div class="image-wrap">
<div class="image-iner">
<a href="<?php echo base_url().'index.php/produk/'.$item->nama_kategori.'/'.$item->url_produk;?>">
<?php if($item->thumb == ''): ?>
<div class="no-image">
<span>Belum ada Gambar</span>
</div>
<?php else: ?>
<img src="<?php echo base_url().$item->thumb ?>" />
<?php endif; ?>
<?php if ($item->stok == 0): ?>
<div class="trans">
<span>Stok Habis</span>
</div>
<?php endif; ?>
</div>
</div>
<div class="produk-name">
<?php echo $item->nama_produk;?>
</div>
<?php if($item->harga_baru != 0): ?>
<div class="harga-lama">
Rp. <?php echo $this->cart->format_number($item->harga_jual) ?>
</div>
<div class="harga-jual">
Rp. <?php echo $this->cart->format_number($item->harga_baru) ?>
</div>
<?php else: ?>
<div class="harga-jual">
Rp. <?php echo $this->cart->format_number($item->harga_jual) ?>
</div>
<?php endif; ?>
</a>
</div>
<?php endforeach; ?>
<?php else:
echo "Data yang dicari belum ada";
endif;?>
please change these lines
$uri_segment = 2;
to
$uri_segment = 3;
and
$this->data->produk = $this->produk_m->home($config['per_page'],$this->uri->segment(2));
to
$this->data->produk = $this->produk_m->home($config['per_page'],$this->uri->segment(3));
and try beacause segment 1 is for controller, and 2 for methods in it, 3 may be the pagination element.
Related
I tried to use the codeigniter pagination library and it doesn't work for me. I'm trying to have 24 products on the all products page and when there's more products it should add a link to the next page. So right now I have more than 24 products on the site and no new page is added.
with this line I'm getting all the products from product table:
$data['products'] = $this->Product_model->selectProducts();
This is my controller function:
public function pagination() {
//load pagination library
$this->load->library('pagination');
//laad Products_model voor pagination
$this->load->model('Products_model');
$this->load->model('Product_model');
$config = array();
$config["base_url"] = base_url() . "AlleCadeausController/pagination";
$config["total_rows"] = $this->products->record_count();
$config["per_page"] = 24;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['products'] = $this->Product_model->selectProducts();
$data["links"] = $this->pagination->create_links();
$this->load->view("allecadeaus", $data);
}
My model (Products_model):
<?php
class Products extends CI_Model
{
public function __construct() {
parent::__construct();
}
public function record_count() {
return $this->db->count_all("products");
}
public function fetch_products() {
$query = $this->db->get("products");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
?>
This is my view file (I don't know if I need to add anything in the view file for the pagination to work):
<?php include_once ('templates/header.php'); ?>
<!-- Alle cadeaus gele title bovenaan pagina -->
<div class="all-content">
<div class="row">
<div class="col-lg-12 bg-warning header-text" style="font-size:25px;font-weight: bold;">
<center>Alle cadeaus</center>
</div>
</div>
<hr />
<br>
<!-- Cadeau categorie side menu -->
<div class="row">
<div class="menu">
<div id="categorymenu">
<center> <h3>Categorieën</h3> </center>
<div class="categorieen">
<?php foreach (get_categories_h() as $category) : ?>
<input id="box1" type="checkbox" />
<label for="box1"><a class="listcat" href="<?php echo base_url().'Product/category/' . $category->id; ?>"> <?php echo $category->name; ?></a></label>
<?php endforeach; ?>
</div>
</div>
</div>
<!-- Laat cadeau zien op alle cadeaus pagina -->
<div class="main">
<?php foreach($products as $product) : ?>
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product["product_id"]; ?>"> <div class="main-products">
<img id="cadeaufoto" src="<?php echo base_url(); ?>upload/<?php echo $product["product_foto_thumb"]; ?>">
<div class="product_naam"><h3><?php echo $product["product_naam"]; ?></h3></div>
<div class="ophaal_plaats">
<?php echo $product["ophaal_plaats"]; ?>
</div>
</a>
<div class="aangeboden_door">
<?php
//Here is the active record query which is getting the 'voorname' and other data
$userarray = $this->db->get_Where('users', array('user_id'=>$product["user_id"]))->row_array();
// you can print_r($userarray); for see the array you get
?>
<a href="<?php echo base_url() . 'User/userdetails/'.$product['user_id'];?>">
<td><?php echo $userarray['voornaam'];?></td>
</tr>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php');?>
Below is the fine working pagination code, hope it will fix the issue you have.
Model
public function fetch_oldusers($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get_where('dc_user',array('Is_Hidden'=>0));
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
View
<div class="text-center"><nav aria-label="Page navigation">
<ul class="pagination">
<li><?php echo $links; ?></li>
</ul>
</nav></div>
Controller
public function Users()
{
$config = array();
$config["base_url"] = base_url() . "master/Users";
$config["total_rows"] = $this->bm->record_countolduser();
$config['use_page_numbers'] =True;
$config['cur_tag_open'] = '<a><b class="text-success">';
$config['cur_tag_close'] = '</b></a>';
$config["per_page"] =10;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['title'] = "Users List";
$data["posts"] = $this->bm->fetch_oldusers($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view('Admin/header',$data);
$this->load->view('Admin/nav');
$this->load->view('Admin/sidebar');
$this->load->view('Admin/old_user', $data);
$this->load->view('Admin/footer');
}
It is working fine for me.
I have a project on which i need to implement pagination. I tried searching various time but couldn't find any satisfactory result. That's why I'm posting my question here --
This is Controller file IndexController.php
This is my indexAction, in which I'm trying to implement pagination -
public function indexAction() {
$this->_helper->layout->setLayout('layout');
//pagination
$page = $this->_request->getParam('page');
if (empty($page)) {
$page = 1;
}
$paginator = $this->_objCoupon->getOnePageOfOrderEntries($page);
$this->view->paginator = $paginator;
//pagination ends
$coupons = $this->_objCoupon->getAllcoupon();
$storage = new Zend_Auth_Storage_Session();
$dataUser = $storage->read();
$id = $dataUser->id;
//$id = $storage->id;
$arrCoupon = array();
if(count($coupons) > 0) {
$i= 0;
foreach($coupons as $couponArray) {
$arrCoupon[$i]["pid"] = $couponArray['id'];
$arrCoupon[$i]["title"] = $couponArray['partner_name'];
$arrCoupon[$i]["img"] = $couponArray['companylogo'];
$arrCoupon[$i]["value"] = $couponArray['coupon_value'];
$i++;
}
}
$this->view->arrCoupon = $arrCoupon;
$this->view->selectedcheckbox = $this->selectCheckboxes;
$this->view->id = $id;
$this->view->asd = 'all';
}
This is model file CouponModel.php
And this is my model function
public function getOnePageOfOrderEntries($page=1) {
//$paginator = $this->_coupon->getAllcoupon();
$query = "SELECT * FROM rechargecoupon WHERE online_offer = 'N' ORDER BY id ASC";
$paginator = new Zend_Paginator(
new Zend_Paginator_Adapter_DbTableSelect($query)
);
$paginator->setItemCountPerPage(20);
$paginator->setCurrentPageNumber($page);
return $paginator;
}
And in view I'm just trying to print paginator variable value for now.
This is View file index.phtml
<?php if (count($this->paginator)): ?>
<?php print_r($this->paginator); die; ?>
When I'm executing this file, I'm getting "Class Zend_Paginator_Adapter_DbTableSelect not found error".
You should have these variables in your action:
$currentPage = value obtained from URL, has to be >= 1
$itemsPerPage = hard coded value, ideally taken from configuration
$itemsCount = SELECT COUNT(*) FROM ... WHERE ...
$pagesCount = ceil($itemsCount / $itemsPerPage)
$items = SELECT * FROM ... WHERE ... LIMIT ($currentPage - 1) * $itemsPerPage, $itemsPerPage, these are items to be displayed, for example articles
When you set these variables in your action, you should pass them to your view and generate pagination from them, that means:
If pages count is larger than 1:
display pagination
generate pagination from 1 to $pagesCount = 1, 2, 3 ... 10
each generated pagination number should have associated URL to your action
If pages count is 1:
do not display pagination
First of all.
Correct to use
$query = $this->select()
->from('rechargecoupon')
->where('online_offer = ?', 'N')
->order('id ASC')
$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbTableSelect($query));
$paginator->setItemCountPerPage('50');
$paginator->setCurrentPageNumber($page);
This is in the model.
In view
<?php if ($this->paginator->getTotalItemCount() !== 0): ?>
// echo elements you need
<?php endif; ?>
<?php echo $this->paginationControl( $this->paginator, 'Sliding', 'pagination.phtml' ); ?>
pagination.phtml displays pagination.
in pagination.phtml
<?php if ($this->pageCount > 1): ?>
<div>
<!-- Previous page -->
<?php if (isset($this->previous)): ?>
<a href="<?php echo $this->url(array('page' => $this->previous)); ?>">
<span>< <?php echo 'previous'; ?></span>
</a>
<?php else: ?>
<span class="disabled">< <?php echo 'previous'; ?></span>
<?php endif; ?>
<!-- Page links -->
<?php foreach ($this->pagesInRange as $page): ?>
<?php if ($page != $this->current): ?>
<a href="<?php echo $this->url(array('page' => $page)); ?>">
<span><?php echo $page; ?></span>
</a>
<?php else: ?>
<?php endif; ?>
<?php endforeach; ?>
<?php if(!in_array($this->pageCount, $this->pagesInRange)): ?>
<a href="<?php echo $this->url(array('page' => $this->pageCount)); ?>">
<span><?php echo $this->pageCount; ?></span>
</a>
<?php endif;?>
<?php if (isset($this->next)): ?>
<a href="<?php echo $this->url(array('page' => $this->next)); ?>">
<span><?php echo 'next'; ?> ></span>
</a>
<?php else: ?>
<span class="disabled"><?php echo 'next'; ?> ></span>
<?php endif; ?>
</div>
<?php endif; ?>
Basically Im using Bootstrap themes with Codeigniter
Here is my controller code. tutors is the table from database.
public function index() {
$this->load->library('pagination');
$config['base_url'] = 'http://localhost:8888/bootstrap/index.php/frontpage/index';
$config['total_rows'] = $this->db->get('tutors')->num_rows();
$config['per_page'] = 6;
$config['num_links'] = 10;
$this->pagination->initialize($config);
$data['records'] = $this->db->get('tutors', $config['per_page'], $this->uri->segment(3));
$this->load->view('include/header');
$this->load->view('frontpage', $data);
$this->load->view('include/footer');
}
I want to customize the view as the followings
<div class="container" style="text-align: center;">
<?php
$i=0;
foreach($records as $row) :
if ($i%3==0) { ?>
<div class="row">
<div class="col-md-4">
<h1><?php echo $row->title; ?></h1>
<h2><?php echo $row->id; ?></h2>
</div>
<?php
} else {
?>
<div class="col-md-4">
<h1><?php echo $row->title; ?></h1>
<h2><?php echo $row->id; ?></h2>
</div>
<?php
if ($i%3==2) {
echo "</div>";
}
}
$i++;
endforeach;
?>
</div>
<div class="container">
<?php echo $this->pagination->create_links(); ?>
</div>
But it gets an error. How can I modify it?
Thanks guys.
Try to use
foreach ($query->result() as $row)
instead of foreach($records as $row)
I am trying to apply delete function on selected record in my application. the problem is that the record are displayed in pagination ie 3,4 rows, i am able to delete record from the first page but when i move to the second page and select few records, the delete record buton doesn't work. any adivise will really help.
I am looking for replies
here is my code for listings
public function index($offset=0)
{
if ( $this->session->userdata('u_name') == FALSE )
{
$data['page_title']="Admin Login";
redirect('admin/login',"refresh",$data);
}
$this->load->model('candidate_model');
$limit=4;
$results=$this->candidate_model->get_all_Candidate($limit,$offset);
$data['candidates']=$results['rows'];
$offset = $this->uri->segment(3, 0);
$data['num_results']=$results['num_rows'];
$this->load->library('pagination');
$config=array();
$config['first_url'] = 'candidateList/index';
$config['base_url']=site_url('candidateList/index');
$config['total_rows']=$data['num_results'];
$config['per_page']=$limit;
$config['uri_segment']=3;
$config['first_link'] = '>';
$config['first_tag_open'] = '<div>';
$config['first_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data['page_title']="Candidate List";
$this->load->view('manageCandidate',$data);
}
my code for delete function is bellow
if($this->input->post('Delete')=='Delete')
{
for($i=0;$i<count($_POST['checkbox']);$i++)
{
$candidate_id = $_POST['checkbox'][$i];
$this->candidate_model->deleteCandidate($candidate_id);
$this->session->set_flashdata('deleteSelected','Selected Candidate has been deleted.');
}
$data['page_title']="Candidate List";
redirect('candidateList',$data);
}
Delete function is here:
public function candidate()
{
if ( $this->session->userdata('u_name') == FALSE )
{
$data['page_title']="Admin Login";
redirect('admin/login',"refresh",$data);
}
$checkList=$this->input->post('checkbox');
$this->load->model('candidate_model');
if($checkList!=NULL)
{
if($this->input->post('Delete')=='Delete')
{
for($i=0;$i<count($_POST['checkbox']);$i++)
{
$candidate_id = $_POST['checkbox'][$i];
$this->candidate_model->deleteCandidate($candidate_id);
$this->session->set_flashdata('deleteSelected','Selected Candidate has been deleted.');
}
$data['page_title']="Candidate List";
redirect('candidateList',$data);
}
else if($this->input->post('Email')=='Email')
{
for($i=0;$i<count($_POST['checkbox']);$i++)
{
$admin_id = $_POST['checkbox'][$i];
$email_to[$i] = $_POST['checkbox'][$i];
$data['email_to'] = $email_to;
}
$data['page_title']='Send Mail';
$this->load->view('admin/sendmail4',$data);
}
}
else
{
$this->session->set_flashdata('deleteSelect','Please Select at-least one Candidate.');
$data['page_title']="Candidate List";
redirect('candidateList',$data);
}
}
ManageCandidate code:
<?php
$this->load->view('includes/template3');
?>
<?php
$this->load->view('includes/superAdminMenu');
?>
<div class="dashboard">
Add Candidate
<?php if($this->session->flashdata('deleteCandidate')) : ?>
<p class="successMsg"><?php echo $this->session->flashdata('deleteCandidate')?></p>
<?php endif; ?>
<?php if($this->session->flashdata('editCandidate')) : ?>
<p class="successMsg"><?php echo $this->session->flashdata('editCandidate')?></p>
<?php endif; ?>
<?php if($this->session->flashdata('deleteSelected')) : ?>
<p class="successMsg"><?php echo $this->session->flashdata('deleteSelected')?></p>
<?php endif; ?>
<?php if($this->session->flashdata('deleteSelect')) : ?>
<p class="noRows"><?php echo $this->session->flashdata('deleteSelect')?></p>
<?php endif; ?>
<?php if($this->session->flashdata('msgSent')) : ?>
<p class="successMsg"><?php echo $this->session->flashdata('msgSent')?></p>
<?php endif; ?>
<?php
if($num_results==0)
{ ?>
<p class="noRows"><?php echo "You have not added any Candidate.";?></p>
<?php }
else
if($num_results>0)
{ ?>
<form action="candidateList/candidate" method="post" name="sendMail" class="addformClass" id="candidateList1">
<!-- Script by hscripts.com -->
<!-- copyright of HIOX INDIA -->
<!-- Free javascripts # http://www.hscripts.com -->
<script type="text/javascript">
checked=false;
function checkedAll (candidateList1) {
var aa= document.getElementById('candidateList1');
if (checked == false)
{
checked = true
}
else
{
checked = false
}
for (var i =0; i < aa.elements.length; i++)
{
aa.elements[i].checked = checked;
}
}
</script>
<!-- Script by hscripts.com -->
<?php if(isset($candidates)) { ?>
<div class="candidateTable">
<div class="candidateRowHeading">
<div class="candidateHeadingChkBox">
<h4><input type='checkbox' name='checkall' onclick='checkedAll(candidateList1);'>
</h4>
</div><!--END candidateHeadingChkBox-->
<div class="candidateColHeading1">
<h4>Candidate Name</h4>
</div><!--END candidateColHeading1-->
<div class="candidateColHeading3">
<h4>Email</h4>
</div><!--END candidateColHeading3-->
<div class="candidateColHeading4" style="display:none">
<h4>Status</h4>
</div><!--END candidateColHeading4-->
<div class="candidateColHeading6">
<h4>Recruiter Assigned</h4>
</div><!--END candidateColHeading6-->
<div class="candidateColHeading5">
<h4>Action</h4>
</div><!--END candidateColHeading5-->
</div><!--END candidateRowHeading-->
<?php $count=0; ?>
<?php foreach ($candidates as $candidate) { ?>
<div class="candidateRowData">
<div class="candidateColChkBox">
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $candidate->candidate_id; ?>">
</div><!--END candidateColChkBox-->
<div class="candidateColData1">
<?php echo anchor('admin/viewCandidate/'.$candidate->candidate_id, $candidate->first_name." ".$candidate->last_name); ?>
</div><!--END candidateColData1-->
<div class="candidateColData3">
<h4><?php echo $candidate->email; ?></h4>
</div><!--END candidateColData3-->
<div class="candidateColData4" style="display:none">
<h4><?php echo $candidate->lead_status; ?></h4>
</div><!--END candidateColData4-->
<div class="candidateColData6">
<h4>
<?php if($candidate->recruiter_id_fk!=0) { ?>
<?php echo get_recruiterFirstName($candidate->recruiter_id_fk); ?>
<?php echo get_recruiterLastName($candidate->recruiter_id_fk); ?>
<?php echo " (<strong>"; ?>
<?php echo get_recruiterLogin($candidate->recruiter_id_fk); ?>
<?php echo "</strong>) "; ?>
<?php }
else if($candidate->recruiter_id_fk==0) { ?>
<?php echo "Not Assigned"; ?>
<?php } ?>
</h4>
</div><!--END candidateColData6-->
<div class="candidateColData5">
<?php echo anchor('admin/editCandidate/'.$candidate->candidate_id,'Edit'); ?>
<?php echo anchor('admin/deleteCandidate/'.$candidate->candidate_id,'Delete'); ?>
</div><!--END candidateColData5-->
</div><!--END candidateRowData-->
<?php $count++; } ?>
</div><!--END candidateTableCom-->
<?php } ?>
<?php } ?>
<?php if(isset($candidates)) { ?>
<div id="pageNum">
<?php echo $this->pagination->create_links(); ?>
</div>
<?php } ?>
<?php
if($num_results>0) {
echo '<input type="submit" name="Delete" class="emailAllBtn" value="Delete" />';
echo '<input type="submit" name="Email" class="emailAllBtn" value="Email" />';
echo "</form>";
} ?>
</div>
<?php
$this->load->view('includes/footer2');
?>
<?php
function get_recruiterFirstName($id)
{
$CI =& get_instance();
$mod = $CI->load->model('recruiter_model');
$count = $CI->recruiter_model->get_recruiterFirstName($id);
return $count;
}
?>
<?php
function get_recruiterLastName($id)
{
$CI =& get_instance();
$mod = $CI->load->model('recruiter_model');
$count = $CI->recruiter_model->get_recruiterLastName($id);
return $count;
}
?>
<?php
function get_recruiterLogin($id)
{
$CI =& get_instance();
$mod = $CI->load->model('recruiter_model');
$count = $CI->recruiter_model->get_recruiterLogin($id);
return $count;
}
?>
Small mistake exist in your pagination code offset value correct it
public function index($offset=0)
{
if ( $this->session->userdata('u_name') == FALSE )
{
$data['page_title']="Admin Login";
redirect('admin/login',"refresh",$data);
}
$this->load->model('candidate_model');
$limit=4;
//whenever you are calling this pagination offset value you need to fetch it from URL
$offset_url = $offset = $this->uri->segment(3, 0);
//if it's not exist take the default value
$offset = is_numeric($offset_url)?$offset_url:$offset;
$results=$this->candidate_model->get_all_Candidate($limit,$offset);
$data['candidates']=$results['rows'];
..............
}
I would add each two result one div class
<?php while ($fetch = $db->fetch($query)) { ?>
<?php echo $fetch['title']; ?>
<?php } ?>
Output should be like this
<div class="one">
<div class="two">
<article>Title</article>
<article>Title</article>
</div>
</div>
<div class="one">
<div class="two">
<article>Title</article>
<article>Title</article>
</div>
</div>>
<?php
$i=0;
while ($fetch = $db->fetch($query)) { ?>
<?php if ($i%2==0) { ?>
<div class="one">
<div class="two">
<?php } ?>
<article><?php echo $fetch['title']; ?></article>
<?php if ($i++%2==1) { ?>
</div>
</div>
<?php } ?>
<?php } ?>
//Also is a good idea to verify if the <div> tags are closed
<?php if ($i%2==1) { ?>
</div>
</div>
<?php } ?>
$count = 0;
while ($fetch = $db->fetch($query))
{
if ($count == 0)
echo '<div class="one"><div class="two">';
if ($count < 2)
echo '<article>'.$fetch['title'].'</article>';
if ($count == 2)
{
echo '</div></div>';
$count = 0;
}
$count++;
}