【问题标题】:Real time notification by using php and jquery使用 php 和 jquery 实时通知
【发布时间】:2015-11-01 21:33:03
【问题描述】:

我有一些事件,当创建新事件时,我希望向用户弹出通知,以便他们知道。我想在任何用户创建新事件时生成实时通知,我为此使用了 php 和 jQuery,但我面临的问题是我的代码在新事件到达时生成通知,但只针对一个用户。我希望每个用户都可以收到有关事件的通知,但如果用户收到该通知则不需要多次显示,但如果没有,则应该生成通知。所以这是我的代码:

Notification.php:-

<?php
$user = "root";
$pass = "";
$host = "localhost";
$dbname = "db_notification";
try {
    $conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); 
    $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo $e->getMessage();
}
$output = array(
    'found' => 0,
    'notifications' => array()
);
if ( ! empty($_GET['action'])) {
    switch ($_GET['action']) {
        case "show_last_five":
            $sql = "SELECT * FROM `tbl_notification` INNER JOIN    tbl_user_notification ON tbl_user_notification.notification_id =   tbl_notification.id WHERE tbl_user_notification.user_id = '1';";
            $q = $conn->query($sql);
            $q->setFetchMode(PDO::FETCH_ASSOC);
            if ($q->rowCount() > 0) {
                $output['found'] = 0;
                $output['notifications'] = null;
            } else {
                $sql = "SELECT * FROM `tbl_notification` LIMIT 5;";
                $q = $conn->query($sql);
                $q->setFetchMode(PDO::FETCH_ASSOC);
                $output['found'] = $q->rowCount();
                $tempArr = array();
                $insertArray = "";
                foreach ($q->fetchAll() as $row) {
                    array_push($tempArr, $row);
                    $sql1 = "INSERT INTO tbl_user_notification (user_id,notification_id)   VALUES('1','" . $row['id'] . "')";
                    $q1 = $conn->query($sql1);
                }
                $output['notifications'] = $tempArr;
            }
        break;
        default:
        break;
    }
    header('Content-Type:application/json;');
    echo json_encode($output);
}
?>

Notification.js:-

$(document).ready(function() {
    var lastDisplayed = 0;
    $('#gnrt_notfctn').on('click', function () {
        $.getJSON('http://localhost/test/test/notifications/notification.php?  action=show_last_five', function (data) {
            var output = '';
            if (data['found'] > 0) {
                $.each(data['notifications'], function (i, v) {
                    if ($('#lastNotification').text() !== v['id']) {
                        var close_button = '';
                        if (v['close_button'] == 1) {
                            close_button = "<span style='position:absolute;right:25px;' data- dismiss='alert'><i class='fa fa-close'></i></span>";
                        } else {
                            close_button = "";
                        }
                        output = output + "<div class='alert alert-" + v['type'] + "'>" +
                            close_button + "<h4 class='alert_heading'><i class='fa fa-" + v['icon'] + "'>    </i>
                            &nbsp;" + v['heading'] + "</h4>" +
                            "<p class='alert_body'>" + v['notification_text'] + "</p></div>";
                        $('#lastNotification').text(v['id']);
                    }
                });
                $('#show_notification').html(output);
            }
        });
    });
    setInterval(function () {
        $('#gnrt_notfctn').click();
    }, 3000);
});

【问题讨论】:

    标签: php jquery pdo notifications


    【解决方案1】:

    设计 API,使其接受用户 ID 和上次收到的通知。

    所以在查询中,使用 user_id 和日期时间过滤通知表

    例如:(考虑表中有数据时间字段)

    SELECT * FROM tbl_notification INNER JOIN    tbl_user_notification ON tbl_user_notification.notification_id =   tbl_notification.id WHERE tbl_user_notification.user_id = '1' and  tbl_notification.datetime>lastreceiveddatatime
    

    希望对你有用

    【讨论】:

    • 提供更多细节和一些代码示例,会让您的回答更有帮助。你的答案目前很模糊。
    • 很高兴能帮到你:)
    猜你喜欢
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多