dqdmvg7332 2018-03-09 18:44
浏览 79

回复部分的逻辑如何与PHP和MySQL一起使用?

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&nbsp;</p>
                                <p class="bullet left-replies">&bull;</p>
                                <p class="show-replies-link left-replies">&nbsp;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.

  • 写回答

1条回答 默认 最新

  • dqy92287 2018-03-09 18:58
    关注

    @IncredibleHat is correct on both points. You should be able to drop the GROUP BY and get the same results. Also, the PHP portion of the code would look like this:

    <?
       $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'];
    
            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&nbsp;</p>
                                    <p class="bullet left-replies">&bull;</p>
                                    <p class="show-replies-link left-replies">&nbsp;Show All Replies</p>
                                </div>';
    
    
            //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="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>';
                        }
                    }
    
                              echo '<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>';
                }
            }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler