我在php中创建了一个简单的静态私人消息脚本,我希望发送者和接收者的消息有不同的颜色背景
$sql="SELECT message,timestamp from pm WHERE (from_user='".$_SESSION["username"]."' OR from_user='$touser') AND (to_user='$touser' OR to_user='".$_SESSION["username"]."') ORDER BY timestamp";
$ex=$conn->prepare($sql);
$ex->execute();
while($result=$ex->fetch(PDO::FETCH_ASSOC))
{
echo "<div class='message-view'>";
echo "<p class='subject'>".$result["message"]."</p>";
echo "</div>";
}
pm表具有以下属性(id,to_user,from_user,subject,message,timestamp)
答案 0 :(得分:0)
变化:
$sql="SELECT message,timestamp from pm WHERE (from_user='".$_SESSION["username"]."' OR from_user='$touser') AND (to_user='$touser' OR to_user='".$_SESSION["username"]."') ORDER BY timestamp";
到
$sql="SELECT message,timestamp,from_user from pm WHERE (from_user='".$_SESSION["username"]."' OR from_user='$touser') AND (to_user='$touser' OR to_user='".$_SESSION["username"]."') ORDER BY timestamp";
然后在特定课程期间检查,具体取决于谁是发件人:
if ($result['from_user'] == $_SESSION["username"])
echo '<div class="message-view-sender" style="background-color: yellow;">';
else
echo "<div class='message-view'>";