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

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

问题描述

假设有一个新行插入到数据库中.如何在div中显示一次通知,并在关闭通知后又不显示该通知?我知道我不能使用setInterval,因为它会在一段时间(例如10秒)后不断弹出.我的想法是,它每10秒检查一次更新,如果有新行,它将显示通知,但是在我按"x"后,它将关闭. 10秒钟后,它将再次检查,现在将检查该行的ID是否与以前相同.如果不是,则显示新的吐司.

Let's say there is a new row inserted into database. How can I display a notification once in a div and after closing it, it does not show up again? I know I cannot use setInterval, because it keeps popping up after an interval (let's say 10 seconds). My idea is that it checks for updates every 10 seconds and if there is a new row, it will display the notification, but after I press "x", it closes. After 10 seconds it will check again and now it will check if the id of the row is the same as previously or not. If not, then display a new toast.

所以我使用了setTimeout函数并再次触发了它.

So I used the setTimeout function and triggered it again.

    (function poll(){
        setTimeout(function(){
            $.ajax({ url: "ravimDataCount.php", success: function(data){
                //check if the id is the same or not here

                $("#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.</div>');

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

长期以来一直在为此奋斗.任何帮助表示赞赏.

Been struggling with it for a long time now. Any help appreciated.

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

EDIT! 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();
?>

我无法弄清楚.我知道我必须将通知的正确行ID传递给updateResults.php.

I cannot figure it out. I know I have to pass the correct row ID of the notification to updateResults.php.

推荐答案

我知道实现的方式是

The way I know to implement it is

  1. 根据需要添加一个名为seen的列(INT)或您喜欢的名称.
  2. seen设置为默认值0.
  3. 显示所有看不见的行的通知,即seen =0;
  4. 的值 在显示通知后,
  5. 将所有具有seen=0的行的列seen更新为1.
  6. 仅此而已.

  1. Add a column(INT) named seen or something you like according to your requirement.
  2. set seen to default values 0.
  3. display the notification for all the rows which are unseen , ie., value of seen =0;
  4. update the column seen as 1 for all the rows that have seen=0after displaying the notification.
  5. that's all .


而jQuery部分是

And the jQuery part is

  1. 首先创建一个函数,让fetch_notification()包括ajax来选择seen=0
  2. 所在的行
  3. 然后创建另一个函数,让我们说update_notification()将具有seen=0的每一行更新为seen=1.
  4. 然后间隔调用fetch_notification()
  1. first create a function lets say fetch_notification() including the ajax to select the rows where seen=0
  2. then create another function lets say update_notification() to update each rows which have seen=0 to seen=1.
  3. then after an interval call fetch_notification()

更新

另一个想法是
创建表并将新通知添加到表中,并在用户显示或关闭通知时删除每行

another idea is
Create a table and add new notifications into a table and delete each rows when the notification is displayed or closed by the user

让您说出是否要向管理员发送有关新用户的通知

lets say if you want to give admin notification about new user

  1. 将每个新用户ID添加到新表中.
  2. 当管理员在每个通知中单击关闭时,逐一删除相应的每一行


jQuery部分

jQuery part

  1. 使用ajax检查表(新通知表)的行号是否大于一, 如果它大于一个,则显示所有要作为通知管理员的用户ID.
  2. 如果管理员单击关闭以获取通知,请使用另一个ajax删除该行.
  3. 因此,您需要创建一个函数来频繁检查行数是否大于一.
  4. 另一个使用ajax删除行的功能,仅当管理员单击x(关闭)时,该功能才需要工作.
  1. Check if the row number of the table(new notification table) is greater than one using ajax, if its greater than one display all the user id to admin as notification.
  2. if admin clicks close for a notification ,delete the row using another ajax.
  3. So you need to create a function to frequently check if the number of rows are greater than one .
  4. And another function using ajax to delete the rows and it need to work only when the admin click x (close).

这个想法更好,因为它不会给服务器端带来太大的负载(实际上根本没有负载.).

主要数据库功能是插入,删除和检查行号

This idea is better since it doesn't give much load on the server side (actually speaking no load at all.).

main database functions are insertion,deletion and checking row number

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

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