I have a code that is suppose to display messages back and forth between the users. For some reason the data is mismatching and I'm not sure where in my code I have the error. I've tried to change my if statements of $x < $sender
but that didn't work.
https://gyazo.com/80b0b776f76b33c99402f373c021e790
but the data being displayed doesn't line up with the database information.
https://gyazo.com/cf61e6055fc4d68ac4923dd04029156c
"Test 2" message was sent from woof3, so it should be green and not blue when logged in as woof. And "test again" was sent from woof, so it should be blue when logged in as woof
Please help!
<?php
include "../Site/db.php";
$url = $_SERVER['REQUEST_URI'];
$urlArray = explode('=', $url);
$Username = $urlArray[sizeof($urlArray) - 1];
$Myself = $_SESSION['username'];
$sql = "SELECT * FROM messages ORDER BY `Id` ASC";
$result = $conn->query($sql);
// Header of message box
echo '<div id="messages" class="messagePersonBox shadow">
<span class="usersname" name="receiver"><a href="Account?='.$Username.'">'.$Username.'</a></span>
<hr style="margin-top:24px">
<div id="allMessages"class="allMessages">
';
// $sql2 = "SELECT * FROM messages WHERE Sender = '$Myself' ORDER BY `Id` ASC";
// $result2 = $conn->query($sql2);
$sql3 = "SELECT * FROM messages WHERE conversationID = '$Username$Myself' ORDER BY Id DESC";
$result3 = $conn->query($sql3);
$x = 0;
$sql4 = "SELECT * FROM messages WHERE Sender = '$Myself'";
$result4 = $conn->query($sql4);
$sql5 = "SELECT * FROM messages WHERE Receiver = '$Myself'";
$result5 = $conn->query($sql5);
$totalSender = mysqli_num_rows($result4);
$totalReceiver = mysqli_num_rows($result5);
// might have to add both totals for while loop
// to get total messages
$totalmessages = mysqli_num_rows($result);
// For receiving
while ($row = $result->fetch_assoc()) {
// output data of each row
if ($result3->num_rows > 0) {
if ($row = $result3->fetch_assoc()) {
$Sender2 = $row['Sender'];
$Message2 = $row['Message'];
$Receiver2 = $row['Receiver'];
$TimeSent2 = $row['MessageTime'];
}
}
if ($result3->num_rows > 0) {
if ($row = $result3->fetch_assoc()) {
$Sender = $row['Sender'];
$Message = $row['Message'];
$Receiver = $row['Receiver'];
$TimeSent = $row['MessageTime'];
}
}
// sending a message
if ($x < $totalSender) {
echo '<div id="messages2" class="messageDisplay">'.$Message.'<span style="float: right;margin-top:2px"><font size=1>'.time_elapsed_string($TimeSent).'</span></font></div>';
}
// receiving a message from someone else
if ($x < $totalReceiver) {
echo '<div id="messages2" class="messageDisplay2">'.$Message2.'<span style="float: right;margin-top:2px"><font size=1>'.time_elapsed_string($TimeSent2).'</span></font></div>';
}
$x++;
}
// Typing part of message
echo '</div>
<input type="text" id="messagetosend" name="message" class="messagetype" placeholder="Type a message..."><span id="send" class="messageSend">↩</span>
</div>
';
?>
I can add more info if needed.
Aucun commentaire:
Enregistrer un commentaire