I am using PHP and created a pretty sophisticated messaging system. But my problem is it will not show the first message, until it is "pushed" up. If i go to someone's profile and message them, the first message will not show, but if I send another the first one shows but the second one doesn't...until I send a third message, then the first and second are shown and so on and so forth.
I have tested it and it doesnt appear to be the css or MySQL. The text is actually being shown and the database is storing each message the second that it is sent. I even send 1 message, go to home, and then check if it was sent, and it was but it wont display until the 2nd one is sent. The database also shows the message data being stored and it's there and everything.
Is there a way to PUSH the row in my php output forward by one? So that it is on track?
This code below checks for any messages between the two users from the database and stores them into an array if they exist.
$query = "select * from messages INNER JOIN users u on u.userid = messages.createduserid
where (createduserid='$user' && recuserid='$recuser') OR (createduserid='$recuser' && recuserid='$user')
ORDER BY createddate DESC limit 6 " or die("Error in the consult.." . mysqli_error($connection));
$result = $connection->query($query);
$row = mysqli_fetch_array($result);
This takes the array created above and out puts it for the users to see
while ($profilepreviewlist = $result->fetch_assoc()) {
echo '<div class="previewlistitem">';
echo'<p class="messagetext">';
echo $profilepreviewlist['username'],': ';
echo $profilepreviewlist['body'];
echo'<div class="messagedate">';
echo ($profilepreviewlist["createddate"]);
echo'</div>';
echo'</p>';
echo '</div>';
}
?>
When this button is pressed, the message is sent and it runs the code below it
<input class="input" name="messageplace" placeholder="Type a message here" id="messageplace">
This code stores the data from their message sending it to the database, it then refreshes the message page
$userid = $_SESSION['userid'];
$recuserid = $_SESSION['tempuserid'];
$body=$_POST['messageplace'];
$datee = new DateTime("NOW");
$createddate = $datee->format('Y-m-d H:i:s');
send_message($userid,$recuserid,$body,$createddate);
include('message.php');
If you are interested in testing the feature out, let me know
When you call $row = mysqli_fetch_array($result)
you are accessing the first record that your SQL query returned and advancing the cursor of $result
to the next record. Therefore, when you call:
while ($profilepreviewlist = $result->fetch_assoc()) {
//do whatever...
}
You won't have access to the first record in the set because you have already processed it - it's stored in $row
.
To fix the problem, remove $row = mysqli_fetch_array($result)
. By removing it you are no longer processing the record and will have access to it within the while loop.