我正在使用通知,如果我添加新记录,则会在菜单中显示通知。
这里的通知工作正常,但问题是如果我刷新页面然后只有菜单通知可见,如果我添加新记录我怎么能立即显示通知
现在我的想法是刷新整页而不会有页面内容的干扰,它有效吗?
<?php
ini_set("display_errors",0);
$query="SELECT count(*) as count FROM patients AS p,users AS u WHERE p.status='a' AND p.confirm_status='p' AND p.users_id_user=u.id_user";
$resultPatients=$db->select($query);
$pendingAppointments=0;
if($resultPatients && count($resultPatients)>0)
{
$pendingAppointments=$resultPatients[0]['count'];
}
$query="SELECT count(*) as count FROM query WHERE shown_status='n'";
$resultQueries=$db->select($query);
$pendingQueries=0;
if($resultQueries && count($resultQueries)>0)
{
$pendingQueries=$resultQueries[0]['count'];
}
$query="SELECT count(*) as count FROM feedback WHERE shown_status='n'";
$resultFeedbacks=$db->select($query);
$pendingFeedbacks=0;
if($resultFeedbacks && count($resultFeedbacks)>0)
{
$pendingFeedbacks=$resultFeedbacks[0]['count'];
}
$query="SELECT count(*) as count FROM register WHERE shown_status='n'";
$resultRegisters=$db->select($query);
$pendingRegisters=0;
if($resultRegisters && count($resultRegisters)>0)
{
$pendingRegisters=$resultRegisters[0]['count'];
}
$query="SELECT count(*) as count FROM unregister WHERE shown_status='n'";
$resultunRegisters=$db->select($query);
$pendingunRegisters=0;
if($resultunRegisters && count($resultunRegisters)>0)
{
$pendingunRegisters=$resultunRegisters[0]['count'];
}
?>
<div class="mainNav">
<div class="user">
<a title="" href="index.php" class="leftUserDrop"><img src="images/icare.png" height="100" width="100" alt="" /><!--span><strong>3</strong></span--></a>
<!--ul class="leftUser">
<li><a href="#" title="" class="sProfile">My profile</a></li>
<li><a href="#" title="" class="sMessages">Messages</a></li>
<li><a href="#" title="" class="sSettings">Settings</a></li>
<li><a href="#" title="" class="sLogout">Logout</a></li>
</ul-->
</div>
<!-- Responsive nav -->
<div class="altNav">
<div class="userSearch">
<form action="">
<input type="text" placeholder="search..." name="userSearch" />
<input type="submit" value="" />
</form>
</div>
<!-- User nav -->
<ul class="userNav">
<li><a href="#" title="" class="profile"></a></li>
<li><a href="#" title="" class="messages"></a></li>
<li><a href="#" title="" class="settings"></a></li>
<li><a href="#" title="" class="logout"></a></li>
</ul>
</div>
<!-- Main nav -->
<ul class="nav">
<li><a href="index.php" title="" <?php if($_SESSION['menu']=='index') { ?> class="active" <?php } ?> ><img src="images/icons/mainnav/dashboard.png" height="30" width="30" alt="" /><span>Dashboard</span></a></li>
<?php
$totalcount=$pendingQueries+$pendingRegisters;
?>
<li><a href="users.php" title="" <?php if($_SESSION['menu']=='user') { ?> class="active" <?php } ?> ><img src="images/icons/mainnav/ui.png" height="30" width="30" alt="" /><span>Users</span><span class="requestSpan" ><?php if($totalcount>0) {?><strong id="queryCount"><?php echo $totalcount?></strong><?php }?></span></a></li>
<li><a href="doctors.php" title="" <?php if($_SESSION['menu']=='doctor') { ?> class="active" <?php } ?> ><img src="images/icons/mainnav/doctors.png" height="30" width="30" alt="" /><span>Doctors</span></span></a></li>
<?php
$totalcount2=$pendingAppointments+$pendingunRegisters;
?>
<li><a href="appointments.php" title="" <?php if($_SESSION['menu']=='patient') { ?> class="active" <?php } ?> ><img src="images/icons/mainnav/appointments.png" height="30" width="30" alt="" /><span>Appointments</span><span class="requestSpan" id="queryCount1"><?php if($totalcount2>0) {?><strong id="queryCount1"><?php echo $totalcount2?></strong><?php }?></span></a></li>
<li><a href="feedbacks.php" title="" <?php if($_SESSION['menu']=='feedback') { ?> class="active" <?php } ?> ><img src="images/icons/mainnav/forms.png" height="30" width="30" alt="" /><span>Feedbacks</span><span class="requestSpan" id="mainFeedbackCount"><?php if($pendingFeedbacks>0) {?><strong id="feedbackCount"><?php echo $pendingFeedbacks?></strong><?php }?></span></a></li>
<li><a href="offers.php" title="" <?php if($_SESSION['menu']=='offer') { ?> class="active" <?php } ?> ><img src="images/icons/mainnav/offers.png" height="30" width="30" alt="" /><span>Offers</span></a></li>
<!--li><a href="slots.php" title="" <?php if($_SESSION['menu']=='slot') { ?> class="active" <?php } ?> ><img src="images/icons/mainnav/about.png" height="30" width="30" alt="" /><span>Info</span></a></li-->
<li><a href="photos.php" title="" <?php if($_SESSION['menu']=='photo') { ?> class="active" <?php } ?> ><img src="images/icons/mainnav/upload.png" height="30" width="30" alt="" /><span>Photos</span></a></li>
<li><a href="services.php" title="" <?php if($_SESSION['menu']=='services') { ?> class="active" <?php } ?> ><img src="images/icons/mainnav/services.png" height="30" width="30" alt="" /><span>Services</span></a></li>
</ul>
</ul>
</div>
<div id="sound" style="display: none"></div>
<script type="text/javascript">
function playSound(filename){
document.getElementById("sound").innerHTML='<audio autoplay="autoplay"><source src="' + filename + '.mp3" type="audio/mpeg" /><source src="' + filename + '.ogg" type="audio/ogg" /><embed hidden="true" autostart="true" loop="false" src="' + filename +'.mp3" /></audio>';
}
function getNotification()
{
var request=$.ajax({
url: "getNotificationCounts.php",
dataType: "json",
async: false
});
request.done(function(response){
var queryCount=$('#queryCount').html();
var queryCount1=$('#queryCount1').html();
var appointmentCount=$('#appointmentCount').html();
var feedbackCount=$('#feedbackCount').html();
var status=false;
if(response.queries>0)
{
if(!queryCount)
{
status=true;
$('#mainQueryCount').html('<strong id="queryCount"></strong>');
}
if(queryCount!=''&&response.queries>parseInt(queryCount))
status=true;
$('#queryCount').html(response.queries);
}
if(response.unregister>0)
{
if(!queryCount1)
{
status=true;
$('#mainQueryCount1').html('<strong id="queryCount1"></strong>');
}
if(queryCount1!=''&&response.queries1>parseInt(queryCount1))
status=true;
$('#queryCount1').html(response.unregister);
}
if(response.appointments>0)
{
if(!appointmentCount)
{
status=true;
$('#mainAppointmentCount').html('<strong id="appointmentCount"></strong>');
}
if(appointmentCount!=''&&response.appointments>parseInt(appointmentCount))
status=true;
$('#appointmentCount').html(response.appointments);
}
if(response.feedbacks>0)
{
if(!feedbackCount)
{
status=true;
$('#mainFeedbackCount').html('<strong id="feedbackCount"></strong>');
}
if(feedbackCount!=''&&response.feedbacks>parseInt(feedbackCount))
status=true;
$('#feedbackCount').html(response.feedbacks);
}
if(status)
playSound('bing');
});
}
setInterval(function(){getNotification();},30000);
</script>
答案 0 :(得分:1)
使用ajax
获取通知记录并调用setInterval
函数以便在每5秒左右调用ajax
函数以获取记录而不刷新整个页面time interval
在miliseconds
中,您可以根据需要给出时间间隔
setInterval(function(){
get_latest_notification() // this will run after every 5 seconds
}, 5000);