0

以下代码从表 CHART 和 COMMENTS 中从 MySQL 数据库中获取带有注释的消息(状态)。当您评论时,代码首先出现问题,评论出现在消息下方(这没关系,所有评论都必须在从最旧到最新排序的消息下方)。

但是当您再次发表评论时,它会创建一个新的对话消息和新评论,而不是在您发送的第一条评论下添加评论。有人可以帮我解决这个问题吗?或者有什么更好的方法来做到这一点?

           <?php
           $profile= htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8');
           $reply_acc= htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8');
           $result = mysqli_query($con, "SELECT ch.msg , ch.msg_id , co.comment , co.comment_id FROM chart AS ch, comments AS co WHERE ch.msg_id=co.comment_id");
           while($row = mysqli_fetch_array($result))
           {
           echo("
           <table border='1' width='600px'>
           <tr>
           <td>
           $row[msg]
           </td>
           </tr>

           <tr>
           <td>
           $row[comment]
           <p></p>
           <form action='drop_comment.php' method='post'>
           <input type='text' name='comment' placeholder='drop a comment...' value='' class='add_hook'>
           <input name='comment_id' type='hidden'  value='$row[msg_id]'>
           <input name='id' type='hidden'  value='$row[msg_id]'>
           <input name='comment_via' type='hidden'  value='$device'>
           <input name='comment_time' type='hidden'  value='$status_time'>
           </form>
           </td>
           </tr>
           </table>
           <p></p>
           ");
           }
           ?>
4

1 回答 1

0

按消息排序查询,然后发表评论。然后仅在消息更改为分组时打印消息。

       $result = mysqli_query($con, "SELECT ch.msg , ch.msg_id , co.comment , co.comment_id
                                     FROM chart AS ch
                                     JOIN comments AS co
                                     ON ch.msg_id=co.comment_id
                                     ORDER BY ch.msg_id, co.comment_id");

       $last_msg = null;
       while($row = mysqli_fetch_array($result))
       {
       if ($row['msg_id'] !== $last_msg) {
         if ($last_msg !== null) {
           echo "</table>\n<p><p>\n";
         }
         echo "<table border='1' width='600px'>
               <tr>
               <td>
               $row[msg]
               </td>
               </tr>\n";
         $last_msg = $row['msg_id'];
       }
       echo "
       <tr>
       <td>
       $row[comment]
       <p></p>
       <form action='drop_comment.php' method='post'>
       <input type='text' name='comment' placeholder='drop a comment...' value='' class='add_hook'>
       <input name='comment_id' type='hidden'  value='$row[msg_id]'>
       <input name='id' type='hidden'  value='$row[msg_id]'>
       <input name='comment_via' type='hidden'  value='$device'>
       <input name='comment_time' type='hidden'  value='$status_time'>
       </form>
       </td>
       </tr>
       ");
       }
       echo "</table>\n<p><p>\n";
于 2013-05-02T09:47:27.027 回答