I had met some problems when doing the pagination in Codeigniter.
It can show the pagination's link below the table
But it cannot show 5 rows per page in each pages, it shown the total rows that retrieve from database in each pages.
For example, what I want is I had total 15 rows data, each pages show 5 rows data.
Hope someone can give me some suggestions. Thank You.
Model
public function record_count() {
return $this->db->count_all("order");
}
public function fetch_order($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("order");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
Controller & view
$branchCode = "";
if(!$this->session->userdata('branchcode')){
$branchCode ="ABC";
//if branchCode == ABC, show all orders from database
$output = '';
$pgcode = '';
$this->load->model('order');
if($this->input->post('code')){
$code = $this->input->post('code');
}
$config = array();
$config["base_url"] = base_url() . "index.php/Ordering/OrderListIndex/";
$config["total_rows"] = $this->order->record_count();
$config["per_page"] = 5;
$config["num_links"] = 10;
$config["uri_segment"] = 3;
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$links["results"] = $this->order->fetch_order($config["per_page"], $page);
$links["links"] = $this->pagination->create_links();
$data = $this->order->SearchCusOrder($code);
//var_dump($data);
$output .= '
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Order ID</th>
<th width="250px">Name</th>
<th>Date</th>
<th>Pay Option</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
';
if($data->num_rows() > 0){
foreach($data->result() as $row){
if($row->status == 'Placed'){
$output.= '
<tbody>
<tr>
<td>'.$row->id.'</td>
<td>'.$row->cust_name.'</td>
<td>'.$row->order_date.'</td>
<td>'.$row->options.'</td>
<td>'.$row->status.'</td>
<td>
<a class="btn btn-warning" href="EditOrderPage/'.$row->id.'">Edit</a>
<a class="btn btn-success" href="GenerateInvoice/'.$row->id.'">Print Invoice</a>
</td>
</tr>
';
}
}
}
$output .= '
</table>
<ul class="pagination">
<li class="page-item">'.$links["links"].'</li>
</ul>
';
echo $output;
}
You have to add HTML to below created output code and pass the total per pages value.
Codeigniter by default not showing how many rows are display on per page like data tables.
You have to add custom HTML.
Like <span>Rows per pages 5 </span>
Related
My pagination isn't working correctly. I get the page numbers but each page only has one row when it should have 10 rows. I new to php and am adapting code that I found on this site. When I load the page I can tell by the id number that the result showing is what I would expect the 10th row in the query to be...rows 1-9 are missing.
<?php
//Function to return rows for each page
function getPage($stmt, $pageNum, $rowsPerPage)
{
$offset = ($pageNum - 1) * $rowsPerPage;
$rows = array();
$i = 0;
while(($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC,SQLSRV_SCROLL_ABSOLUTE,$offset + $i)) && $i < $rowsPerPage)
{
array_push($rows, $row);
$i++;
}
return $rows;
}
?>
<?php
// Set the number of rows to be returned on a page.
$rowsPerPage = 10;
$usr = $_SESSION['user'];
if ($_SESSION['admin']="YES") {
$query = "SELECT iID,FirstName,LastName, convert(varchar, SubmitDate, 101) as SubDate,LEFT(ImproveIdea,30) as MyIdea,Status FROM Idea where Status='Pending' ORDER BY iID desc";
}else {
$query = "SELECT iID,FirstName,LastName,convert(varchar, SubmitDate, 101) as SubDate,LEFT(ImproveIdea,30) as MyIdea,Status FROM Idea where SubmitBy='".$usr."' ORDER BY iID desc";
}
$stmt = sqlsrv_query($connect,$query, array(), array( "Scrollable" => 'static' ));
if ( !$stmt )
die( print_r( sqlsrv_errors(), true));
?>
<table class="table table-striped table-bordered">
<thead class="thead-light">
<tr>
<th width="54%" scope="col">Submitted By</th>
<th width="14%" scope="col">Date Submitted</th>
<th width="13%" scope="col">Status</th>
<th width="19%" scope="col">Actions</th>
</tr>
</thead>
<tbody>
<?php
$pageNum = isset($_GET['pageNum']) ? $_GET['pageNum'] : 1;
$page = getPage($stmt, $pageNum, $rowsPerPage);
foreach($page as $row)
$ideanum = $row[0];
$fname = $row[1];
$lname = $row[2];
$submitdate = $row[3];
$idea = $row[4];
$status = $row[5];
echo '<tr>';
echo '<td>'.$ideanum.' '.$fname.' '.$lname.' - '.$idea.'...</td>';
echo '<td>'.$submitdate.'</td>';
echo '<td>'.$status.'</td>';
echo '<td><button type="button" class="btn btn-success"><i class="la la-eye"></i></button><button type="button" class="btn btn-info"><i class="la la-edit"></i></button></td>';
echo "</tr>";
?>
<?php
// Get the total number of rows returned by the query.
// Display links to "pages" of rows.
$rowsReturned = sqlsrv_num_rows($stmt);
if($rowsReturned === false)
die( print_r( sqlsrv_errors(), true));
elseif($rowsReturned == 0)
{
echo "No rows returned.";
exit();
}
else
{
// Display page links.
$numOfPages = ceil($rowsReturned/$rowsPerPage);
for($i = 1; $i<=$numOfPages; $i++)
{
$pageLink = "?pageNum=$i";
print("<a href=$pageLink>$i</a> ");
}
echo "<br/><br/>";
}
?>
</tbody>
</table>
SOLVED: The issue was that I was missing the brackets in my foreach statement as a redditor pointed out to me.
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'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?
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 :)
I have a function retrieving data from the db and set pagination to display 7 records per page. At the moment the retrieved data can start at any day but what I want to do is that the retrieved data per page must always start from Thur and end on Friday
Below is my controller function
public function index()
{
//Count all records
$shopID= $this->uri->segment(2);
$this->db->where('BDP_COST_CENTRE_NUMBER', $shopID);
$this->data['shops_performances'] = $this->performance_m->get_data();
$count = (count($this->data['shops_performances']));
//Set up pagination
$perpage = 7;
if ($count > $perpage)
{
$config['base_url'] = 'http://bwr/index.php/daily_shop_performance/' . $shopID . '/';
$config['total_rows'] = $count;
$config['per_page'] = $perpage;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$this->data['pagination']=$this->pagination->create_links();
$offset = $this->uri->segment(3);
} else {
$this->data['pagination']='';
$offset = 0;
}
$shopID= $this->uri->segment(2);
$this->db->where('BDP_COST_CENTRE_NUMBER', $shopID);
$this->db->limit($perpage, $offset);
$this->data['shops_performances'] = $this->performance_m->get_data();
$this->data['subview'] = 'page/performance_page';
$this->load->view('_layout_main', $this->data);
}
The function get_data in the model is below
public function get_data($id)
{
$this->db->select('*');
$this->db->from('WHOUSE1.DLY_BWR_MAN_PERFORMANCE');
$this->db->order_by('BDP_DATE DESC');
$this->db->join('WHOUSE1.DATE_DIM', 'WHOUSE1.DATE_DIM.DATE_KEY = WHOUSE1.DLY_BWR_MAN_PERFORMANCE.BDP_DATE');
$query = $this->db->get()->result_array();
return $query;
}
My view is below
<table class="table table-striped">
<thead>
<tr>
<th>Week Day</th>
<th>Date</th>
<th>Takings</th>
<th>Payout</th>
<th>Actual Slippage</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php if(count($shops_performances)): foreach($shops_performances as $shops_performance): ?>
<tr>
<td><?= $shops_performance['DAY_OF_WEEK_DDD']; ?></td>
<td><?= $shops_performance['BDP_DATE']; ?></td>
<td><?= $shops_performance['BDP_TAKE']; ?></td>
<td><?= $shops_performance['BDP_PAYOUT']; ?></td>
<td><?= $shops_performance['BDP_ACTUAL_SLIPPAGE']?></td>
<td><?php echo btn_edit('daily_shop_performance/edit/' . $shops_performance['BDP_ID']); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="5">We could not find any record.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
I will like to sort the week day column
Any idea will be well appreciated. Thank you.