notification system in codeigniter
table creation for notification-system-in-codeigniter code given below
create table comments
(
comment_id INT not null primary key,
comment_subject VARCHAR(250) not null,
comment_text TEXT not null,
comment_status INT not null
)
views/notifications.php
<!DOCTYPE html>
<html>
<head>
<title>Notification using PHP Ajax Bootstrap</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
.count {
color: white;
background-color: red;
}
</style>
</head>
<body>
<br /><br />
<div class="container">
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">PHP Notification System</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle sample" data-toggle="dropdown">
<span class="label label-pill count" style="border-radius:10px;">
</span>
<span class="glyphicon glyphicon-bell" style="font-size:18px;">
</span></a>
<ul class="dropdown-menu"></ul>
</li>
</ul>
</div>
</nav>
<br />
<form method="post" id="comment_form" action="">
<div class="form-group">
<label>Enter Subject</label>
<input type="text" name="subject" id="subject" class="form-control">
</div>
<div class="form-group">
<label>Enter Comment</label>
<textarea name="comment" id="comment" class="form-control" rows="5"></textarea>
</div>
<div class="form-group">
<input type="submit" name="post" id="post" class="btn btn-info" value="Post" >
<?php echo date('d-m-Y') ?>
</div>
</form>
</div>
</body>
<script>
$(document).ready(function(){
//updating the view with notifications using ajax
function load_unseen_notification(view = '')
{
$.ajax({
url:"<?php echo base_url();?>Notify/user_notifi",
method:"POST",
data:{"view":view},
dataType:"json",
success:function(data)
{
$('.dropdown-menu').html(data.notification);
$('.count').show();
if(data.unseen_notification > 0)
{
$('.count').html(data.unseen_notification);
} else if(data.unseen_notification == ''){
$('.count').hide();
}
}
});
}
load_unseen_notification();
//submit form and get new records
$('#comment_form').on('submit', function(event){
event.preventDefault();
var subject=document.getElementById('subject').value;
var comment=document.getElementById('comment').value;
if(subject!=""||comment!="")
{
$.ajax({
url:"<?php echo base_url();?>Notify/submit_comm",
method:"POST",
data:{subject:subject,comment:comment},
success:function(data)
{
$('#comment_form')[0].reset();
load_unseen_notification();
}
});
}
else
{
alert("Both Fields are Required");
}
});
// load new notifications
$(document).on('click', '.dropdown-toggle', function(){
$('.count').html('');
load_unseen_notification('yes');
});
setInterval(function(){
load_unseen_notification();;
}, 5000);
});
</script>
</html>
Controllers/Notify.php
<?php
class Notify extends CI_Controller{
public function __construct() {
parent::__construct();
$this->load->model('Notify_Model');
}
public function index() {
$this->load->view('notifications');
}
public function submit_comm()
{
$subject =$this->input->post('subject');
$comment =$this->input->post('comment');
$chk=$this->Notify_Model->data_insert($subject,$comment);
if($chk==1)
{
echo 'inserted';
}
}
public function user_notifi()
{
$v=$this->input->post('view');
echo $op= $this->Notify_Model->fetch_data($v);
return $op;
}
}
Models/Notify_Model.php
<?php
class Notify_Model extends CI_Model{
public function __construct() {
parent::__construct();
}
// creating table given below:
//
// create table comments
// (
// comment_id INT not null primary key,
// comment_subject VARCHAR(250) not null,
// comment_text TEXT not null,
// comment_status INT not null
// )
public function data_insert($subject,$comment) {
$this->db->set('comment_subject', $subject);
$this->db->set('comment_text',$comment);
$qry=$this->db->insert('comments');
if($qry)
{
return 1;
}
}
public function fetch_data($v)
{
if($v != '')
{
$this->db->set('comment_status', '1');
$this->db->where('comment_status','0');
$this->db->update('comments');
echo '0';
}
$this->db->select();
$this->db->from("comments");
$this->db->order_by("comment_id", "DESC");
$result = $this->db->get();
$output = '';
if($result->num_rows() > 0)
{
foreach($result->result() as $row)
{
//var_dump($row);
$output .='<li><a href="#"> <strong>Subject: ' . $row->comment_subject . ' </strong> <br /> <small><em>Comment: '.$row->comment_text.'</em></small></a> </li>';
}
}
else
{
$output .= '<li><a href="#" class="text-bold text-italic"> No Noti Found </a></li>';
//
}
$this->db->select();
$this->db->from("comments");
$this->db->where("comment_status", "0");
$result1 = $this->db->get();
$count=$result1->num_rows();
$data= array('notification'=>$output,'unseen_notification'=>$count);
return json_encode($data);
}
}