MySQL PHP显示对话中按父记录分组的所有消息

MySQL PHP显示对话中按父记录分组的所有消息,php,mysql,Php,Mysql,我正在使用mysqli\u fetch\u assoc将查询结果回显到我的页面。不幸的是,这给了我一对一的关系 以下是它们当前的显示方式: Listing Name Thread ID Guest Name Sent by UserID Message Spacious Bedroom 338082922 Daniel 43762688 I can't, I'm sorry. I won't even be around at the end of Ju

我正在使用mysqli\u fetch\u assoc将查询结果回显到我的页面。不幸的是,这给了我一对一的关系

以下是它们当前的显示方式:

Listing Name    Thread ID   Guest Name  Sent by UserID  Message     
Spacious Bedroom    338082922   Daniel  43762688    I can't, I'm sorry. I won't even be around at the end of July.  
Spacious Bedroom    338082922   Daniel  98936811    Are you willing to rent it out at the end of july   
Spacious Bedroom    338082922   Daniel  43762688    Sorry, no room  
Spacious Bedroom    338082922   Daniel  98936811    Hello, I'd like to stay Thanks -Dan     
我想显示一次列表名、一次线程ID、一次来宾名,然后在旁边显示两个用户之间的对话

我希望将对话分组在列表、线程和来宾下

以下是我目前所做的并没有达到预期效果的事情:

<table>
      <tr>
        <th style="text-align: left; width: 300px;">Listing Name</th>
        <th style="text-align: left; width: 100px;">Thread ID</th>
        <th style="text-align: left; width: 100px;">Guest Name</th>
        <th style="text-align: left; width: 100px;">Sent by User ID</th>
        <th style="text-align: left; width: 300px;">Message</th>
      </tr>
            <?php while($message = mysqli_fetch_assoc($message_set)) { ?>
      <tr>
        <td><?php echo htmlentities($message["listingname"]); ?></td><td><?php echo htmlentities($message["thread_id"]); ?></td><td><?php echo htmlentities($message["guest_name"]); ?></td><td><?php echo htmlentities($message["sent_by"]); ?></td><td><?php echo htmlentities($message["msg_body"]); ?></td>
      </tr>
            <?php } ?>
        </table>

清单名称
线程ID
客人姓名
由用户ID发送
消息

诀窍是跟踪当前列表名。如果更改,请打印,否则不打印任何内容。逻辑如下:

$name='';
…
if($message["listingname"]==$name) echo '';
else echo $name=$message["listingname"];
在这里,我们

  • 将变量初始化为
    '
  • 如果新名称匹配,则回显
    '
  • 否则,将重复新名称

echo$a=$b
构造复制$b int$a,然后打印结果。

我可能会设计一些不同的表,以便将消息放在一个单独的表中,并将
线程ID
作为外键。如果一个列表可以有多个线程,甚至可能有三个表。所以:列表、线程和消息。然后你可以抓取一次线程(和列表),然后抓取该线程的所有消息。难道没有办法使用嵌套循环或使用mysql将表连接到自身吗?你可以循环结果并将消息存储在数组中,然后最终显示该数组的内容(连同第一个结果行中的列表名等,如果它们完全相同)谢谢,我会尝试一下。但是线程ID可以在每个列表名中更改。换句话说,每个列表可以有许多线程,每个线程可以有许多消息