making correct pagination - php

When I display a data table for this page it works, but when I try to add pagination it becomes an error. And even if I have a similar problem where do I find a solution
How to make the correct script to display pagination based table data?
Where is the error in my code?
View
<table class="table mt-3">
<thead class="table-danger text-center">
<tr>
<th scope="col">Kode</th>
<th scope="col">Type</th>
<th scope="col">Nama Barang</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody class="text-center table-primary">
<?php foreach ( $barang as $tb ) : ?>
<tr>
<td class="text-uppercase"><?= $tb['barang_kode'] ?></td>
<td class="text-capitalize"><?= $tb['type_nama'] ?></td>
<td class="text-capitalize"><?= $tb['barang_nama'] ?></td>
<td>
<button id="btnGroupDrop1" type="button" class="btn btn-outline-danger dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Action</button>
<div class="dropdown-menu" aria-labelledby="btnGroupDrop1">
<a class="dropdown-item" href="<?= base_url(); ?>gudang/hapus_type/<?= $tb['barang_id']; ?>" onclick="return confirm('Yakin akan menghapus data ini ?');">Hapus</a>
<a class="dropdown-item" href="<?= base_url(); ?>gudang/ubah_type/<?= $tb['barang_id']; ?>">Ubah</a>
</div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="container">
<?php // Tampilkan link-link paginationnya
echo $barang['pagination']; ?>
</div>
models
$config['base_url'] = base_url('gudang/lihat_type');
$config['total_rows'] = $this->db->query($query)->num_rows();
$config['per_page'] = 5;
$config['uri_segment'] = 3;
$config['num_links'] = 3;
$config['full_tag_open'] = '<ul class="pagination pagination-sm m-t-xs m-b-xs">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = 'First';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['last_link'] = 'Last';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['next_link'] = ' <i class="glyphicon glyphicon-menu-right"></i> ';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['prev_link'] = ' <i class="glyphicon glyphicon-menu-left"></i> ';
$config['prev_tag_open'] = '<li>';
$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>';
$config['num_tag_close'] = '</li>'; // End style pagination
$this->pagination->initialize($config); // Set konfigurasi paginationnya
$page = ($this->uri->segment($config['uri_segment'])) ? $this->uri->segment($config['uri_segment']) : 0;
$query .= " LIMIT ".$page.", ".$config['per_page'];
$data['limit'] = $config['per_page'];
$data['total_rows'] = $config['total_rows'];
$data['pagination'] = $this->pagination->create_links();
$data['barang'] = $this->db->query($query)->result_array();
return $data

On the model, change :
$data['pagination'] = $this->pagination->create_links();
to
$data['barang']['pagination'] = $this->pagination->create_links();
so that the pagination data is moved inside the $barang array.

Related

Double pagination in one view for CI 3

We need to have two different pagination tables for one view because a client wanted to have it. It shows up no problem, but the problem is, when I change the pages of the first table, it's fine. But when I change the second table to the second of this page, the highlight of the second page don't light up.
Any advice? Or any possible alternate solutions for having two paginations in one view?
my config is:
$data['title'] = 'Dashboard ';
$configPendingDash = array();
$configOpenDash = array();
$configPendingDash["base_url"] = base_url() . "/index.php/Users/dashboard";
$configPendingDash["total_rows"] = $this->Users_model->get_count_pending_applications();
$configPendingDash["per_page"] = 5;
$configPendingDash["uri_segment"] = 3;
$configOpenDash["base_url"] = base_url() . "/index.php/Users/dashboard/".$this->uri->segment(3,0);
$configOpenDash["total_rows"] = $this->Users_model->get_count_open_applications();
$configOpenDash["per_page"] = 5;
$configOpenDash["uri_segment"] = 4;
$configPendingDash['full_tag_open'] = '<div class="pagination">';
$configPendingDash['full_tag_close'] = '</div>';
$configPendingDash['first_link'] = 'First';
$configPendingDash['last_link'] = 'Last';
$configPendingDash['first_tag_open'] = '<li class="page-item"><span class="page-link">';
$configPendingDash['first_tag_close'] = '</span></li>';
$configPendingDash['prev_link'] = '&laquo';
$configPendingDash['prev_tag_open'] = '<li class="page-item"><span class="page-link">';
$configPendingDash['prev_tag_close'] = '</span></li>';
$configPendingDash['next_link'] = '&raquo';
$configPendingDash['next_tag_open'] = '<li class="page-item"><span class="page-link">';
$configPendingDash['next_tag_close'] = '</span></li>';
$configPendingDash['last_tag_open'] = '<li class="page-item"><span class="page-link">';
$configPendingDash['last_tag_close'] = '</span></li>';
$configPendingDash['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';
$configPendingDash['cur_tag_close'] = '</a></li>';
$configPendingDash['num_tag_open'] = '<li class="page-item"><span class="page-link">';
$configPendingDash['num_tag_close'] = '</span></li>';
$configOpenDash['full_tag_open'] = '<div class="pagination">';
$configOpenDash['full_tag_close'] = '</div>';
$configOpenDash['first_link'] = 'First';
$configOpenDash['last_link'] = 'Last';
$configOpenDash['first_tag_open'] = '<li class="page-item"><span class="page-link">';
$configOpenDash['first_tag_close'] = '</span></li>';
$configOpenDash['prev_link'] = '&laquo';
$configOpenDash['prev_tag_open'] = '<li class="page-item"><span class="page-link">';
$configOpenDash['prev_tag_close'] = '</span></li>';
$configOpenDash['next_link'] = '&raquo';
$configOpenDash['next_tag_open'] = '<li class="page-item"><span class="page-link">';
$configOpenDash['next_tag_close'] = '</span></li>';
$configOpenDash['last_tag_open'] = '<li class="page-item"><span class="page-link">';
$configOpenDash['last_tag_close'] = '</span></li>';
$configOpenDash['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';
$configOpenDash['cur_tag_close'] = '</a></li>';
$configOpenDash['num_tag_open'] = '<li class="page-item"><span class="page-link">';
$configOpenDash['num_tag_close'] = '</span></li>';
$this->pagination->initialize($configPendingDash);
$data["pendinglink"] = $this->pagination->create_links();
$this->pagination->initialize($configOpenDash);
$data["openlink"] = $this->pagination->create_links();
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$page2 = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$data['pending_applications'] = $this->Users_model->get_count_pending_applications($configPendingDash["per_page"], $page);
$data['open_applications'] = $this->Users_model->get_count_open_applications($configOpenDash["per_page"], $page2);
$this->load->view('include/header', $data);
$this->load->view('include/sidetopbar', $data);
$this->load->view('user/dashboard', $data);
my model is:
public function get_count_open_applications()
{
if(empty($searchBook))
$this->db->where('col_status', 'OPEN');
$this->db->from($this->tbl_loan_application);
return $this->db->count_all_results();
}
public function get_count_pending_applications()
{
$this->db->where('col_status', 'PENDING');
$this->db->from($this->tbl_loan_application);
return $this->db->count_all_results();
}
public function get_maximum_applications($limit, $start)
{
$this->db->limit($limit, $start);
$this->db->select('*');
$this->db->from($this->tbl_loan_application);
$this->db->where('col_status', 'PENDING');
$this->db->order_by('loan_id','desc');
$query=$this->db->get();
return $query->result();
}
public function get_open_applications($limit, $start)
{
$this->db->limit($limit, $start);
$this->db->select('*');
$this->db->from($this->tbl_loan_application);
$this->db->where('col_status', 'OPEN');
$this->db->order_by('loan_id','desc');
$query=$this->db->get();
return $query->result();
}
my view is:
<div class="table-responsive">
<table class="table table-sm table-hover table-bordered">
<thead class="thead">
<?php if(empty($pending_applications)){
echo "<strong>Whoops!</strong> Looks like there's no pending loans as of the moment.";
} else {?>
<tr>
<th scope="col">Name</th>
<th scope="col">Company Code</th>
<th scope="col">Loan Type</th>
<th scope="col">Period</th>
<th scope="col">Borrower ID</th>
<th scope="col">Co-maker</th>
<th scope="col">Loanable</th>
<th scope="col">Terms</th>
<th scope="col">Status</th>
</tr>
<?php } ?>
</thead>
<tbody>
<?php foreach($pending_applications as $row): ?>
<tr>
<th scope="row">
<strong><?php echo $row->col_full_name; ?> </strong><br>
<?php echo $row->col_loan_assignid; ?>
</th>
<td><?php echo $row->col_comp_code; ?></td>
<td><?php echo $row->col_loan_type; ?></td>
<td scope="row">
<?php echo $row->col_dte_application; ?><br><br>
<?php echo $row->col_due_date; ?>
</td>
<td><?php echo $row->col_borrower_id; ?></td>
<td><?php echo $row->col_coMaker_perc; ?></td>
<td><?php echo $row->col_loanable; ?></td>
<td><?php echo $row->col_terms; ?></td>
<td>
<button class="btn btn-secondary btn-xs" data-bs-toggle="modal" data-bs-target="#exampleModal">Pending</button>
<button class="btn btn-link btn-xs">View</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<p><?php echo $pendinglink; ?></p>
</div>
When creating the first set of pagination links, CodeIgniter divides the page offset value from the url by the per_page value from the configuration, and saves this internally as the current page value.
When you initialize the library for the second time, it does not seem to reset this current page value. And since it already has a current page value, it does not use the page offset from the url anymore, and marks the wrong page as "active" in the second set of pagination links.
You can solve this by setting cur_page manually for the second call to initialize:
Either set it to something that is considered "empty" by php to reset the page number and force CodeIgniter to use the value from the url again: $configOpenDash['cur_page'] = 0;
Or, if this gives problems with php8, set it explicitly like in your previous question:
$configOpenDash['cur_page'] = $this->uri->segment(4) ?: -1;

missing data in table with php

Currently I am calling a table of publications and add a page with php, when going through the data with a foreach, I have noticed that I am missing 3 data, for example it brings me the ID 1 but hey it jumps to ID 5 without showing the ID's [ 2,3,4]
In the previous image it is shown what I commented previously, it does not show all the fields of the table.
the data model i am using is the following:
public function list( $uuid = null, $usuario_uuid = null, $estado_id = null, $pagina = 0)
{
$this->db->select('publicacion.*,
concat(usuario.nombre, " ", usuario.paterno) as "usuario",
COALESCE(renta.nombre, "No definido") as "renta",
COALESCE(jornada.nombre, "No definido") as "jornada",
COALESCE(comuna.nombre, "No definido") as "comuna",
COALESCE(profesion.nombre, "No definido") as "profesion",
rango_etario.desde, rango_etario.hasta,
renta.desde as renta_desde, renta.hasta as renta_hasta,
');
$this->db->join('usuario', 'usuario.uuid = publicacion.usuario_uuid');
$this->db->join('renta', 'renta.id = publicacion.renta_id', 'left');
$this->db->join('jornada', 'jornada.id = publicacion.jornada_id', 'left');
$this->db->join('comuna', 'comuna.id = publicacion.comuna_id', 'left');
$this->db->join('profesion', 'profesion.id = publicacion.profesion_id', 'left');
$this->db->join('rango_etario', 'rango_etario.id = publicacion.rango_etario_id', 'left');
if ($uuid != null) {
$this->db->where('publicacion.uuid', $uuid);
return $this->db->get('publicacion',REGISTROS_PAGINA, $pagina)->row_array();
}
if ($usuario_uuid != null) {
$this->db->where('publicacion.usuario_uuid', $usuario_uuid);
}
if ($estado_id != null) {
$this->db->where('publicacion.estado_publicacion_id', $estado_id);
}
return $this->db->get('publicacion')->result_array();
}
function total($usuario_uuid = null, $estado = null)
{
if ($usuario_uuid != null) {
$this->db->where('publicacion.usuario_uuid', $usuario_uuid);
}
if ($estado != null) {
$this->db->where('publicacion.estado_publicacion_id', $estado);
}else{
$this->db->where_in('publicacion.estado_publicacion_id', [1,2]);
}
return $this->db->get('publicacion')->num_rows();
}
my controller is the following:
function publicaciones($offset = 0)
{
$this->load->model('Modelo_publicacion');
$this->load->model('Modelo_usuario');
$this->load->library('pagination');
$config['base_url'] = base_url("Administracion/publicaciones");
$config['total_rows'] = $this->Modelo_publicacion->total();
$config['per_page'] = REGISTROS_PAGINA;
$config['uri_segment'] = 3;
$config['num_links'] = 5;
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['attributes'] = ['class' => 'page-link'];
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '<li class="page-item">';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '&laquo';
$config['prev_tag_open'] = '<li class="page-item">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '&raquo';
$config['next_tag_open'] = '<li class="page-item">';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li class="page-item">';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="page-item active"><a href="#" class="page-link">';
$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
$config['num_tag_open'] = '<li class="page-item">';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$data["paginacion"] = $this->pagination->create_links();
$data["publicaciones"] = $this->Modelo_publicacion->list(null, null, null, $offset);
$this->load->view('admin/publicaciones/list_publicaciones', $data);
}
and finally my html code section from where I call the data:
<div class="row">
<div class="col-10 offset-1 table-responsive pt-3">
<table class="table table-striped table-bordered ">
<thead class="thead-dark bg-secondary">
<th>#</th>
<th>Nombre</th>
<th>Tipo Empleo</th>
<th>Comuna</th>
<th>Jornada</th>
<th>Renta ofrecida</th>
<th>Fecha publicación</th>
<th>Estado</th>
</thead>
<tbody>
<?php sort($publicaciones);
foreach ($publicaciones as $p) { ?>
<tr>
<td><?= $p["id"] ?></td>
<td><?= $p["usuario"] ?></td>
<?php if($p["nombre"] != null){ ?>
<td><?= $p["nombre"] ?></td>
<?php } else { ?>
<td>No definido</td>
<?php } ?>
<td><?= $p["comuna"] ?></td>
<td> <?= $p["jornada"] ?> </td>
<td><?= $p["renta_id"] == "-1" ? $p["otra_remuneracion"] : $p["renta"] ?></td>
<td> <?= $p["fecha_creacion"] ?> </td>
<td> <?= $p["estado_publicacion_id"] ?> </td>
</tr>
<?php } ?>
</tbody>
</table>
I've changed the model and controller data, but I'm still missing data. Also look for similar questions but none have an answer that has helped me solve this problem.

In codeigniter how to do pagination on searched values

list page
<div class="container">
<!--search criteria-->
<div class="alert alert-info">
<form method="post" class="form-inline" action="<?php echo base_url();?>/admin/area/index" id="form_search">
<div class="pull-left" style="color:#666;">Show Results:
<select name="limit" id="limit" style="width:50px;">
<option value="10"<?php if(10==$limit_selected) echo "selected";?>>10</option>
<option value="20" <?php if(20==$limit_selected) echo "selected";?>>20</option>
<option value="30" <?php if(30==$limit_selected) echo "selected";?>>30</option>
<option value="40" <?php if(40==$limit_selected) echo "selected";?>>40</option>
<option value="50" <?php if(50==$limit_selected) echo "selected";?>>50</option>
</select>
</div>
<label style="color:#666;"> Search By City: </label>
<select id="cityId" name="cityId" >
<option value='0'> --All-- </option>
<?php
foreach($cityOptionList as $city) { ?>
<option value="<?php echo $city['id']?>" <?php if($city['id']==$cityId) echo "selected";?>><?php echo $city['name']?></option>
<?php } ?>
</select>
</form>
</div>
<!--search criteria end-->
<table class="table table-striped table-bordered bootstrap-datatable">
<thead>
<tr>
<th>Sl.no</th>
<th>Area Name</th>
<th>City</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php $i=1; foreach($areaList as $area) {?>
<tr>
<td><?php echo $i;?></td>
<td class="center"><?php echo $area->areaName;?></td>
<td class="center"><?php echo $area->city_name;?></td>
<td class="center">
<a href="<?php echo base_url();?>admin/area/edit/<?php echo $area->areaId ; ?>">
<i class="icon-edit" style="font-size:16px;"></i></a>
<!-- <a href="#" data-id="<?php echo $area->areaId;?>">
<i class="icon-trash" style="font-size:16px;"></i></a>-->
</td>
</tr>
<?php $i++; } ?>
</tbody>
</table>
<?php echo $links;?>
</div>
<!--/container-->
<!--=== End Content Part ===-->
<script>
$(function() {
$("#cityId").change(function() {
$("#form_search").submit();
});
$("#limit").change(function() {
$("#form_search").submit();
});
/*
$(".pagination").click(function(){
alert("hi");
$("#form_search").submit();
})
*/
});
</script>
controller code
public function index(){
$searchParam = '';
if(! empty($this->input->get('q'))){
$searchParam = $this->input->get('q');
}
if(!empty($this->input->post('cityId'))){
$search_city= $this->input->post('cityId');
}
$data['title'] = 'Area';
$data['action'] = 'admin/area/';
$data['searchParam'] = $searchParam;
$data['arealist'] = $this->getList();
$this->load->view('admin/header',$data);
$this->load->view('admin/area/view.php', $data);
$this->load->view('admin/footer',$data);
}
protected function getList(){
$stateId = $this->session->userdata('stateId');
$searchParam = '';
$search_city = '';
if(! empty($this->input->get('q'))){
$searchParam = $this->input->get('q');
}
if(!empty($this->input->post('cityId'))){
$search_city= $this->input->post('cityId');
}
if(!empty($this->input->post('limit'))){
$limit= $this->input->post('limit');
}
else
{
$limit = 10;
}
$config = $this->config->item('pagination');
$config = array();
$config["base_url"] = base_url() . "admin/area/index/";
$config["total_rows"] = $this->area_model->getArea(0, 0, $searchParam, 1,$search_city);
$totalCount = $config['total_rows'];
$config["per_page"] = $limit;
$config["uri_segment"] = 4;
$config['use_page_numbers'] = TRUE;
$config['reuse_query_string'] = TRUE;
$data['cityId'] = $search_city;
$data['limit_selected'] = $limit;
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 1;
$data["links"] = $this->pagination->create_links();
$data["areaList"] = $this->area_model->getArea($config["per_page"], $page, $searchParam,0,$search_city);
$data['cityOptionList'] = $this->area_model->getCityByStateId($stateId);
return $this->load->view('admin/area/list.php', $data, true);
}
in list page when i change the city then the data is getting according to city and displaying in list page and the pagination is displayed if there are more than selected records.
The problem is when i click on pagination link for searched values then all the records are loading instead of search values.
In pagination how to get the searched values only if the search is applied on the records.
Sorry missed your whole question. To incudle your search parameters as filters in paging you need to add them.Refer below link to see what setting in config you need to change.
https://www.codeigniter.com/userguide3/libraries/pagination.html#customizing-the-pagination
you need to change below setting.
$config[‘reuse_query_string’] = FALSE;
I'm guessing from your code that the search form will do a POST action when it gets submitted by the user. After the user submitted the form, of course it will work. Because they just did a POST request, and you fetch the search query from $_POST by using $this->input->post('user_input') or something similar. But then, when they try to navigate to another page by clicking on the page number link (which is a GET request), the search fails because the superglobal $_POST array is empty.
A solution to solve this problem is to set a session value (I recommend that you use CodeIgniter's Session Class) based on the user input and instead of using the search query from $_POST, and use the one stored in session instead.
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();
}
}
?>
then your veiw page looks like
<!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 add pagination to search result

I have a datatable with pagination and I have just added a search function. At the moment my search results do not have pagination. I have tried to add the configurations from my index to the search function but it hasn't worked. I'm wondering if I'm missing something. I'd appreciate it if someone can take a look at it?
Controller
public function index(){
$config['base_url'] = base_url('/Control/Users');
$config['total_rows'] = $this->UserModel->countUsers();
$config['per_page'] = 30;
$config['uri_segment'] = 3;
$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="current"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['first_link'] = '<<';
$config['last_link'] = '>>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['datatable'] = $this->UserModel->getAllUsers($config["per_page"], $page);
$data['links'] = $this->pagination->create_links();
$data['counter'] = $config['total_rows'];
$data['message']='';
$this->load->view('control/controlMenu/navigationLink');
$this->load->view('control/controlUsers/manageUsers',$data);
$this->load->view('control/controlMenu/navigationJquery');
}
public function searchUser(){
$inputsearchterm = $this->input->post('inputsearchterm');
if($inputsearchterm!=""){
$data['datatable'] = $this->UserModel->searchUser($inputsearchterm);
$data['links'] = '';
$data['counter'] = $this->UserModel->countSearchUser($inputsearchterm);
$data['message']='';
$this->load->view('control/controlMenu/navigationLink');
$this->load->view('control/controlUsers/manageUsers',$data);
$this->load->view('control/controlMenu/navigationJquery');
}else{
redirect('Control/Users');
}
}
View
<?php echo form_open_multipart('Control/Users/searchUser','class="inputform"');?>
<div class="form-group">
<label for="inputsearchterm">Search</label>
<input type="text" class="form-control" id="inputsearchterm" name="inputsearchterm" placeholder="Search">
</div>
<button type="submit" class="btn btn-primary">Search</button>
<?php echo form_close(); ?>
<table class="table">
<thead>
<tr>
<td>Name</td>
<td>IC Number</td>
<td>Passport Number</td>
<td>Email</td>
<td>Phone</td>
</tr>
</thead>
<tbody>
<?php
if(!empty($datatable)){
foreach ($datatable as $dataitem):
$id = $dataitem->id;
?>
<tr>
<td class="name"><?php echo $dataitem->firstname." "; ?><?php echo $dataitem->lastname; ?></td>
<td class="icnumber"><?php echo $dataitem->icNumber; ?></td>
<td class="passportnumber"><?php echo $dataitem->passportNumber; ?></td>
<td class="email"><?php echo $dataitem->email; ?></td>
<td class="phone"><?php echo $dataitem->phone; ?></td>
<td><a class="edituser" href="<?php echo base_url();?>Control/UserDetail/<?php echo $id; ?>/<?php echo "true"; ?>"><i class="fa fa-pencil-square-o" aria-hidden="true"></i>Edit</a></td>
<td><a class="deleteuser" href="<?php echo base_url();?>Control/Users/Users/deleteUser/<?php echo $id; ?>"><i class="fa fa-trash" aria-hidden="true"></i>Delete</a></td>
</tr>
<?php
endforeach;
}
?>
</tbody>
</table>
<p><?php echo $this->session->flashdata('Table'); ?></p>
<?php echo $links; ?>
<p><?php echo $this->session->flashdata('Form'); ?></p>
</div>
Model
public function searchUser($userdetail){
$this->db->select('*');
$this->db->from('Users');
$this->db->like('firstname',$userdetail);
$this->db->or_like('lastname',$userdetail);
$this->db->or_like('email',$userdetail);
$this->db->or_like('phone',$userdetail);
$this->db->or_like('icNumber',$userdetail);
$this->db->or_like('passportNumber',$userdetail);
$query = $this->db->get();
if($query->num_rows()>0){
return $query->result();
}else{
return $query->result();
}
}
public function countSearchUser($userdetail){
$this->db->select('*');
$this->db->from('Users');
$this->db->like('firstname',$userdetail);
$this->db->or_like('lastname',$userdetail);
$this->db->or_like('email',$userdetail);
$this->db->or_like('phone',$userdetail);
$this->db->or_like('icNumber',$userdetail);
$this->db->or_like('passportNumber',$userdetail);
$query = $this->db->count_all_results();
if($query>0){
return $query;
}else{
return 0;
}
}
i think you should add pagination library or check your active query in model

ajax call in pagination on codeigniter

I have a pagination like this in my controller :
<?php
$nama = $this->session->userdata('nama');
$start_row = $this->uri->segment(3);
$per_page = 5;
if (trim($start_row) == '') {
$start_row = 0;
};
$total_rows = $this->model_request->countPerUser($this->session->userdata('nama'));
$this->load->library('pagination');
$config['base_url'] = base_url() . 'control_closing/show';
$config['total_rows'] = $total_rows;
$config['per_page'] = $per_page;
$config['full_tag_open'] = '<div class="pagination pagination-centered"><ul>';
$config['full_tag_close'] = '</ul></div><!--pagination-->';
$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['pagination'] = $this->pagination->create_links();
$request = $this->model_request->selectRequestPerUser($nama, $per_page, $start_row);
$data['data_request'] = $request;
$this->load->view('view_closing', $data);
?>
this is the modal :
public function selectRequestPerUser($nama_user, $limit, $start_row ) {
$query = $this->db->get_where('tbl_requestfix', array('nama_user' => $nama_user), $limit, $start_row);
return $query->result_array();
}
public function countPerUser($nama_user) {
$query = $this->db->get_where('tbl_requestfix', array('nama_user' => $nama_user));
return $query->num_rows();
}
I decided to divide my view into 2 file for better looking and easy maintenance.
This is the main view named view_closing:
<div class="container-fluid-full">
<div class="row-fluid">
<?php $this->load->view('/include/sidebar_closing.php'); ?>
<div id="content" class="span10">
<ul class="breadcrumb">
<li>
<i class="icon-home"></i>
Home
<i class="icon-angle-right"></i>
</li>
<li>Closing</li>
</ul>
<div class="row-fluid sortable" id="isi">
<div class="box span12">
<div class="box-header">
<h2><i class="halflings-icon align-justify"></i><span class="break"></span>Data Request</h2>
<div class="box-icon">
<i class="halflings-icon chevron-up"></i>
</div>
</div>
<div class="box-content">
**<?php $this->load->view('view_closing_table'); ?>**
</div>
</div>
</div>
</div>
</div>
for table that linked with pagination named view_closing_table
<table class="table table-bordered table-striped table-condensed" id="table1">
<thead>
<tr>
<th>No. </th>
<th>No Request</th>
<th>Kirim Request</th>
<th>Keluhan</th>
<th>Status</th>
<th>Approved by</th>
<th>IT Handle</th>
<th>Estimasi Penyelesaian</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
foreach ($data_request as $data) {
?>
<tr>
<td class="center"><?php echo $no++ . ". "; ?> </td>
<td class="sorting1" id='no_request' data-id-reseh="<?php echo $data['id_request']; ?>"><?php echo $data['kode_kantor'] . '/' . $data['kode_departement'] . '/' . date('m', strtotime($data['bulan'])) . '/' . $data['id_request']; ?></td>
<td class="center"><?php echo date("d-m-Y, H:i ", strtotime($data['waktu_mulai'])); ?></td>
<td class="center" id="description"><?php echo $data['keluhan']; ?></td>
<td class="center"><a href="#" id="status" name="status" class="linkStatus" ><span class="label label-success" ><?php echo $data['status_request']; ?> </span></a></td>
<td class="center"><?php echo $data['by_who'] ?></td>
<td class="center"><?php echo $data['it_person'] ?></td>
<td class="center"><?php
if ($data['tanggal_estimasi'] != NULL) {
echo date("d-m-Y ", strtotime($data['tanggal_estimasi'])) . ', ' . date("H:i ", strtotime($data['jam_estimasi']));
} else {
echo "";
}
?></td>
<!-- Action-action -->
<td class="center" width="10px">
<a class="btn btn-success" >
<i class="halflings-icon white print" id="print"></i>
Print
</a>
</td>
</tr>
<?php } ?>
</tbody>
My problem is how to make the pagination when call the next or previous just refreshing the table not all the page. I think jquery ajax can do it. This is my code but unfortunatelly is not working.
$('.pagination ul li a').click(function(){
var this_url = $(this).attr("href");
$.post(this_url,{ }, function(data){
('div#table1').html(data);
});
return false;
});
Any help it so appriciated.
('div#table1').html(data); should be $('#table1').html(data);
Now it depends on what comes back in data.

Categories