CodeIgniter - How do I do Pagination? - php

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

Related

Codeigniter Pagination directs me to 'about:blank#blocked'

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?

CodeIgniter Pagination : Show Records from SQL Server

In codeigniter, I use sqlsrv and pagination library to show records from database,
but after i click on page 2 on pagination link, the Records will show at the end of current page instead of showing in new page, any help would be greatly appreciated,
here is the Controller:
public function example1() {
$config = array();
$config["base_url"] = base_url() . "index.php/posts/example1";
$config["total_rows"] = $this->PostModels->record_count();
$config["per_page"] = 10;
$config["uri_segment"] = 3;
$config["num_links"] = 5;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["results"] = $this->PostModels->
fetchdata($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view("example1", $data);
Here is the Model:
public function record_count() {
return $this->db->count_all("BlawBlaw");
}
public function fetchdata($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("BlawBlaw");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
& here is the View:
<table>
<tr>
<th><strong>Post Id</strong></th>
<th><strong>Post Title</strong></th>
<th><strong>Post Title</strong></th>
<th><strong>Post Title</strong></th>
</tr>
<?php foreach($results as $data){?>
<tr>
<td><?php echo $data->a;?></td>
<td><?php echo $data->b;?></td>
<td><?php echo $data->c;?></td>
<td><?php echo $data->d;?></td>
</tr>
<?php }?>
<p><?php echo $links; ?></p>
</table>
you can used this this model and view changes make in your code
Here is the Model:
function fetch_data($limit, $id)
{
$this->db->select('*')->from('category ')->limit($limit, $id);
//$this->db->limit($limit);
//$this->db->where('Cid', $id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
return false;
& here is the View:
<table>
<tr>
<th><strong>Post Id</strong></th>
<th><strong>Post Title</strong></th>
<th><strong>Post Title</strong></th>
<th><strong>Post Title</strong></th>
</tr>
<?php foreach($results as $data){?>
<tr>
<td><?php echo $data->a;?></td>
<td><?php echo $data->b;?></td>
<td><?php echo $data->c;?></td>
<td><?php echo $data->d;?></td>
</tr>
<?php }?>
<p><?php foreach ($links as $link) {
echo $link; ?></p>
</table>

javascript:void(0) on ajax pagination code igniter

I'm developing this pagination page with ajax on code igniter hmvc but I get this javascript:void(0) on the uri of my link to the next pages. how can I fix this?
here is my codes.
controllers
<?php
class Job_Titles extends MY_Controller{
public function __construct(){
parent::__construct();
$this->load->model('Job_Titles_Model');
$this->load->library('Ajax_pagination');
$this->perPage = 10;
}
// VIEW REDIRECTING /////////////////////////////////////////////////////////
public function index(){
/// view ajax config/////
$data = array();
//total row count
$totalRec = count($this->Job_Titles_Model->getRows());
//configuration
$config['first_link'] = 'First';
$config['div'] = 'postList'; //parent div tag id
$config['base_url'] = base_url().'Job_Titles/ajaxPaginationData';
$config['total_rows'] = $totalRec;
$config['per_page'] = $this->perPage;
$this->ajax_pagination->initialize($config);
$data['content_view'] = 'Job_Titles/jobtitles_read';
$data['content'] = $this->Job_Titles_Model->getRows(array('limit'=>$this->perPage));
$data['false'] = FALSE;
$this->templates->admin_template($data);
}
//// pagination
public function ajaxPaginationData(){
$data = array();
$page = $this->input->post('page');
if(!$page){
$offset = 0;
}else{
$offset = $page;
}
//total row count
$totalRec = count($this->Job_Titles_Model->getRows());
//pagination config
$config['first_link'] = 'First';
$config['base_url'] = base_url().'Job_Titles/ajaxPaginationData';
$config['total_rows'] = $totalRec;
$config['per_page'] = $this->perPage;
$this->ajax_pagination->initialize($config);
//get post data
$data['content_view'] = 'Job_Titles/ajax-pagination-data';
$data['content'] = $this->Job_Titles_Model->getRows(array('start'=>$offset,'limit'=>$this->perPage));
$this->templates->admin_template($data);
}
}
models
public function getRows($params = array()){
$this->db->select('*');
$this->db->from($this->table);
if(array_key_exists("start", $params) && array_key_exists("limit", $params)){
$this->db->limit($params['limit'],$params['start']);
}elseif (!array_key_exists("start", $params) && array_key_exists("limit", $params)) {
$this->db->limit($params['limit']);
}
$query = $this->db->get();
return ($query->num_rows() > 0)?$query->result_array():FALSE;
}
views
<?php
echo anchor('Job_Titles/add_view','ADD ');
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<div id = "container" class = "page-header">
<table class= "table table-bordered">
<thead>
<tr>
<th>JOB CODE</th>
<th>JOB NAME</th>
<th></th>
<th></th>
<tr>
</thead>
<tbody>
<?php if(!empty($content)){foreach ($content as $job_title) {?>
<tr>
<td><?php echo $job_title['JOB_CODE']; ?></td>
<td><?php echo $job_title['JOB_NAME']; ?></td>
<td></i></td>
<td><i class="fa fa-trash-o"></i></td>
</tr>
<?php }}else{ ?>
<li class="err_msg">Post(s) not available.</li>
<?php } ?>
</tbody>
</table>
<?php echo $this->ajax_pagination->create_links(); ?>
</div>
I'm developing this pagination page with ajax on code igniter hmvc but I get this javascript:void(0) on the uri of my link to the next pages. how can I fix this?

codeigniter pagination 404 not found

I am trying to use pagination with codeigniter 3 and bootstrap twitter. When i click the link pagination it's always give me 404 not found.
I think it's my fault for not fully understanding codeigniter URI that's why i need help in my code
Here is my model :
class Mcrud extends CI_Model {
public function record_count() {
return $this->db->count_all("crud");
}
function view() {
$ambil = $this->db->from('crud')->order_by('idcrud', 'DESC')->limit(5, 3)->get();
if ($ambil->num_rows() > 0) {
foreach ($ambil->result() as $data) {
$hasil[] = $data;
}
$ambil->free_result();
return $hasil;
}
}
here is my controller :
class Chome extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('Mcrud');
}
function index() {
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['nama'] = $session_data['fullname'];
$data['username'] = $session_data['username'];
$data['id'] = $session_data['idlogin'];
$this->load->view('Header', $data);
$this->suratkeluar();
} else {
redirect('welcome', 'refresh');
}
}
function suratkeluar()
{
$result_per_page = 2; // the number of result per page
$config['base_url'] = base_url() . '/Chome/index';
$config['total_rows'] = $this->Mcrud->record_count();
$config['per_page'] = $result_per_page;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = 20;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$data['data_get'] = $this->Mcrud->view();
$data['pagination'] = $this->pagination->create_links();
$this->load->view('Vhome', $data);
$this->load->view('Footer');
}
and this is my Vhome view :
<div class="table-responsive">
<table class="table table-bordered table-hover table-striped">
<caption>List Data</caption>
<thead>
<tr>
<th width="80px">ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Address</th>
<th width="80px">Action</th>
</tr>
</thead>
<tbody>
<?php
if ($data_get == NULL) {
?>
<div class="alert alert-info" role="alert">Data masih kosong, tolong di isi!</div>
<?php
} else {
foreach ($data_get as $row) {
?>
<tr>
<td><?php echo $row->idcrud; ?></td>
<td><?php echo $row->firstname; ?></td>
<td><?php echo $row->lastname; ?></td>
<td><?php echo $row->age; ?></td>
<td><?php echo $row->address; ?></td>
<td>
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
</td>
<?php
}
}
?>
</tr>
</tbody>
</table>
<?php echo $pagination; ?>
</div>
and last this is my base url in config.php :
$config['base_url'] = 'http://localhost/arsip/';
try to amend this section:
Model:
function view($opset) {
$ambil = $this->db->from('crud')->order_by('idcrud', 'DESC')->limit(5, $opset)->get();
if ($ambil->num_rows() > 0) {
foreach ($ambil->result() as $data) {
$hasil[] = $data;
}
$ambil->free_result();
return $hasil;
}
}
Controllers:
function suratkeluar($opset=NULL)
{
$result_per_page = 2; // the number of result per page
$config['base_url'] = base_url('Chome/suratkeluar');
$config['total_rows'] = $this->Mcrud->record_count();
$config['per_page'] = $result_per_page;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = 20;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$data['data_get'] = $this->Mcrud->view($opset);
$data['pagination'] = $this->pagination->create_links();
$this->load->view('Vhome', $data);
$this->load->view('Footer');
}
work for me :)

pagination not working unsolved error

hi everyone i am trying to build a pagination function in a web project i fallowed the user guide on codeigniter and a tutorial on net tuts here is the link http://net.tutsplus.com/tutorials/php/codeigniter-from-scratch-day-7-pagination/
but i keep getting these tow errors:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Loader::$pagination
Filename: views/result_view.php
Line Number: 41
and
Fatal error: Call to a member function create_links() on a non-object in C
cant figure these errors out where are they coming from and how can i fix them can some one help me here is my cmv :
controller
<?php
class Result_controller extends CI_Controller{
function getall(){
$this->load->model('result_model');
$data['query'] = $this->result_model->result_getall();
// print_r($data['query']); die();
$this->load->view('result_view', $data);
}
function pagination()
{
$this->load->library('pagination');
$config['base_url'] = 'result_controller/pagination';
$config['total_rows'] = $this->db->get('data')->num_rows();
$config['per_page'] = 5;
$config['num_links'] = 3;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data['records'] = $this->db->get('data', $config['per_page'], $this->uri->segment(3));
$this->load->view('result_view', $data);
}
view
<table border="1">
<tr>
<th>Name</th>
<th>Second Name</th>
<th>Phone</th>
<th>Email</th>
<th>Answer</th>
<th>Comment</th>
</tr>
<?php foreach ($query as $row): ?>
<tr>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->second_name; ?></td>
<td><?php echo $row->phone; ?></td>
<td><?php echo $row->email; ?></td>
<td> <?php echo $row->answerA;?>
<?php echo $row->answerB;?>
<?php echo $row->answerC;?></td>
<td><?php echo $row->comment;?><br></td>
</tr>
<?php endforeach; ?>
</table>
<?php echo $this->pagination->create_links(); ?>
model
<?php
class Result_model extends CI_Model{
function result_getall(){
return $this->db->select('*')
->from('tblanswers, credentials')
->get()
->result_object();
}
}
?>
Remove this line from your view
<?php echo $this->pagination->create_links(); ?>
And paste this one
<?php echo $pagination ?>
EDIT
Instead of having a pagination function put the pagination code inside your getall() function like this:
class Result_controller extends CI_Controller{
function getall(){
$this->load->model('result_model');
$data['query'] = $this->result_model->result_getall();
// print_r($data['query']); die();
$this->load->library('pagination');
$config['base_url'] = 'result_controller/getall';
$config['total_rows'] = $this->db->get('data')->num_rows();
$config['per_page'] = 5;
$config['num_links'] = 3;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$data['pagination'] = $this->pagination->create_links();
$data['records'] = $this->db->get('data', $config['per_page'], $this->uri->segment(3));
$this->load->view('result_view', $data);
}
}

Categories