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.
Related
Hi guys i am trying to update the status column of my table contentdetails To 0.I don't know what is the exact problem and where i have done mistake i couldn't able to update my status column.
Here is my controller:
function deleteImage() {
// Pass the $id to the contentDelete() method
$id = $this->input->post('image_id');
if ($id) {
$this->Content_model->imageDelete($id);
$this->session->set_flashdata('success', 'Image deleted.');
redirect('Content');
} else {
$this->session->set_flashdata('error', 'Image Id not captured.');
redirect('Content');
}
}
Here is my model:
function imageDelete($id) {
$this->db->set('status', 0); //value that used to update column
$this->db->where('id', $id); //which row want to upgrade
$this->db->update('contentdetails'); //table name
}
Here is my view:
<table class="table table-striped table-bordered" id="dataTable1">
<thead>
<tr>
<th>#</th>
<th>Image path</th>
<th class='text-center'>Delete</th>
</tr>
</thead>
<tbody>
<?php
foreach ($bgimglist as $bgimgs) {
?>
<tr>
<td><?php echo $bgimgs->id; ?></td>
<td><?php echo $bgimgs->details; ?></td>
<td class='text-center'>
<form method="post" action="<?php echo base_url('Content/deleteImage'); ?>">
<input type="text" name="image_id" value="<?php echo $bgimgs->id; ?>">
<button type="submit" onclick="return ConfirmDelete()" class="btn btn-danger btn-xs confirmation" name="login"><i class='fas fa-times'></i></button>
</form>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
Can anyone help me where i have done mistake.
Thanks in advance.
Due to some misunderstanding OP though form_open() also closed the form in code available in the discussion in the above comments. Thus, any further form calls were going to the form that was never closed.
Adding a </form> for the open form solved the issue.
How can I use Distinct with join in CodeIgniter, I'm trying to fetch customer's name and registered category, but my problem is whiles fetching the data I'm getting data in repetation because of sub-category, I've used distinct but then I'm getting Ambiguous field error, how can I solve this problem.
My View
<table class="table table-hover example1">
<thead>
<tr>
<th>SNo.</th>
<th>Business Name</th>
<th>Category Name</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php $i=0;
foreach($value as $row){ ?>
<tr>
<td><?php echo ++$i;?></td>
<td><?php echo $row->Bussiness_Name;?></td>
<td><?php echo $row->Category_Name;?></td>
<td onClick="RestoreVendor(<?php echo $row->Business_Id; ?>,<?php echo $row->Trash;?>,'<?php echo $row->Bussiness_Name;?>')" class="btn btn-primary">Remove</td>
</tr>
<?php }?>
</tbody>
</table>
My Controller:
public function removecategory()
{
$this->load->model('VendorModel');
$data = $this->VendorModel->remove_category();
$this->load->view('admin/remove_cat', array('value'=>$data));
}
My Model
public function remove_category()
{
$this->db->select('*');
$this->db->distinct('Category_Id');
$this->db->from('business_mapping');
$this->db->join('business_profile_details', 'business_profile_details.Business_Id = business_mapping.Business_Id');
$this->db->join('category_master', 'category_master.Category_Id = business_mapping.Category_Id');
$query = $this->db->get();
return $query->result();
}
result image
You can use below mentioned query.
$query = $this->db->group_by('category_master.Category_Id,business_profile_details.Business_Id');
Please try above, It will help you.
you can use
$this->db->distinct();
Some times distinct not works then you can use
$this->db->group_by('business_mapping.unique_coloumn_Name');
This is my HTML view:
<tr class="header">
<th>CLASS</th>
<th>NAME</th>
<th>Actions</th>
</tr>
<?php
foreach($this->documents as $row): ?>
<tr>
<td><?php echo $row['CLASS'];?></td>
<td><?php echo $row['NAME'];?></td>
href="/admin/documents/delete?ID=<?php echo $row['ID'];?>">
delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
This is my view Form:
public function init()
{
$CLASS = new Zend_Form_Element_Text('CLASS');
$CLASS->setLabel('CLASS')
->setRequired(true);
$NAME = new Zend_Form_Element_Text('NAME');
$NAME->setLabel('NAME')
->setRequired(true);
$submit = new Zend_Form_Element_Submit('Submit');
$submit->setAttrib('ID', 'submitbutton');
$this->addElements(array($CLASS, $NAME,$submit));
}
I have zend form, having two inputs CLASS and NAME.
How to display CLASS and required input text box column in the same line?
This is my output image:
And same thing, how to put submit and Reset button in the same line?
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>
    <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
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.