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.
but the data being displayed doesn't line up with the database information.
"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
Edit working code down below, no pdo because I'm not done with this project and I save those for the end to make testing easier.
<?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 (Sender = '$Myself' AND Receiver = '$Username')
OR (Receiver = '$Myself' AND Sender = '$Username') ORDER BY `MessageTime` ASC";
$result3 = $conn->query($sql3);
print_r($result3);
$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 ($x < $totalmessages) {
// output data of each row
if ($result3->num_rows > 0) {
if ($row = $result3->fetch_assoc()) {
$Sender = $row['Sender'];
$Message = $row['Message'];
$Receiver = $row['Receiver'];
$TimeSent = $row['MessageTime'];
}
}
$class = $Sender == $Myself ? 'messageDisplay' : 'messageDisplay2';
// sending a message
echo '<div id="messages2" class="'.$class.'">'.$Message.'<span style="float: right;margin-top:2px"><font size=1>'.time_elapsed_string($TimeSent).'</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.