Bootstrap Notification Alerts - mysql

I have a Codeigniter project to manage letter details. Then I need to notify no of urgent letters in the menu bar as a Bootstrap notification to each subject users. Urgent letters denoted by 2 (Two) in the classification column.
Controller
public function index()
{
$meta=array(
'UrgentLetters'=>$this->Welcome_Model->getUrgentLetters()
);
$this->load->view('header',$meta);
$this->load->view('dashboard');
$this->load->view('footer');
}
Model
function getUrgentLetters()
{
$subject = $this->session->userdata('subject_id');
$this->db->select("letter_subject.subject_name AS subjects, count(letter_id) AS no_of_urgent");
$this->db->from('letter_letter');
$this->db->join('letter_subject', 'letter_subject.subject_id=letter_letter.subject');
$this->db->where('letter_letter.status=1 and letter_letter.classification=2');
$this->db->group_by('subject_name');
if ($subject && $subject !=100)
$this->db->where('letter_letter.subject', $subject);
$q = $this->db->get();
if ($q->num_rows() > 0) {
return $q;
}
}
View
<li class="dropdown notifications-menu">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-bell-o"></i>
<span class="label label-info"><?= count($UrgentLetters) ?></span>
</a>
<ul class="dropdown-menu" style="width: 940px">
<li class="header">You have <?= count($UrgentLetters)?> Urgent Letters</li>
<li>
<!-- Inner Menu: contains the notifications -->
<ul class="menu">
<table style="width: 100%">
<thead style="padding-bottom: 10px">
<tr style="width: 100%">
<th style="width: 40%;padding-left: 2%;padding-bottom: 5px;padding-top: 5px">Subject Name</th>
<th style="width: 40%;padding-bottom: 5px;padding-top: 5px">Letter Count</th>
</tr>
</thead>
<tbody>
<?php foreach ($UrgentLetters as $UrgentLetterss){
$subjects=$UrgentLetterss->subjects;
$no_of_urgent = $UrgentLetterss->no_of_urgent;
echo "<tr>";
echo "<td style='width: 45%;padding-left: 2%'>$subjects</td>";
echo "<td style='width: 45%'>$no_of_urgent->desc</td>";
echo "</tr>";
}?>
</tbody>
</table>
</ul>
</li>
</ul>
</li>
All the sections are working fine. But the following error encountered.
A PHP Error was encountered
Severity: Notice
Message: Undefined property: PDO::$subjects
Filename: views/header.php
Line Number: 111
What may be the reason ? Can anyone help ?

you are missing to return a query result like
if ($q->num_rows() > 0) {
return $q->result();
}
right now you just send a CI_DB_mysqli_result Object and therefor you get the error message in your view.
see Generating Query Results()

Related

Change button text based on cell value in each table row - Jquery

I have a HTML table that is showing data from DB. A [status] column shows the status of each record as "AVAILABLE" , "BOOKED" or "ON-HOLD". the last column of table is [ACTION] column which contains a BUTTON. If the status of row = "Available" then button text should be "BOOK". Likewise if the status = "BOOKED" or "ON-HOLD" the the button text should be "VIEW". I have applied css successfully via jquery that changes the background-color of status column based on the text in the cell as mentioned above but the same code is not working for button text.
HTML
<tr id="data_row">
<td class="align-middle text-center"><?php echo $Project; ?></td>
<td class="align-middle text-center"><?php echo $Sector; ?></td>
<td class="align-middle text-center"><?php echo $Category ;?></td>
<td class="align-middle text-center"><?php echo $Plot_title ;?></td>
<td class="align-middle text-center"><?php echo $App_date ;?></td>
<td class="align-middle text-center"><?php echo $Customer ;?></td>
<td class="align-middle text-center" id= "status"><?php echo $Status ;?></td>
<td class="align-middle text-center">
<button type="button" name="edit" id="<?php echo $Plot_ID; ?>" class="btn btn-outline-primary btn-sm edit_data" data-toggle="modal" data-target="#modalform" data-backdrop="static" data-keyboard="false">Book</button>
</td>
</tr>
Jquery
$('#myTable tr#data_row td#status').each(function () {
switch ($(this).text()) {
case "Booked":
$(this).css({"background-color":"#eeac88", "color":" #7a3b2e"}); // (WORKING FOR CSS)
$(".edit_data").text("View"); // (NOT WORKING FOR TEXT CHANGE)
break;
case "on-Hold":
$(this).css("background-color","#fefbd8");
$(".edit_data").text("View");
break;
default:
$(this).css({"background-color": "#c5d5c5", "color":"green"});
$(".edit_data").text("Book");
break;
}
});
Try
$(".edit_data").hmtl("View");
Or put a <span> inside the button to access it with .text() method

popup auto redirect to undefined controller

i want to update data "barang" using pop up.
before the pop up goes down completely, the page redirect http://localhost/koperasi/index.php/cukl/undefined & show
" The page you requested was not found" error.
....
this is my controller
public function ubahbarang(){
if ($this->input->post('ubahdatabarang')) {
$id_barang=$this->input->post('id_barang');
$id_jenis=$this->input->post('id_jenis');
$nm_barang=$this->input->post('nm_barang');
$stok=$this->input->post('stok');
$hrg_beli=$this->input->post('hrg_beli');
$hrg_jual=$this->input->post('hrg_jual');
$this->barang->mengubah_barang($id_barang, $id_jenis, $nm_barang, $stok, $hrg_beli, $hrg_jual);
redirect('cukl', 'refresh');
}
}
....
this is my model
public function mengubah_barang($id_barang, $id_jenis, $nm_barang, $stok, $hrg_beli, $hrg_jual)
{
$hasil = $this->db->query("UPDATE barang SET id_barang='$id_barang', id_jenis='$id_jenis', nm_barang='$nm_barang', stok='$stok',
hrg_beli='$hrg_beli', hrg_jual='$hrg_jual'");
return $hasil;
}
...
this is table view which I use to update data using popups
<table class="table table-borderless table-striped table-earning">
<h2>BARANG</h2><br>
<thead>
<tr>
<th>ID BARANG</th>
<th>ID JENIS</th>
<th>NAMA BARANG</th>
<th class="text-right">STOK</th>
<th class="text-right">HARGA BELI</th>
<th class="text-right">HARGA JUAL</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
<tr>
<?php
foreach($barang as $i): ?>
<td><?=$i->id_barang;?></td>
<td><?=$i->id_jenis;?></td>
<td><?=$i->nm_barang;?></td>
<td><?=$i->stok;?></td>
<td><?=$i->hrg_beli;?></td>
<td><?=$i->hrg_jual;?></td>
<td style="text-align: center">
<a data-toggle="modal" data-target="#modal_edit<?= $i->id_barang;?>" class="btn btn-success">
<i class="fa fa-pencil" color="white"></i>Ubah
</a>
</td>
</tr>
</tbody>
<?php endforeach ?>
</table>
and this is the popups code
(sorry i cant write it one the page :(( )here is the pict
Opening
Closing
just in case the source code
( https://drive.google.com/open?id=1LbKScF65ej2kT0iQfhU2egBwAGWGA7Ee )
You have used redirect('cukl', 'refresh'); to redirect
Redirect goes in ways :-
redirect('control_name/method_name','refresh')
Your Code is right,but I think there is problem with routing.
You can try it by 301 redirect
redirect('/cukl', 'location', 301);
if still problem not solved please provide .htaccess and config/routes.php here.

Update status column in codigniter is not working

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.

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.

codeigniter- 3 pagination loading all data on all page

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