在计数查询中不要多次显示多行?

时间:2014-01-18 03:53:19

标签: php count

我正在创建一个网站,用于评估所有事情的发生。 表: 披萨约翰 谷歌约翰 披萨哈利

让我们说你喜欢谷歌和披萨。它将显示如下: 约翰,约翰,哈利。

我希望它看起来像这样: 约翰,哈利。

我做错了什么?这是我的代码。它与while循环中的while循环有什么关系吗?我怎么能让它看起来像我想要的那样......我无法理解它。

$people_like = mysqli_query ($link,    
    "SELECT user_liked_by FROM ( 
        SELECT COUNT(user_liked_by) AS total, id, user_liked_by
        FROM whatilike WHERE whats_liked LIKE '%$thing_liked%' &&
            user_liked_by !='$user'
        GROUP BY user_liked_by
        ORDER BY COUNT(user_liked_by) DESC ) as names;
    ");

while ($rpl = mysqli_fetch_assoc ($people_like)) {
    $person_like_u = $rpl ['user_liked_by'];
    $pluq = mysqli_query ($link, "SELECT profile_pic FROM users WHERE 
                                    username='$person_like_u'");

    while ($pluqr = mysqli_fetch_assoc ($pluq)) {
        $pli = $pluqr ['profile_pic'];
        echo "<a href='profile.php?u=$person_like_u'  >
            <img src='data/user_photos/$pli' height='50px' width='50px'
                    style='border-radius:400px;' id='ioplu' ></a>";
    }
}

1 个答案:

答案 0 :(得分:0)

$people = [];    
while ($rpl = mysqli_fetch_assoc ($people_like)) {
  $people[] = $rpl ['user_liked_by'];
}

$people = array_unique(array_map("trim",$people));
foreach($people as $p){
  $pluq = mysqli_query ($link, "SELECT profile_pic FROM users WHERE 
                                username='$p'");

  while ($pluqr = mysqli_fetch_assoc ($pluq)) {
    $pli = $pluqr ['profile_pic'];
    echo "<a href='profile.php?u=$person_like_u'  >
        <img src='data/user_photos/$pli' height='50px' width='50px'
                style='border-radius:400px;' id='ioplu' ></a>";
  }
}