I am trying to make a reply section to a message forum in my website. Below is my code that will be referenced in pieces to let readers fully understand where I'm at and what my question is (I realize this post is lengthy for the viewer, but I did my best to be accurate and detailed as possible).`
<?
$sqlMessage = "SELECT * FROM messages GROUP BY message_number";
$resultMessage = mysqli_query($conn, $sqlMessage);
if(mysqli_num_rows($resultMessage) > 0) {
while($rowMessagesDetails=mysqli_fetch_assoc($resultMessage)) {
$messageUsername = $rowMessagesDetails['username'];
$messageMessage = $rowMessagesDetails['message'];
$message_number = $rowMessagesDetails['message_number']; // using this number for the next part to find the replies
$messageDate = $rowMessagesDetails['date'];
//Getting ALL replies that match the message_number in the MESSAGES table
$sqlReply = "SELECT * FROM replies WHERE message_number='$message_number' ORDER BY date DESC";
$resultReply = mysqli_query($conn, $sqlReply);
if(mysqli_num_rows($resultReply) > 0) {
while($rowReplyDetails=mysqli_fetch_assoc($resultReply)) {
$replyUsername = $rowReplyDetails['username'];
$replyMessage = $rowReplyDetails['message'];
$replyDate = $rowReplyDetails['date'];
echo '<div class="user-details-box">
<div class="user-username">'.$messageUsername.'</div>
<div class="user-date">'.$messageDate.'</div>
</div>
<div class="user-message">'.$messageMessage.'</div>
<div class="reply-section">
<div class="replies">
<p class="reply-link-button left-replies">Reply </p>
<p class="bullet left-replies">•</p>
<p class="show-replies-link left-replies"> Show All Replies</p>
</div>
<div class="reply-details-box">
<div class="user-replies-box">
<div class="reply-username">'.$replyUsername.'</div>
<div class="reply-date">'.$replyDate.'</div>
</div>
<div class="reply-message">'.$replyMessage.'</div>
</div>
<form class="open-textbox" id="reply-form" action="reply.php" method="post">
<input type="hidden" class="message_number" name="message_number" value="'.$message_number.'">
<div class="right-reply">
<textarea id="getReply" class="insert-reply" name="reply" placeholder="Reply."></textarea>
<div class="right-reply-btn">
<button id="reply-link" class="reply-link" type="submit" name="submit">Reply</button>
</div>
</div>
</form>
</div>';
}
}
}
}
?>`
The problem is, I'm trying to figure out what I need to do to query replies specifically in reference to the user that posted the comment; and have the replies stack underneath. I have made a REPLIES and MESSAGES table that each have a message_number that does this (Explaining with example input):
MESSAGES TABLE
message_number: 1 //useful primary that we'll use in the REPLIES table
username: BuzzLightyear2018
message: Star Command do you read me?
date: 3/9/2018
REPLIES TABLE
reply_id: 1 //random primary to keep rows unique
message_number: 1 //foreign key from MESSAGES that connect the message with the reply
username: CowboyWoody2018
reply: No, but I've got a stick in my boot!
date: 3/9/2018
How do I match the message_number from the REPLIES table to the message_number in the MESSAGES table and show all replies underneath the message here:
<div class="reply-details-box">
<div class="user-replies-box">
<div class="reply-username">**username from REPLIES here**</div>
<div class="reply-date">**date from REPLIES here**</div>
</div>
<div class="reply-message">**message from REPLIES here**</div>
</div>
Just to reiterate: I want all replies to stack underneath the message from MESSAGES; all I keep getting when trying to querying $sqlMessage = "SELECT * FROM messages GROUP BY message_number" and $sqlReply = "SELECT * FROM replies WHERE message_number='$message_number' ORDER BY date DESC" is shown in the image below:
This shows the same message twice because it's querying each reply to each message Any suggestions? Thank you in advance.