【发布时间】:2019-05-23 22:05:45
【问题描述】:
首先我有一个聊天脚本,我聊天中的每个用户都有他自己的个人资料和他的个人资料中的一些信息,例如国家、年龄、磨床、他的照片和他的朋友,不知何故我需要显示他所有消息的计数他在聊天中发送的消息,我会在他的个人资料中创建一个新字段并输入这个号码。
所以这里我有这个功能,可以为数据库中的用户存储消息。
function userPostChat($content, $snum = ''){
global $mysqli, $data;
$lact = calMinutes(3);
$style = escape($data['bccolor'] . ' ' . $data['bcbold']);
$mysqli->query("INSERT INTO `my_chat` (post_date, user_id, post_message, post_roomid, type, snum, tcolor) VALUES ('" . time() . "', '{$data['user_id']}', '$content', '{$data['user_roomid']}', 'public', '$snum', '$style')");
$last_id = $mysqli->insert_id;
$mysqli->query("UPDATE my_users SET caction = caction + 1 WHERE user_roomid = '{$data['user_roomid']}' and last_action > '$lact'");
if($snum != ''){
$user_post = array(
'post_id'=> $last_id,
'type'=> 'public',
'post_date'=> time(),
'tcolor'=> $style,
'post_message'=> $content,
);
$post = array_merge($data, $user_post);
if(!empty($post)){
return createLog($data, $post);
}
}
}
我忘了说该函数已经计算了来自所有用户的消息并将其存储在 my_users 表中名为“caction”的列中,但是此函数同时计算所有用户的消息,例如,如果我有 0 分并且有人发送聊天中的一条消息它会为我和他以及聊天中的所有用户计算这条消息,我不想要它,我想要一个函数来计算每个用户的消息并显示数字。
这是我的桌子:
表my_users
user_id
user_name
password
user_photo
user_email
room_id
caction
表格my_chat
post_id
user_id
post_date
post_message
post_roomid
type
snum
tcolor
这里有我的 chat_file.php
function userPostChatFile($content, $file_name, $type, $file_name2 = ''){
global $mysqli, $data;
$lact = calMinutes(3);
$mysqli->query("INSERT INTO `my_chat` (post_date, user_id, post_message, post_roomid, type, file) VALUES ('" . time() . "', '{$data['user_id']}', '$content', '{$data['user_roomid']}', 'public', '1')");
$rel = $mysqli->insert_id;
$mysqli->query("UPDATE my_users SET caction = caction + 1 WHERE user_roomid = '{$data['user_roomid']}' and last_action > '$lact'");
if($file_name2 != ''){
$mysqli->query("INSERT INTO `my_upload` (file_name, date_sent, file_user, file_zone, file_type, relative_post) VALUES
('$file_name', '" . time() . "', '{$data['user_id']}', 'chat', '$type', '$rel'),
('$file_name2', '" . time() . "', '{$data['user_id']}', 'chat', '$type', '$rel')
");
}
else {
$mysqli->query("INSERT INTO `my_upload` (file_name, date_sent, file_user, file_zone, file_type, relative_post) VALUES ('$file_name', '" . time() . "', '{$data['user_id']}', 'chat', '$type', '$rel')");
}
return true;
}
if(!myAllow($data['allow_image']) || muted() || roomMuted()){
die();
}
if (isset($_FILES["file"])){
ini_set('memory_limit','128M');
$info = pathinfo($_FILES["file"]["name"]);
$extension = $info['extension'];
$origin = escape(filterOrigin($info['filename']) . '.' . $extension);
if ( fileError() ){
echo 1;
die();
}
if (isImage($extension)){
$imginfo = getimagesize($_FILES["file"]["tmp_name"]);
if ($imginfo !== false) {
$width = $imginfo[0];
$height = $imginfo[1];
$type = $imginfo['mime'];
$fname = encodeFileTumb($extension);
$file_name = $fname['full'];
$file_tumb = $fname['tumb'];
move_uploaded_file(preg_replace('/\s+/', '', $_FILES["file"]["tmp_name"]), "../upload/chat/" . $file_name);
$source = '../upload/chat/' . $file_name;
$tumb = '../upload/chat/' . $file_tumb;
$img_path = $data['domain'] . "/upload/chat/" . $file_name;
$tumb_path = $data['domain'] . "/upload/chat/" . $file_tumb;
$create = imageTumb($source, $tumb, $type, 180);
if(file_exists($source) && file_exists($tumb)){
$check_tumb = getimagesize($tumb);
if ($check_tumb !== false) {
$myimage = tumbLinking($img_path, $tumb_path);
userPostChatFile($myimage, $file_name, 'image', $file_tumb);
}
else {
$myimage = linking($img_path);
userPostChatFile($myimage, $file_name, 'image');
}
}
else {
$myimage = linking($img_path);
userPostChatFile($myimage, $file_name, 'image');
}
echo 5;
die();
}
else {
echo 1;
die();
}
}
else if (isFile($extension)){
$file_name = encodeFile($extension);
move_uploaded_file(preg_replace('/\s+/', '', $_FILES["file"]["tmp_name"]), "../upload/chat/" . $file_name);
$myfile = $data['domain'] . "/upload/chat/" . $file_name;
$myfile = fileProcess($myfile, $origin);
userPostChatFile($myfile, $file_name, 'file');
echo 5;
die();
}
else if (isMusic($extension)){
$file_name = encodeFile($extension);
move_uploaded_file(preg_replace('/\s+/', '', $_FILES["file"]["tmp_name"]), "../upload/chat/" . $file_name);
$myfile = $data['domain'] . "/upload/chat/" . $file_name;
$myfile = musicProcess($myfile, $origin);
userPostChatFile($myfile, $file_name, 'music');
echo 5;
die();
}
else {
echo 1;
}
}
else {
echo 1;
}
?>
那么我如何创建一个函数来计算每个用户的消息并将其存储在 my_users 表的列中。
【问题讨论】: