codeigniter- 3 pagination loading all data on all page - mysql

my models
function get_all_events($limit = null, $offset = null) {
if ($limit) {
$this->db->limit($limit, $offset);
}
$this->db->order_by('events.added_on', 'desc');
return $this->db->get('events');
}
my controller
public function manage_events() {
$this->securepage();
$data['events'] = $this->events_model->get_all_events();
$this->load->library('pagination');
$config['base_url'] = site_url('events/manage-events');
$config['total_rows'] = $this->events_model->get_all_events()->num_rows();
$config['per_page'] = 2;
$config['uri_segment'] = 3;
$this->pagination->initialize($config);
$data = array(
'news_title' => 'Manage events | kanchan news.com' ,
'Keywords' => 'manage',
'url' =>'' ,
'content'=> $this->load->view('events/manage', $data, true)
);
$this->load->view('kanchan', $data);
}
My view
<div class="container">
<ol class="breadcrumb">
<li class="active"><?php echo $this->session->userdata('full_name');?></li>
<li class="active">Manage Events</li>
</ol>
<h1>Manage Events here.</h1>
<?php echo $this->session->flashdata('msg'); ?>
<div>
<?php if ($events->num_rows()): ?>
<table class="table table-condensed">
<thead>
<tr>
<th>SN</th>
<th>Add Title</th>
<th>expire date</th>
<th>Added on</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($events->result() as $u): ?>
<tr>
<td><?php echo $u->id; ?></td>
<td><?php echo $u->heading = word_limiter($u->heading,10); ?></td>
<td><?php echo $u->venue; ?></td>
<td><?php echo date('Y-m-d | h:i:a', $u->added_on); ?></td>
<td>
<span class="glyphicon glyphicon-trash"></span>
&nbsp &nbsp <span class="glyphicon glyphicon-edit"></span>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>No any Events
<hr>
Add New Events
</p>
<?php endif; ?>
</div>
<?php echo $this->pagination->create_links();?>
</div>
It creates pagination <1 2 but it loads all result on page 1 and also same result on page 2 and I had tried many ideas but it does not work.
I'm using codeigniter 3 and mysql .

It happen because you are getting all records at every page.You did not pass $limit and $offset. at your function.
$data['events'] = $this->events_model->get_all_events();//getting all records every time.
Try to write your manage_event function like this way
public function manage_events($page_num=1)
{
$this->securepage();
$data['events'] = $this->events_model->get_all_events(2,($page_num-1)*2);
///keep your rest code here
//remember this 2 is from your config because you wanted view 2 record per page.You can replace it with your varible.
Note
$config['base_url'] = site_url('events/manage-events');
This should be
$config['base_url'] = site_url('events/manage_events');//underscore not hypen

Related

Why json data is showing in header in codeigniter HMVC - AJAX

I'm new in ajax and I don't know how to get over this error. I don't understand this error. Please help.
Here is my Modal User_m.
I fetch data from database successfully
<?php
class Users_m extends CI_Model
{
public function showAllAdmins()
{
$query = $this->db->get('admin_users');
if($query->num_rows() > 0){
return $query->result();
}else{
return false;
}
}
}
?>
Here is my Controller AdminUsers
I get all data from database through my modal and encode it using by json_encode
but I don't know why this json data is showing on my page header.In this Image
class AdminUsers extends MY_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('AdminUsers/Users_m');
}
public function index()
{
$data['content_view'] = 'AdminUsers/viw_users';
$data['page_title'] = 'Users';
$data['page_descr'] = 'all admin users';
$result = $this->Users_m->showAllAdmins();
echo json_encode($result);
$this->template->admin_template($data);
}
}
And this is my ajax code
When I Every time refresh my page it execute alert Could not get data from database. I don't know How to pass Json data in ajax. I see console log but not show any error. I load jQuery also. Any help will be appreciated. Thanks for your answers.
And when I loop through this it goes to infinity.
$(function(){
showAllAdmins();
function showAllAdmins(){
$.ajax({
type:'ajax',
url: 'http://localhost/hmvcExample/AdminUsers',
async:false,
success: function(data){
var html = '';
var i = 0;
for(i=0; i<data.length; i++){
html +='<tr>'+
'<td>Rakesh</td>'+
'<td>Kumar</td>'+
'<td>Rakeshkrishan1992#gmail.com</td>'+
'<td>Rocky</td>'+
'<td><button class="btn btn-primary">Edit</button> <button class="btn btn-danger">Delete</button></td>'+
'</tr>';
}
$('#showData').html(html);
},
error: function(){
alert('Could not load Data from Database');
}
});
}
});
View file
<div class="box">
<div class="box-header">
<h3 class="box-title">Users List</h3>
<button class="btn btn-success pull-right">Add New</button>
</div>
<!-- /.box-header -->
<div class="box-body">
<table id="example2" class="table table-bordered table-hover table-responsive">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email Address</th>
<th>User Name</th>
<th>Action</th>
</tr>
</thead>
<tbody id="showData">
</tbody>
</table>
</div>
<!-- /.box-body -->
</div>
I'm posting a sample code. Please refer.
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Slno</th>
<th>Name</th>
<th>Username</th>
<th>Email</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="user_table">
<?php if($result) { $i =1; foreach ($result as $row){ ?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row->fullname; ?></td>
<td><?php echo $row->username; ?></td>
<td><?php echo $row->email; ?></td>
<td><?php if($row->active == "1"){ ?>
<span class="label label-success"><?php echo 'Active'; ?></span>
<?php }elseif($row->active == "0"){ ?>
<span class="label label-warning"><?php echo 'Inactive'; ?></span>
<?php } ?></td>
<td>
<div class="btn-group">
<a class="btn btn-xs btn-success" href="<?php echo base_url().'admin/user/view_user?id='.$row->user_id.'&action=edit'; ?>" title="View">
<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
</a>
</div>
</td>
</tr>
<?php $i++; } } ?>
</tbody>
</table>
You have to pass the data from the controller as follows
$data['result'] = $result; //your sql query resultset
$this->template->admin_template($data);
No need to use ajax at all. Hope this can help you.
Feel free to ask if you have any queries.

Arrange data in tables?

I retrieve the data from the database and tried to implement them in the table, but now I don't know how to arrange them in the table. My view is:
<?php include('inc/header.php');?>
<div class="main">
<table>
<thead>
<tr>ID:</tr>
<th>Name:</th>
</thead>
<tbody>
<tr>
<?php foreach ($view as $row) :?>
<?php $i = 1;?>
<?php echo "<td>".$row->audio."</td>";?>
<?php echo $i++;?>
<?php endforeach;?>
</tr>
</tbody>
</table>
</div>
<?php include('inc/footer.php');?>
I just want to make the place id increase from one to the as many records and arrange them into one table.
Your foreach loop should be like this
<tbody>
<?php
$i = 1;
foreach ($view as $row)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$row->audio."</td>";
$i++;
echo "</tr>";
}
?>
</tbody>
And <thead> should be
<thead>
<tr>
<th>ID:</th>
<th>Name:</th>
</tr>
</thead>
Try This Code
<?php
$i=1;
foreach ($view as $row)
{
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row->audio;?></td>
</tr>
<?php
$i++;
} ?>
You can try this
<?php $i = 1;?>
<?php foreach ($view as $row) :?>
<tr>
<?php echo "<td>".$i++."</td>";?>
<?php echo "<td>".$row->audio."</td>";?>
</tr>
<?php endforeach;?>
Try the following code:
<?php
$i=0;//place the initial value outside the loop
foreach ($view as $row)
{
$i++;//increment the value
?>
<tr>
<td><?php echo $i;//display the value ?></td>
<td><?php echo $row->audio;?></td>
</tr>
<?php
} ?>

Split foreach code based on even/odd

I currently use a code to display some content in my Magento shop.
But now I want to split the loaded content based on even/odd into two different divs.
My current code is displayed below.
How can I split the code based on even/odd so that I get to <div class="block-specs">.
I want a div <div class="block-specs odd"> and <div class="block-specs even">
How can I achieve that?
Current code:
<?php if($_additionalgroup = $this->getAdditionalData()): ?>
<section id="additional">
<div class="box-collateral box-additional">
<h2><?php echo $this->__('Additional Information') ?></h2>
<?php $i=0; foreach ($_additionalgroup as $_additional): $i++; ?>
<div class="block-specs-<?php echo $i?>">
<h3 class="specs-<?php echo $i?>"><?php echo $this->__( $_additional['title'] )?></h3>
<table class="data-table specs-<?php echo $i?>" id="product-attribute-specs-table-<?php echo $i?>">
<col width="25%" />
<col />
<tbody>
<?php foreach ($_additional['items'] as $_data): ?>
<?php $_attribute = $_product->getResource()->getAttribute($_data['code']);
if (!is_null($_product->getData($_attribute->getAttributeCode())) && ((string)$_attribute->getFrontend()->getValue($_product) != '')) { ?>
<tr>
<th class="label"><?php echo $this->htmlEscape($this->__($_data['label'])) ?></th>
<td class="data"><?php echo $_helper->productAttribute($_product, $_data['value'], $_data['code']) ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
</tbody>
</table>
</div>
<script type="text/javascript">decorateTable('product-attribute-specs-table-<?php echo $i?>')</script>
<?php endforeach; ?>
</div>
</section>
<?php endif;?>
Check if the index is evenly divisable by 2 with '%' this returns the remainder after dividing (0 if even).
<?php foreach ($_additionalgroup as $i => $_additional):
// if evenly divisable by 2, it is even
$oddEven =($i % 2) ? 'odd':'even';
?>
<div class="block-specs-<?php echo $oddEven; ?>">

Only single return value($data) will display codeigniter View page

I have a result.php page in view/result.php
<body>
<table border="1">
<tr>
<th>name1</th>
<th>name2</th>
<th>name3</th>
</tr>
<?php foreach($result as $row): ?>
<tr>
<td><?php echo $row->name1; ?></td>
<td><?php echo $row->name2; ?></td>
<td><?php echo $row->name3; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php echo 'Your new value is '.$name1;?>
<?php echo 'Your new value is '.$name2;?>
<?php echo 'Your new value is '.$name3;?>
</body>
html table is currently working. It retrives data from my table testing and display it. The problem is echo part in below code, it doesn't work.
<?php echo 'Your new value is '.$name1;?>
<?php echo 'Your new value is '.$name2;?>
<?php echo 'Your new value is '.$name3;?>
From the same table testing. It shows an error in the error page it shows Undefined variable name1,name2,name3
controller/example.php
<?php
class Example extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
}
function index() {
$this->load->view('example_view');
$this->getvalue();
$this->edit_content();
}
function getvalue()
{
if ($this->input->post('submit')==true) {
$data1['name1']=$this->input->post('name1');
$data1['name2']=$this->input->post('name2');
$data1['name3']=$this->input->post('name3');
$this->load->view('result',$data1);
$this->load->model('Insert_model');
$this->Insert_model->uploaddata($data);
}
}
function edit_content()
{
$data = array();
$this->load->model('Selected_model');
$this->load->helper('url');
$data['result'] = $this->Selected_model->get_contents();
$this->load->view('result',$data);
}
}
?>
<body>
<table border="1">
<tr>
<th>name1</th>
<th>name2</th>
<th>name3</th>
</tr>
<?php if(is_array($result)){ foreach($result as $row): ?>
<tr>
<td><?php echo $row->name1; ?></td>
<td><?php echo $row->name2; ?></td>
<td><?php echo $row->name3; ?></td>
</tr>
<?php endforeach; } ?>
</table>
<?php echo 'Your new value is '.$row->name1;?>
<?php echo 'Your new value is '.$row->name2;?>
<?php echo 'Your new value is '.$row->name3;?>
If your array is like in the following format.
$info = array
(
[0] => Array
(
[name1] => a
[name2] => b
[name3] => c
)
[1] => Array
(
[name1] => aa
[name2] => bb
[name3] => cc
)
[2] => Array
(
[name1] => aaa
[name2] => bbb
[name3] => ccc
)
[3] => Array
(
[name1] => aaaa
[name2] => bbbb
[name3] => cccc
)
);
Then
<body>
<table border="1">
<tr>
<th>name1</th>
<th>name2</th>
<th>name3</th>
</tr>
<?php foreach($info as $row): ?>
<tr>
<td><?php echo $row->name1; ?></td>
<td><?php echo $row->name2; ?></td>
<td><?php echo $row->name3; ?></td>
</tr>
<?php endforeach; ?>
</table>
The above loop will give you accurate result like.
name1 name2 name3
a b c
aa bb cc
aaa bbb ccc
aaaa bbbb cccc
Now the following code will generate an error because it does not know the following
$row->name1
$row->name1
$row->name1
The reason is that these are defined in the scope of foreach loop thats why its generate error.

How to display Serial Numbers with mysql query result?

I have a script that displays information of these fields- batchname, class, batchinstructor from the table "batch". But I want to display dynamically generated serial number on the left side when I show the data. For example:
Serial Number BatchName Class Batch Instructor
1. Solar Class Five John
2. Lunar Class six Bon Jovi
I have tried a lot but its not working. Would you please kindly help me to solve this? Please note that these serial number are not from database.
Here's my Controller:
<?php
class Batchlist extends CI_Controller{
function index(){
$this->load->library('pagination');
$config['base_url'] = base_url().'batchlist/index';
$config['total_rows'] = $this->db->get('batch')->num_rows();
$config['per_page'] = 20;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination" align="center">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$this->load->model('mod_batchlist');
$data['records']= $this->mod_batchlist->batch_list($config['per_page'],$this->uri->segment(3));
$data['main_content']='view_batchlist';
$this->load->view('includes/template',$data);
}
}
?>
Here's my Model:
function batch_list($perPage,$uri) {
$this->db->select('*');
$this->db->from('batch');
$this->db->join('teacher', 'batch.batchinstructor = teacher.teacherid');
$this->db->order_by('batchid','DESC');
$getData = $this->db->get('', $perPage, $uri);
if($getData->num_rows() > 0)
return $getData->result_array();
else
return null;
}
Here's my View
<h1>Batch List </h1>
<?php if(count($records) > 0) { ?>
<table id="table1" class="gtable sortable">
<thead>
<tr>
<th>Batch Name</th>
<th>Class</th>
<th>Batch Instructor</th>
<th>Edit/Delete</th>
</tr>
</thead>
<tbody>
<?php foreach ($records as $row){ ?>
<tr>
<td><?php echo $row['batchname'];?></td>
<td><?php echo $row['class'];?></td>
<td><?php echo $row['teachername'];?></td>
<td> <img src="<?php echo base_url(); ?>support/images/icons/edit.png" alt="Edit" />
<img src="<?php echo base_url(); ?>support/images/icons/cross.png" alt="Delete" />
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
<div class="tablefooter clearfix">
<div class="pagination">
<?php echo $this->pagination->create_links(); ?>
</div>
</div>
Maybe I misunderstood, but...what about a simple counter?? (after you ordered your records in SQL).
You need to pass to the view the number of items per page (in this snippet: $per_page), then you retrieve the current page from the URI ($this->uri->segment(n)).
At page 1, counter starts from (20*0)+1, i.e. 1. At page 2, starts from (1*20)+1 ie 21, at page 3 from (2*20)+1 ie 41 and so on...
<?php
$cur_page = $this->uri->segment(n) ? intval($this->uri->segment(n)) : 1;
$i = (($cur_page-1) * $per_page) +1;
foreach ($records as $row) :
?>
<tr>
<td><?php echo $i;?>.</td>
<td><?php echo $row['batchname'];?></td>
<td><?php echo $row['class'];?></td>
<td><?php echo $row['teachername'];?></td>
<td> <img src="<?php echo base_url(); ?>support/images/icons/edit.png" alt="Edit" /><img src="<?php echo base_url();?>support/images/icons/cross.png" alt="Delete" />
</td>
</tr>
<?php
++$i;
endforeach;
?>
You can place the counter wherever you want, I added a column for simplicity but if you want it somewhere else just place the counter there.
You said the serial numbers are not from the database. Where are they from then? If you want the database to generate them you can create a column named id and set it as the primary key and have it auto increment.