CodeIgniter Pagination : Show Records from SQL Server - php

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>

Related

CodeIgniter - How do I do Pagination?

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

Table content does not showing correctly

I have an issue to show a content that not showing correctly:
my NAME is showing over and over again
my IKLAN (image) too
but my SIZE is not showing over and over again
so funny
like this the eror
This is my View:
data.php
<thead>
<tr>
<?php $i = 1;?>
<th>Nomer</th>
<th>Nama</th>
<th>Iklan</th>
<th>Ukuran</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$no = $this->uri->segment('3') + 1;
foreach ($list as $l) {?>
<td align="center"><h4><?php echo $i++; ?></h4></td>
<td align="center"><h4><span style="font-weight:bold"><?php echo $l->nama_client?></span></h4></td>
<td align="center"><h4><img src="./upload_iklan/<?php echo $l->iklan;?>" id="gambar_nodin" width="200"/></h4></td>
<td align="center"><h3><?php echo $l->size_ads?></h3></td>
this is my controller : Iklan.php
public function __construct()
{
parent::__construct();
$this->load->model('iklan_model');
$this->load->helper(array('form','url'));
$this->load->library('form_validation','upload','pagination');
$this->load->helper('url');
}
public function dashboard()
{
$data['konten'] = "dashboard";
$this->load->view('index', $data);
}
public function index($offset=0)
{
$this->load->database();
$data['list']=$this->iklan_model->show_iklan();
json_encode($data);
$this->load->library('pagination');
$config['base_url'] = base_url();
$config['total_rows'] = $data;
$config['per_page'] = 10;
$from = $this->uri->segment(3);
$this->pagination->initialize($config);
$data['list'] = $this->iklan_model->data($config['per_page'],$from);
$this->load->view('data',$data);
}
this is my model:
Iklan_model.php
public function __construct()
{
parent::__construct();
}
function data($number,$offset){
return $query = $this->db->get('client,size,advertisement',$number,$offset)->result();
}
function jumlah_data(){
return $this->db->get('data')->num_rows();
}
Loop must be start before tag so it will print each row with proper data.
<?php
$no = $this->uri->segment('3') + 1;
foreach ($list as $l) {?>
<tr>
<td align="center"><h4><?php echo $i++; ?></h4></td>
<td align="center"><h4><span style="font-weight:bold"><?php echo $l->nama_client?></span></h4></td>
<td align="center"><h4><img src="./upload_iklan/<?php echo $l->iklan;?>" id="gambar_nodin" width="200"/></h4></td>
<td align="center"><h3><?php echo $l->size_ads?></h3></td>
</tr>
<?php } ?>

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 :)

How to retrieve records that will always start and end at specific day per-page in codeigniter

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.

Categories