z0mbieKale
z0mbieKale

Reputation: 1028

How can we show recent notifications in jQuery/php/mySQL? update

Continuing question from here How can we show recent notifications in jQuery/php/mySQL?

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");
        }  

My two PHP files are fetchResults.php and updateResults.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:

<?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?

Code:

    $.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);

Upvotes: 1

Views: 691

Answers (1)

jeroen
jeroen

Reputation: 91762

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();

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.

Using the first option:

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

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.

Upvotes: 2

Related Questions