1 回答
TA贡献1895条经验 获得超7个赞
首先把线去掉
echo "<br>";
从getLatestMessage
函数中 - 这会添加一个您不想要的新行。
其次,将该函数中的所有 echo 转换为字符串连接,然后从函数返回字符串 - 这使您可以更好地控制在何处使用它生成的结果。
第三,尝试在user_found_messages
div 内回显消息数据 - 因为 div 是块级元素,通常也会开始一个新行(除非 CSS 规则告诉它们不要这样做)。
所以,我想是这样的:
public function getLatestMessage ($userLoggedIn, $user2) {
$query = $this->con->prepare('SELECT body, user_to, date FROM messages WHERE
user_to = ? AND user_from = ? OR user_to = ? AND user_from = ? ORDER BY id DESC LIMIT 1');
$query->bind_param("ssss", $userLoggedIn, $user2, $user2, $userLoggedIn);
$query->execute();
//$query->bind_result($body, $user_to, $date);
$query_result = $query->get_result();
$msg = ""; //to hold the message data
if ($row = $query_result->fetch_assoc()) { //don't need a while here because we've limited the query to one record
//Timeframe
$date_time_now = date("Y-m-d H:i:s");
$start_date = new DateTime($row['date']); //Time of post
$end_date = new DateTime($date_time_now); //Current time
$interval = $start_date->diff($end_date); //Difference between dates
if($interval->y >= 1) {
if($interval->y == 1)
$time_message = $interval->y . "yr"; //1 year ago
else
$time_message = $interval->y . "yrs"; //1+ year ago
}
else if ($interval-> m >= 1) {
if($interval->d == 0) {
$days = " ago";
}
else if($interval->d == 1) {
$days = $interval->d . "d";
}
else {
$days = $interval->d . "d";
}
if($interval->m == 1) {
$time_message = $interval->m . "month";
}
else {
$time_message = $interval->m . "months";
}
}
else if($interval->d >= 1) {
if($interval->d == 1) {
$time_message = "Yesterday";
}
else {
$time_message = $interval->d . "d ";
}
}
else if($interval->h >= 1) {
if($interval->h == 1) {
$time_message = $interval->h . "hr";
}
else {
$time_message = $interval->h . "hrs";
}
}
else if($interval->i >= 1) {
if($interval->i == 1) {
$time_message = $interval->i . "m";
}
else {
$time_message = $interval->i . "m";
}
}
else {
if($interval->s < 30) {
$time_message = "Just now";
}
else {
$time_message = $interval->s . " seconds ago";
}
}
$msg .= ($row['user_to'] == $userLoggedIn) ? "They said: " : "You said: ";
$msg .= ' ';
$msg .= $body = $row['body'];
$msg .= ' ';
$msg .= $time_message ;
$msg .= ' ';
}
return $msg;
}
public function getConvos () {
$userLoggedIn = $this->user_obj->getUsername();
$return_string = "";
$convos = array();
$query = mysqli_query($this->con, "SELECT user_to, user_from FROM messages WHERE user_to='$userLoggedIn' OR
user_from='$userLoggedIn' ORDER BY id DESC");
while ($row = mysqli_fetch_array($query)) {
$user_to_push = ($row['user_to'] != $userLoggedIn) ? $row['user_to'] : $row['user_from'];
if(!in_array($user_to_push, $convos)) {
array_push($convos, $user_to_push);
}
}
foreach ($convos as $username) {
$user_found_obj = new User($this->con, $username);
$latest_message_details = $this->getLatestMessage($userLoggedIn, $username);
$return_string .= "<a href='messages.php?u=$username'><div class='user_found_messages'>
<img src='" . $user_found_obj->getProfilePic() .
"' style='border-radius: 5px; margin-right: 5px;'>
". $user_found_obj->getFirstAndLastName() ."<br/>".
$latest_message_details . "</div></a>";
}
return $return_string;
}
- 1 回答
- 0 关注
- 96 浏览
添加回答
举报