Codeigniter Pagination doesnt work, the URL changes to 'about:blank#blocked'.
I'm fairly new at php frameworks and codeigniter. I tried to read the documentation but I don't seem to see the problem.
Controller:
class Listing extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
//Get Total Records Count
$this->db->select("*");
$this->db->from("cities");
if (!empty($_GET['cityFilter'])) {
$this->db->like('city_name', $_GET['cityFilter']);
}
$cityRecordsCount = $this->db->get();
$totalRecords = $cityRecordsCount->num_rows();
$limit = 10;
if (!empty($_GET['cityFilter'])) {
$config["base_url"] = base_url('Listing/index?cityFilter=' . $_GET['cityFilter']);
} else {
$config["base_url"] = base_url('Listing/index?cityFilter=');
}
$config["total_rows"] = $totalRecords;
$config["per_page"] = $limit;
$config['use_page_numbers'] = TRUE;
$config['page_query_string'] = TRUE;
$config['enable_query_strings'] = TRUE;
$config['num_links'] = 2;
$config['cur_tag_open'] = ' <li class="active"><a>';
$config['cur_tag_close'] = '</a></li>';
$config['next_link'] = 'Next';
$config['prev_link'] = 'Previous';
$this->pagination->initialize($config);
$str_links = $this->pagination->create_links();
$links = explode(' ', $str_links);
$offset = 0;
if (!empty($_GET['per_page'])) {
$pageNo = $_GET['per_page'];
$offset = ($pageNo - 1) * $limit;
}
//Get actual result from all records with pagination
$this->db->select("*");
$this->db->from("cities");
if (!empty($_GET['cityFilter'])) {
$this->db->like('city_name', $_GET['cityFilter']);
}
$this->db->limit($limit, $offset);
$cityRecords = $this->db->get();
$this->load->view('listCities', array(
'totalResult' => $totalRecords,
'results' => $cityRecords->result(),
'links' => $links
));
}
}
HTML
<div id="container">
<div class="row">
<div class="col-md-8 col-md-offset-2 col-lg-8 col-lg-offset-2 col-sm-12">
<h1>Pagination With Search.</h1>
<p>A demo for Codeigniter 2.X framework</p>
<br>
<form action="" method="GET">
<div class="input-group pull-right">
<input type="text" class="form-control" placeholder="Search For City"
name="cityFilter" value="<?php
if (!empty($_GET['cityFilter'])) {
echo $_GET['cityFilter'];
}
?>">
<span class="input-group-btn">
<button type="submit" class="btn btn-success"><i class="fa fa-search"></i> Search</button>
</span>
</div>
</form>
<table class="table table-bordered table-hover" border="1">
<thead>
<tr>
<th>#</th>
<th>City Name</th>
<th>City State</th>
</tr>
</thead>
<tbody>
<?php
foreach ($results as $o) {
?>
<tr>
<td><?php echo $o->city_id; ?></td>
<td><?php echo $o->city_name; ?></td>
<td><?php echo $o->city_state; ?></td>
</tr>
<?php }
?>
</tbody>
<tfoot>
</tfoot>
</table>
<ul class="pagination pull-right">
<!-- Show pagination links -->
<?php
foreach ($links as $link) {
echo "<li>" . $link . "</li>";
}
?>
</ul>
</div>
</div>
</div>
It shows a blank page and the URL changes to 'about:blank#blocked'. What am I doing wrong?
Related
I've look through lots of question asked about Pagination but I can't really understand how Pagination works. I need Pagination to work on index() and when user enter date range searchdate().In my Controller:
public function __construct() {
parent::__construct();
$this->load->model('ReportModel');
}
public function index()
{
$orders=new ReportModel;
$data['data']=$orders->get_orders();
$this->load->view('includes/header');
$this->load->view('Report/view',$data);
$this->load->view('includes/footer');
}
public function searchDate()
{
$orders=new ReportModel;
$searchfrom = $this->input->post('searchDateFrom');
$searchto = $this->input->post('searchDateTo');
$data['data']=$orders->get_orders($searchfrom,$searchto);
$this->load->view('includes/header');
$this->load->view('Report/view',$data);
$this->load->view('includes/footer');
}
In my Model:
public function get_orders(){
$searchDateFrom = $this->input->post("searchDateFrom");
$searchDateTo = $this->input->post("searchDateTo");
$this->db->select('platform,id,no,date,printed_date');
$this->db->from('orders');
if(!empty($searchDateFrom) || !empty($searchDateTo) || !empty($searchPlatform)){
if (!empty($searchDateFrom)) {
$this->db->where('date >= ', $this->input->post("searchDateFrom"));
}
if (!empty($searchDateTo)) {
$this->db->where('date <= ', $this->input->post("searchDateTo")." 23:59:59");
}
}
$this->db->order_by("date", "desc");
$this->db->limit(300);
$query = $this->db->get();
return $query->result();
}
In my View:
<div class="pull-right">
<form class="form-inline" role="search" action="<?php echo base_url('estoreReport/searchDate')?>" method = "post">
<div class="form-group">
<input type="date" class="form-control" placeholder="Order Date From" name = "searchDateFrom" ">
<input type="date" class="form-control" placeholder="Order Date To" name = "searchDateTo" ">
</div>
<button class="btn btn-default " type="submit" value = "searchDateTo"><i class="glyphicon glyphicon-search"></i>
</form>
</div>
</div>
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr>
<th>Platform</th>
<th>ID</th>
<th>No</th>
<th>Date</th>
<th>Printed Date</th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $d) { ?>
<tr>
<td ><?php echo $d->platform; ?></td>
<td><?php echo $d->id; ?></td>
<td><?php echo $d->no; ?></td>
<td><?php echo $d->date; ?></td>
<td><?php echo $d->printed_date; ?></td>
<td></td>
<td></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
I've read CodeIgniter documentation, and know that I should put the configurations in the controller.
public function pagination($count){
$this->load->library('pagination');
$config['base_url'] = base_url('/order/Report/');
$config['total_rows'] = $count;
$config['per_page'] = 100;
$config["uri_segment"] = 3;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = ;
$config['use_page_numbers'] = TRUE;
$config['reuse_query_string'] = TRUE;
$page = ($this->uri->segment($config["uri_segment"] )) ? $this->uri->segment($config["uri_segment"] ) : 0;
$this->pagination->initialize($config);
$pagination = $this->pagination->create_links();
return array($page, $config['per_page'], $pagination);
}
But I'm still not sure how to do modify other parts of my controller, model and view. I'm a new CodeIgniter learner here, this is my testing page only, please help, thank you.
Your controller should look like this:
public function __construct() {
parent::__construct();
$this->load->model('ReportModel', 'orders');
$this->load->library('pagination');
}
public function index($offset = 0)
{
$data['data']=$this->orders->get_orders($search = array(), $offset);
$config['total_rows'] = $data['total_rows'] = $this->orders->get_orders($search = array(), $offset, true);
$config['base_url'] = base_url('/order/index/');
$config['per_page'] = 100;
$config["uri_segment"] = 3;
$config['reuse_query_string'] = TRUE;
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('includes/header');
$this->load->view('Report/view',$data);
$this->load->view('includes/footer');
}
Then your model should be changed to also be able to paginate your results:
public function get_orders($search = array(), $offset = 0, $count = false){
$searchDateFrom = $search["searchDateFrom"];
$searchDateTo = $search["searchDateTo"];
$this->db->select('platform,id,no,date,printed_date');
$this->db->from('orders');
if(!empty($searchDateFrom) || !empty($searchDateTo) || !empty($searchPlatform)){
if (!empty($searchDateFrom)) {
$this->db->where('date >= ', $this->input->post("searchDateFrom"));
}
if (!empty($searchDateTo)) {
$this->db->where('date <= ', $this->input->post("searchDateTo")." 23:59:59");
}
}
$this->db->order_by("date", "desc");
if ( !$count ) {
$this->db->limit(100, $offset);
$query = $this->db->get();
return $query->result();
}
$query = $this->db->get();
return $query->num_rows();
}
Check these:
codeigniter pagination
codeigniter pagination 2
I have given answers to the question for pagination in Codeigniter on StackOverflow. These links have the full description. Hopefully it will help
I wrote this code:
<div class="container mx-auto">
<!--Add class table-responsive for responsive table -->
<div class="row">
<div class="col-md-6">
<form method="post">
<div class="input-group">
<input size='14' type="text" class="form-control" placeholder="Search for..." name="searchValue">
<span class="input-group-btn">
<button class="btn btn-secondary" name='search' type="submit"><i class="fa fa-search"></i></button>
</span>
</div>
</form>
</div>
</div>
<table class="table mx-auto" id="'table">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Phone</th>
<th>Company</th>
<th>More</th>
</tr>
</thead>
<tbody>
<?php
$pagination = new Pagination(7);
$page_max = $pagination->getPageMax();
$start = $pagination->getStart();
if(!isset($_POST['search'])) {
$numberOfPages = $pagination->numberOfPages($database->getData("SELECT count(id) FROM customers"));
$customers = $database->getDataAsArray("SELECT * FROM customers LIMIT $start, $page_max");
}
else{
$searchValue = '%'. $_POST['searchValue'] . '%';
$numberOfPages = $pagination->numberOfPages($database->getData("SELECT count(id) FROM customers WHERE name LIKE '$searchValue' "));
$customers = $database->getDataAsArray("SELECT * FROM customers WHERE name LIKE '$searchValue' LIMIT $start, $page_max");
}
foreach($customers as $customer){
$name = $customer ['name'];
$surname = $customer['surname'];
$email = $customer['email'];
$phone = $customer['phone'];
$company = $customer['company'];
$id = $customer['id'];
echo "<tr>
<td>$name</td>
<td>$surname</td>
<td>$email</td>
<td>$phone</td>
<td>$company</td>
<td><a href='customerInfo.php?id=$id' class='btn btn-sm btn-info'><i class='fa fa-info'></i></a></td>
</tr>";
}
?>
</tbody>
</table>
<ul class="pagination">
<?php
echo $pagination->previous($numberOfPages);
for($i = 0; $i < $numberOfPages; $i++){
echo '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>';
}
echo $pagination->next($numberOfPages);
?>
</ul>
</div>
The getDataAsArray function is like this:
public function getDataAsArray($myQuery){
$this->connection = mysqli_connect($this->host, $this->dbUsername, $this->dbPassword, 'portal');
$query = mysqli_query($this->connection, $myQuery);
$results = array();
while($line = mysqli_fetch_array($query)){
$results[] = $line;
}
return $results;
}
I know I should use :[name] or something to set a parameter in my query,the question is not about that and ofcourse I should use a prepared statment. Will do that later.
The question:
I wrote the code to search in the db records. I works fine but when I search it creates a new pagination with all the new records and when I click on the second page. The page builds the pagination with all the records from the database so I won't use it's searchValue anymore.
I think this issue will be solved with a $_GET parameter like search in the URL or Ajax but I don't know how.
Could anyone help me out?
I wrote this code:
<div class="container mx-auto">
<!--Add class table-responsive for responsive table -->
<div class="row">
<div class="col-md-6">
<form method="post">
<div class="input-group">
<input size='14' type="text" class="form-control" placeholder="Search for..." name="searchValue">
<span class="input-group-btn">
<button class="btn btn-secondary" name='search' type="submit"><i class="fa fa-search"></i></button>
</span>
</div>
</form>
</div>
</div>
<table class="table mx-auto" id="'table">
<thead>
<tr>
<th>Name</th>
<th>Surname</th>
<th>Email</th>
<th>Phone</th>
<th>Company</th>
<th>More</th>
</tr>
</thead>
<tbody>
<?php
$pagination = new Pagination(7);
$page_max = $pagination->getPageMax();
$start = $pagination->getStart();
if(!isset($_POST['search'])) {
$numberOfPages = $pagination->numberOfPages($database->getData("SELECT count(id) FROM customers"));
$customers = $database->getDataAsArray("SELECT * FROM customers LIMIT $start, $page_max");
}
else{
$searchValue = '%'. $_POST['searchValue'] . '%';
$numberOfPages = $pagination->numberOfPages($database->getData("SELECT count(id) FROM customers WHERE name LIKE '$searchValue' "));
$customers = $database->getDataAsArray("SELECT * FROM customers WHERE name LIKE '$searchValue' LIMIT $start, $page_max");
}
foreach($customers as $customer){
$name = $customer ['name'];
$surname = $customer['surname'];
$email = $customer['email'];
$phone = $customer['phone'];
$company = $customer['company'];
$id = $customer['id'];
echo "<tr>
<td>$name</td>
<td>$surname</td>
<td>$email</td>
<td>$phone</td>
<td>$company</td>
<td><a href='customerInfo.php?id=$id' class='btn btn-sm btn-info'><i class='fa fa-info'></i></a></td>
</tr>";
}
?>
</tbody>
</table>
<ul class="pagination">
<?php
echo $pagination->previous($numberOfPages);
for($i = 0; $i < $numberOfPages; $i++){
echo '<li class="page-item"><a class="page-link" href="?page='. $i . '">'. $i. '</a></li>';
}
echo $pagination->next($numberOfPages);
?>
</ul>
</div>
I want to search in my table, this works fine so far but when I search on A for example and I click my pagination page 1 it rebuilds the page with all te records from the database.
I tried to add a parameter to the url upon button click but upon creating this button with link the searchValue is still null.
This question is not about my sql query. It's about searching with pagination
Could anyone help me fix this?
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>
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.