如何删除重复项并仅显示一次?

时间:2012-10-04 07:21:47

标签: php arrays

我试图停止在数组中显示重复并显示一次。我已经尝试使用array_unique()但文本不会出现,或者我只是错误地使用它。

include '../../../cpages/cmain/func/init.php';
$result = mysql_query("SELECT * FROM `message` WHERE `to` ='".$user_data['username']."' ");
while ($row =  mysql_fetch_array($result)) {
    $m_id = $row['id'];
    $m_to = $row['to'];
    $m_from = $row['from'];
    $m_text = $row['text'];
    $d_open = $row['open'];
    $d_date = $row['date'];
    $getpro = mysql_fetch_assoc(mysql_query("SELECT * FROM `users` WHERE `username` ='$m_from' "));
    $pro = $getpro;
    $first_name =$pro['first_name'];
    $last_name =$pro['last_name'];
    $pro_pic = $pro['profile_pic'];
    //profile picture
    $profile_pic = $pro_pic;
  if ($profile_pic == "") {
  $profile_pic = "../cpages/img/default_pic.jpg";
  }
  else
  {
  $profile_pic = "../userdata/".$m_from."/profilepic".$profile_pic;
  }
  //end

  // echo messages
echo '<div><a style="border-style:none;" href="vmge.php?msge='.$m_id.'"><img   src="'.$profile_pic.'" style="border-style:none;" height="50" width="50"  />('.$m_from.')'.ucwords($first_name).'&nbsp'.ucwords($last_name).'<br/>'.$m_text.'</div></a>';
}

1 个答案:

答案 0 :(得分:0)

在查询中使用DISTINCT关键字:

result = mysql_query("SELECT DISTINCT * FROM `message` WHERE `to` ='".$user_data['username']."' ");