I would like to concatenate a users avatar to a message they send in a chat system . At the moment I have progressed to the point where I have the image name.jpg for each user showing up in the chat (MEANING: I have an ID for each user to INDEX).
I need to be able now have it so that it display as avatar in the chat with the following code
<?php echo "<img width='100' height='100' src='avatars/".$row['imagelocation']."' alt='Profile Pic'>"; ?>
I want to display the avatar here wrapped in the span to $new_str which is the message to be sent
<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>
The 3rd span tag above displays the users avatar but on all messages My query is selecting everything from the table so that all users can see the messages, How can I make my query to show and avatar for each user on each line of a message they send (Im using ID as an INDEX for each user) but I'm stuck trying to display the avatar on each line of a message for that user
If I include a WHERE CLAUSE below to index the ID for the user the chat wont display everything to all other users in the chat.
$query = "SELECT * FROM chat ORDER BY id DESC";
$run = $con->query($query);
while($row = $run->fetch_array()):
$new_str = str_replace($row['msg']);
?>
My TABLE STRUCTURE AVID IS THE USERS ID table chat
Below are my query's showing how im getting the ID and image path and then UPDATE table 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']."'");
}
}
Note Security has not yet been applied to the avatar upload option
<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";
}
?>
Note : Security has not yet been applied to my query's Thanks in advance