weixin_33713503 2016-08-09 20:27 采纳率: 0%
浏览 31

PHP MYSQL,简单通知

can someone help me how to get rid the counts or number on notification after I read or open it... I hope you understand, sorry if its vague and to my bad English tho. Just here my sample codes..

/index.php

   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">

function addmsg(type, msg){

$('#notification_count').html(msg);

}

function waitForMsg(){

$.ajax({
type: "GET",
url: "select.php",

async: true,
cache: false,
timeout:50000,

success: function(data){
addmsg("new", data);
setTimeout(
waitForMsg,
1000
);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
addmsg("error", textStatus + " (" + errorThrown + ")");
setTimeout(
waitForMsg,
15000);
}
});
};

$(document).ready(function(){
waitForMsg();
});
</script>



</head>

<body>

    <span id='notification_count'></span>
<a href="notificationview.php" id="notificationLink" onclick = "return getNotification()">Notifications</a>
<div id="HTMLnoti" style="textalign:center"></div>


<br>
<p style="font-weight: bold; font-size: 20px; font-family: Tahoma;">Admin panel</p>
<form action="index.php" method="post">
<input type="text" required name="notification" autofocus="on" autocomplete="off">
<br>
<br>
<input type="text" name="status" value="unread" style="display: none;">
<input type="submit" name="btnsub" value="Submit">
</form>

and then my /select.php where why my notification counts..

 <?php
       $servername = "localhost";
       $username = "root";
       $password = "";
       $dbname = "messageTest";

       // Create connection

       $conn = new mysqli($servername, $username, $password, $dbname);

       // Check connection

       if ($conn->connect_error) {

           die("Connection failed: " . $conn->connect_error);

       } 

       $sql = "SELECT * from messageTest where status = 'unread'";
       $result = $conn->query($sql);
       $row = $result->fetch_assoc();
       $count = $result->num_rows;
       echo $count;
       $conn->close();
?>

please! all I want is get rid of the counts on the notification after the user open or read it. Thanks!

my database name = "messageTest" my database table = "messagetest" inside my table =

id notification status

  • 写回答

2条回答 默认 最新

  • weixin_33739523 2016-08-09 20:30
    关注

    If you don't want to show the count if there are no unread values, you simply don't show it. Easy as that.

    if ($count > 0) {
        echo $count;
    } else {
        // Do nothing
    }
    

    You may also want to consider checking out some basic programming tutorials.

    评论
  • weixin_33670713 2016-08-09 20:53
    关注

    When you are displaying the notification count:

    $count = $result->num_rows; echo ($count > 0) ? $count : 0;

    Now call the addmsg function only when the count is not == 0 meaning there actually are some unread notifications.

    success: function(data){
      if(data >0){ // unread notification
            addmsg("new", data);
        }
    setTimeout(
    waitForMsg,
    1000
    );
    }

    Now your notification link has a

    onclick = "return getNotification()"

    You would not need the return keyword. You can now have the ajax function as:

    function getNotification(){
    
    $.ajax({
    type: "GET",
    url: "notification.php",
    
    success: function(data){
    },
    error: function(XMLHttpRequest, textStatus, errorThrown){
    addmsg("error", textStatus + " (" + errorThrown + ")");
    }
    });
    };
    Now your notification.php should handle the update query by something like:

    //Connection part 
    
    $sql = "UPDATE messageTest Set status = 'unread'";
           $conn->query($sql);
           echo 1;
           $conn->close();

    However, there is a lot more efficient way to handle this, just trying to fit the code you already have in place. You would not need separate pages for each ajax call. You can create all of them in a single file and create different functions. Try searching for php ajax notification example.

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 PMM 9010/30P
  • ¥15 pom文件依赖管理,未找到依赖
  • ¥15 现在后端返回给我一堆下载地址,都是一个视频切片后的,如何在uniapp安卓环境下将这些分片的视频下周并且合并成原始视频并下载到本地。
  • ¥15 Unity导出2D项目运行时图片变成马赛克
  • ¥15 关于communitytoolkit.mvvm的生成器得到的代码看起来没有被使用的问题
  • ¥15 matlab中此类型的变量不支持使用点进行索引
  • ¥15 咨询第六届工业互联网数据创新大赛原始数据
  • ¥15 Pycharm无法自动补全,识别第三方库函数接收的参数!
  • ¥15 STM32U575 pwm和DMA输出的波形少一段
  • ¥30 android百度地图SDK海量点显示标题