dousong9729 2016-07-07 17:08
浏览 44

将头像连接到聊天消息

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).

enter image description here

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

enter image description here

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

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";
 }
?>

Note : Security has not yet been applied to my query's Thanks in advance

  • 写回答

2条回答 默认 最新

  • du4373 2016-07-07 17:44
    关注

    I know this is a different way from yours but it still gives the same results and in an easier way.

    You can create a function which you can call from anywhere as long as you have the users unique id. Like this:

    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;
    }
    

    This way you cannot only get the users images but also every value of the user in the users table as long as you have the id, so you can say:

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

    And so on and forth. What I normally do I run this function in an external page which I always include in all my pages.

    Hope this helps

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度