gpt4 book ai didi

php - 将头像连接到聊天消息

转载 作者:行者123 更新时间:2023-11-29 20:48:27 26 4
gpt4 key购买 nike

我想将用户头像连接到他们在聊天系统中发送的消息。目前,我已经取得了聊天中显示的每个用户的图像 name.jpg (含义:我有每个用户的 ID 来索引)

enter image description here

我现在需要能够拥有它,以便使用以下代码在聊天中显示为头像

<?php echo  "<img width='100' height='100' src='avatars/".$row['imagelocation']."' alt='Profile Pic'>"; ?>

我想将头像显示在 $new_str 的范围内,这是要发送的消息

    <div id="chat_data">

<span style ="color:orange; font-size:10px;"><?php echo "<a href=http://localhost/db/view_member.php?id=".$row['username'].">My Profile </a>"?><span style ="color:o; font-size:24px;"><?php echo " |".$row['username']." says :"; ?></span></span>

**//LINE I WANT TO ADD THE AVATAR TO**
<span style ="color:white; font-size:20px;"><?php echo $new_str ;?></span>

<span style ="float:right; color:brown;" ><?php echo $row['date']; ?></span>

//THIS LINE DISPLAYS THE IMAGE BUT ON ALL LINES IN THE CHAT
<span style ="float:right; color:brown;" ><?php echo $row['imagelocation']; ?></span>


</div>

上面的第三个跨度标签显示用户头像,但在所有消息上我的查询是从表中选择所有内容,以便所有用户都可以看到消息,我如何使我的查询在每一行上显示每个用户的头像他们发送的消息(我使用 ID 作为每个用户的索引),但我一直试图在该用户的消息的每一行上显示头像

如果我在下面添加一个 WHERE 子句来索引用户的 ID,聊天将不会向聊天中的所有其他用户显示所有内容。

$query = "SELECT * FROM chat ORDER BY id DESC";
$run = $con->query($query);
while($row = $run->fetch_array()):
$new_str = str_replace($row['msg']);



?>

我的表结构 AVID 是用户 ID 表聊天

enter image description here

下面是我的查询,显示了我如何获取 ID 和图像路径,然后更新表 tbl1

 if(isset($_POST['submit'])){  // RETRIVE + UPLOAD THE IMAGE AVATAR FOR THE USER IF THEY SUBMIT A IMAGE TO UPLOAD
move_uploaded_file($_FILES['file'] ['tmp_name'],"avatars/".$_FILES['file']['name']);
include('..\db.php');
$con = mysqli_connect($dbsrvname, $dbusername, $dbpassword, $dbname);
$q = mysqli_query($con,"UPDATE tbl1 SET imagelocation = '".$_FILES['file']['name']."' WHERE username = '".$_SESSION['CurrentUser']."'");
// $me = mysqli_query($con,"UPDATE chat SET imagelocation = '".$_FILES['file']['name']."' WHERE username = '".$_SESSION['CurrentUser']."'");
}
}

注意安全性尚未应用于头像上传选项

enter image description here

<form>
<textarea name="msg" placeholder="enter message"></textarea>
<input type="submit" name="submit" value="Send"/>
</form>

<?php
if(isset($_SESSION['CurrentUser'])){ // if session is true
if(isset($_POST['submit'])){
$name = $_SESSION['CurrentUser'];
$msg = $_POST['msg'];

// GET THE NAME OF FILE FOR THE PATH
$q = mysqli_query($con,"SELECT * FROM tbl1 WHERE username = '".$_SESSION['CurrentUser']."'");
while($row = mysqli_fetch_assoc($q)){
echo $row['imagelocation'];
$path = $row['imagelocation'];


}

////////// SELECT FROM TBL1 TO GET ID OF USER (separate query )
$q = mysqli_query($con,"SELECT * FROM tbl1 WHERE username = '".$_SESSION['CurrentUser']."'");
while($row = mysqli_fetch_assoc($q)){
echo $row['id'];
$onlineid = $row['id'];
}



////////// INSERT USERNAME MSG AND TIME + ID OF USER NAME OF FILE
$query = "INSERT INTO chat (username,msg,date,avid,imagelocation) values ('$name','$msg', now(),'$onlineid','$path')";
$run = $con->query($query);

$me = mysqli_query($con,"UPDATE chat SET imagelocation = '".$path."' WHERE username = '".$_SESSION['CurrentUser']."'");


}
}
else {
echo "You Need to Login to type in this chat";
}
?>

注意:安全性尚未应用于我的查询 提前致谢

最佳答案

我知道这与您的方式不同,但它仍然以更简单的方式给出相同的结果。

只要您拥有用户唯一的id,您就可以创建一个可以从任何地方调用的函数。像这样:

function get_user($user, $field) {
// run your db connection here with $con as your db variable, remember we are using `mysqli` and not `mysql`
$query = mysqli_query ($con, "SELECT $field FROM users_table WHERE id='$user'");
$sql = mysqli_fetch_array($query);
$value = $sql[$field];
return $value;
}

这样,只要你有id,你不仅可以获得用户图像,还可以获得用户表中用户的每个值,所以你可以说:

$image = get_user($user_id, 'imagelocation');
$name = get_user($user_id, 'username');

等等。我通常会在外部页面中运行此函数,我总是将其包含在所有页面中。

希望这有帮助

关于php - 将头像连接到聊天消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38251429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com