Php 用户组的通知

Php 用户组的通知,php,Php,我正在开发一个事件管理器,我想为一组用户创建一个通知系统。 当管理员创建一个新事件时,php会在mysql中创建一个通知,如果未读取,则状态为0;如果已读取,则状态为1 问题是我使用的是组id而不是用户id,当第一个登录的用户单击通知时,它会更新通知状态,以便其他用户不查看通知。也许在数据库中为每个用户插入一行可以解决这个问题?我是否需要使用用户ID数组 这就是我到目前为止所做的: session_start(); // admin id $userid = $_SESSION['user_

我正在开发一个事件管理器,我想为一组用户创建一个通知系统。 当管理员创建一个新事件时,php会在mysql中创建一个通知,如果未读取,则状态为0;如果已读取,则状态为1

问题是我使用的是组id而不是用户id,当第一个登录的用户单击通知时,它会更新通知状态,以便其他用户不查看通知。也许在数据库中为每个用户插入一行可以解决这个问题?我是否需要使用用户ID数组

这就是我到目前为止所做的:

session_start();

// admin id 
$userid = $_SESSION['user_id'];

include('../../../config/database.php');

$event_title = $_POST['titleevent'];
$event_color = $_POST['eventcolor'];
$event_start = $_POST['startevent'];
$new_start_date = date('Y-m-d 00:00:00', strtotime($event_start));
$event_end = $_POST['endevent'];
$new_end_date = date('Y-m-d 23:59:00', strtotime($event_end));
$event_group = $_POST['usergroup'];
$event_description = $_POST['eventdescription'];

    if ($create_event = mysqli_prepare($conn, "INSERT INTO user_events (event_title, event_description, event_start, event_end, event_color, event_group)
                    VALUES (?, ?, ?, ?, ?, ?)" )) {

    mysqli_stmt_bind_param($create_event, 'sssssi', $event_title, $event_description, $new_start_date, $new_end_date, $event_color, $event_group);
    mysqli_stmt_execute($create_event);

        // get event id
        $event_id = mysqli_insert_id($conn);

        $event_start_date = date('d-m-Y', strtotime($new_start_date));
        $notification = "Nuovo evento <span class='text-warning'><strong>$event_title</strong></span> inizia il $event_start_date";
        $notification_status = "0";
        $notification_category= "events";

        $event_notification = mysqli_prepare($conn, "INSERT INTO event_notifications (e_notification_sent_by, e_notification_sent_to, e_notification_message, e_notification_time, e_notification_status, e_notification_category, e_notification_category_id) VALUES(?,?,?,now(),?,?,?)");
        mysqli_stmt_bind_param($event_notification, 'iisisi', $userid, $event_group, $notification, $notification_status, $notification_category, $event_id);
        mysqli_stmt_execute($event_notification);
        mysqli_stmt_close($event_notification);


     echo "event created";

    mysqli_stmt_close($create_event);


    }else{

        echo "Ops, error";

    }
session_start();
//管理员id
$userid=$\u会话['user\u id'];
包括('../../config/database.php');
$event_title=$_POST['titleevent'];
$event_color=$_POST['eventcolor'];
$event\u start=$\u POST['startevent'];
$new_start_date=date('Y-m-d 00:00:00',strottime($event_start));
$event_end=$_POST['endevent'];
$new_end_date=日期('Y-m-d 23:59:00',标准时间($event_end));
$event_group=$_POST['usergroup'];
$event_description=$_POST['eventdescription'];
如果($create\u event=mysqli\u prepare($conn),则插入用户\u事件(事件标题、事件描述、事件开始、事件结束、事件颜色、事件组)
值(?,,,,,,,,?)){
mysqli_stmt_bind_param($create_event,'sssss i',$event_title,$event_description,$new_start_date,$new_end_date,$event_color,$event_group);
mysqli_stmt_execute($create_事件);
//获取事件id
$event\u id=mysqli\u insert\u id($conn);
$event_start_date=日期('d-m-Y',strottime($new_start_date));
$notification=“Nuovo evento$event\u titleIllizia il$event\u start\u date”;
$notification_status=“0”;
$notification_category=“事件”;
$event_notification=mysqli_prepare($conn,“插入事件_通知(e_通知发送人、e_通知发送人、e_通知发送人、e_通知消息、e_通知时间、e_通知状态、e_通知类别、e_通知类别id)值(?,,,,)”;
mysqli_stmt_bind_参数($event_notification,'iissi',$userid,$event_group,$notification,$notification_status,$notification_category,$event_id);
mysqli_stmt_execute($event_notification);
mysqli_stmt_close($event_notification);
回显“已创建事件”;
mysqli_stmt_close($create_event);
}否则{
回声“行动,错误”;
}
非常感谢你的帮助

更新我已通过以下方式使用数组和foreach使其正常工作:

$event_start_date = date('d-m-Y', strtotime($new_start_date));
        $notification = "Nuovo evento <span class='text-warning'><strong>$event_title</strong></span> inizia il $event_start_date";
        $notification_status = "0";
        $notification_category= "events";


        $sql = "SELECT user_join_id FROM user_group_join WHERE group_join_id='$event_group'";

        $result= mysqli_query($conn,$sql);

        $datas= array();

        if(mysqli_num_rows($result) > 0){

            while($row=mysqli_fetch_array($result, MYSQLI_ASSOC)){

            $datas[]= $row;

            }

        }

        foreach($datas as $data) {

        $id_cliente = $data['user_join_id'];

        $event_notification = mysqli_prepare($conn, "INSERT INTO user_notifications (notification_sent_by, notification_sent_to, notification_message, notification_time, notification_status, notification_category, notification_category_id) VALUES(?,?,?,now(),?,?,?)");
        mysqli_stmt_bind_param($event_notification, 'iisisi', $userid, $id_cliente, $notification, $notification_status, $notification_category, $event_id);
        mysqli_stmt_execute($event_notification);
        mysqli_stmt_close($event_notification);

        }
$event_start_date=date('d-m-Y',strottime($new_start_date));
$notification=“Nuovo evento$event\u titleIllizia il$event\u start\u date”;
$notification_status=“0”;
$notification_category=“事件”;
$sql=“从用户组中选择用户加入id,其中组加入id=”$event\u group';
$result=mysqli\u查询($conn,$sql);
$datas=array();
如果(mysqli_num_行($result)>0){
而($row=mysqli\u fetch\u数组($result,mysqli\u ASSOC)){
$datas[]=$row;
}
}
foreach($datas作为$data){
$id_cliente=$data['user_join_id'];
$event_notification=mysqli_prepare($conn,“插入用户_通知(通知发送人、通知发送人、通知消息、通知时间、通知状态、通知类别、通知类别id)值(?,,,,现在(),,,,,,)”;
mysqli_stmt_bind_参数($event_notification,'iissi',$userid,$id_cliente,$notification,$notification_status,$notification_category,$event_id);
mysqli_stmt_execute($event_notification);
mysqli_stmt_close($event_notification);
}
现在的问题是,我如何才能用准备好的语句转换它?而且,看起来我将使用大量服务器资源查询数据库中的每个用户id,有没有办法优化foreach循环?


非常感谢

如果问题是关于布尔值
0 | 1
,您(可能)需要使用UPDATE来“更新”所述行。您好@Fred ii-感谢您的回答,我使用ajax点击事件运行更新。我需要做的是使用数组获取属于特定组的所有用户id,然后在数据库中为每个用户创建一行,以便在事件通知查询中使用id数组而不是$event\u group。使用
WHERE
子句运行SELECT,使用它们各自的列。好,但是,如果我的组中有多个用户,它就不起作用了:(很难说为什么它在这里不为您工作,因为我不知道它的查询是什么样子的。如果您要分组,或者限制1,或者不使用该函数,那么这可能是一个或多个原因。您(可能)需要使用UPDATE来“更新”如果问题是关于布尔值
0 | 1
的一行。您好@Fred ii-感谢您的回答,我使用ajax点击事件运行更新。我需要做的是使用数组获取属于特定组的所有用户id,然后在数据库中为每个用户创建一行,这样我就可以使用id数组代替事件通知查询中$event\u组的ad。使用
WHERE
子句运行SELECT,使用它们各自的列。可以,但如果组中有多个用户,则此选项无效:(很难说为什么它不适用于您,因为我不知道它的查询是什么样子的。如果您使用GROUP BY、LIMIT 1或不使用该函数,那么这可能是一个或多个原因。