我们如何在jQuery/php/mySQL中显示最近的通知?更新 [英] How can we show recent notifications in jQuery/php/mySQL? update

查看:70
本文介绍了我们如何在jQuery/php/mySQL中显示最近的通知?更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处的持续问题因此,如以下答案中所述.我在数据库中添加了一个neew列,名为seened.默认值为0.如果我理解正确,那么我将需要在显示通知后立即将seen = 0更改为1,这样它将不再循环并向我显示无限数量的同一通知.

So as stated below in the answers. I added a neew column to DB called seen. Default value is 0. If I understand correctly, then I would need to change the seen=0 to 1 as soon the notification is displayed, so it wont loop anymore and show me infinite amount of the same notification.

这就是我现在所拥有的:

That's what I have at the moment:

        function fetch_notification(){
            setInterval(function(){ 
                //GET ALL DATA WHERE SEEN=0
                $.ajax({ 
                    url: "fetchResults.php", 
                    success: function(data){ 
                        $.each(data.vormid, function(i, vormid) {
                            $("#noti-box").append('<div class="alert alert-info "><button data-dismiss="alert" class="close close-sm" type="button"><i class="fa fa-times"></i></button>New form filled out by Dr. '+data.vormid[i].arsti_eesnimi+' '+data.vormid[i].arsti_perekonnanimi+'</div>'); 
                        });
                        update_notification();

                    }, dataType: "json"}); 
            }, 5000);
        } 
        fetch_notification();   

        //UPDATE SEEN=0 to 1
        function update_notification(){
            console.log("updating");
        }  

我的两个PHP文件是fetchResults.php和updateResults.php

My two PHP files are fetchResults.php and updateResults.php

fetchResults.php:

fetchResults.php:

<?php
header('Content-Type: application/json');
include_once '../dbconfig.php';



$stmt4 = $DB_con->prepare("SELECT * FROM ravim WHERE seen =0 ORDER BY date_created DESC");
$stmt4->execute();
$vormid = $stmt4->fetchAll(PDO::FETCH_ASSOC);

echo json_encode(array("vormid" => $vormid));
?>

updateResults.php:

updateResults.php:

<?php
header('Content-Type: application/json');
include_once '../dbconfig.php';

$ravim_id = $_POST['ravim_id'] ;

$stmt4 = $DB_con->prepare("UPDATE ravim SET seen=1 WHERE ravim_id=:ravim_id");
$stmt4->execute();
?>

编辑 所以我设法只显示一次通知.现在,我的问题与音频有关,通常来说,我编写的代码是否正常",还是应该更改某些内容. 音频:每次通知到时,我都想播放声音,但从一开始它就一直播放一次.我尝试添加一个循环计数器,并检查对象是否为空,但是它不起作用.有什么建议或好的做法,如何处理音频?

EDIT So i managed to show the notification only once. My question now comes with the audio and in general if the code I wrote is "okay" or should I change something. Audio: I would like to play a sound every time the notification comes, but it keeps playing once in the beginning. I tried adding a loopcounter, and checking if the object is empty or not, but it doesn't work. Any advice or good practice how to deal with audio?

代码:

    $.ajaxSetup ({  
        cache: false  
    });  
    var loopLimit = 1;
    var loopCounter = 0;
    setInterval(function(){
        $.getJSON('fetchResults.php', function(data) {
            $("#loadingDiv").show();
            $('#noti-box').empty();
            $("#notificationTitle").empty();

            if(jQuery.isEmptyObject(data)){
                console.log("there is no data");
            }else{
                console.log(data);
                if (loopCounter < loopLimit){
                    var sound = $("#notification")[0];
                    sound.currentTime = 0;
                    sound.load();
                    sound.play();
                    loopCounter++;
                }
                $.each(data.vormid, function(i, vormid) {
                    $("#noti-box").append('<div class="alert alert-info"><button id='+data.vormid[i].ravim_id+' data-dismiss="alert" class="close close-sm" type="button"><i class="fa fa-times"></i></button>New form filled out by Dr. <b>'+data.vormid[i].arsti_eesnimi+' '+data.vormid[i].arsti_perekonnanimi+' </b> at '+data.vormid[i].date_created+'</div>'); 
                    var id= $(".alert.alert-info").val();
                    $("#notificationTitle").append('<li style="font-size: 14px; padding: 0; margin: 0 0 10px 10px; color: #666666;" >New form filled out by Dr. '+data.vormid[i].arsti_eesnimi+' '+data.vormid[i].arsti_perekonnanimi+'</li>');
                    $(".close").on('click', function () {
                        var ravim_id = $(this).attr('id'); 
                        $.ajax({
                            type: "POST",
                            url:'updateResults.php',
                            data:"ravim_id=" + ravim_id,
                            success:function(data){
                                console.log("success");
                            },error: function(data){
                                console.log("not saved");
                            } 
                        });
                    });
                });
            }
            $("#loadingDiv").fadeOut("slow");
        });
    }, 5000);

推荐答案

目前尚不清楚问题出在哪里,但是这里您的代码有错误:

It is not entirely clear what the problem is, but here you have an error in your code:

$stmt4 = $DB_con->prepare("UPDATE ravim SET seen=1 WHERE ravim_id=:ravim_id");
$stmt4->execute();

您正在使用绑定参数,但没有绑定它.您可以通过将数组添加到execute()方法来解决该问题,或者在单独的语句中手动将其绑定.

You are using a bound parameter but you are not binding it. You can solve that by adding an array to the execute() method or you bind it manually in a separate statement.

使用第一个选项:

$stmt4 = $DB_con->prepare("UPDATE ravim SET seen=1 WHERE ravim_id=:ravim_id");
$stmt4->execute(array(
    ':ravim_id' => $ravim_id
));

请注意,您还可以设置PDO在遇到问题时引发异常,因此您应该这样做,以便在有问题时得到通知.

Note that you can also setup PDO to throw exceptions when it runs into problems so you should probably do that so that you get notified when there is one.

这篇关于我们如何在jQuery/php/mySQL中显示最近的通知?更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆