我已经启动了这个消息系统。在上图中,你看到 Danny 给我发了 3 条消息,Bibby 给我发了 2 条消息。但是我只希望名字显示一次,所以基本上名字就像一个类别,所以当我点击名字时,我会得到那个人的所有消息?
提前致谢
我认为有两种方法可以从数据库中检索唯一记录,
一种是通过使用DISTINCT
SELECT DISTINCT Name
FROM tableName
另一种方法是使用GROUP BY
SELECT Name
FROM tableName
GROUP BY Name
try this
$sql = mysql_query("SELECT * dm WHERE torn='$session_rand' GROUP BY fromuser ");
$count = mysql_num_rows($sql);
while($row = mysql_fetch_array($sql))
{ $sendername = $row["fromuser"];
echo '<div class="eachuser" style="background-color:#FFF; margin-bottom:1px; padding:7px; font-size:12px; text-align:center;"> ' .$sendername. ' </div>'; }
class="eachuser"
and in the same time you have inner style ? put those
background-color:#FFF; margin-bottom:1px; padding:7px; font-size:12px; text-align:center;
in the eachuser
class and remove them from inside the div
您可能想要做的(如果您不想遵循@JW. 修改数据库查询的示例),是创建一个数组来存储您已经显示过一次的名称,并在每个实例中对其进行检查循环遍历结果集。
// the result set coming from the database - all senders
$senders = array('danny', 'danny', 'danny', 'bibby', 'bibby', 'danny', 'bibby');
// the empty array to store the already displayed senders
$displayed = array();
// loop through each sender
foreach($senders as $sender) {
// if the sender is not in the array (therefore not yet displayed)
if(!in_array($sender, $displayed) {
echo $sender . "<br />";
$displayed[] = $sender;
}
}
这个循环应该只回显每个名称一次。