2014-09-01 44 views
0

如何在帖子表中显示热门标签(#)?如何在帖子表中显示流行的标签(#)?

我有1周后= tb_post

post_id | post   | uid 
1  | blabla #one | 01 
2  | wew #two  | 01 
3  | nice #one  | 02 
4  | great #one  | 02 
5  | excellent #one | 01 

-

  1. 怎么能算我是#标签?
  2. 在那个例子中,我们知道#one将会是最高的hashtag。如何显示顶部#hashtag < - 只有hashtag不包含其他文本。例如:

    TOP HASHTAG : #one 
    

我的查询到目前为止:

SELECT DISTINCT post 
FROM tb_post 
WHERE post LIKE '%#%' 
GROUP BY post 
+2

有没有在每个'POST'只有一个主题标签? – Barmar 2014-09-01 10:29:53

+0

这取决于用户更新帖子。所以我不能说它只有一个#标签 – hiDayurie 2014-09-01 10:32:09

回答

3

我不小心在点击后的答案。 downvotes可以等到我完成吗?

在PHP中,得到的post中的所有值的数组,这样你才会有:

Array (
    blabla #one 
    wew #two 
    nice #one 
    great #one 
    excellent #one 
) 

现在,使用implode(" ", $posts)加入所有的数组项,这给你一个字符串:

blabla #one wew #two nice #one great #one excellent #one 

分割使用explode(" ", $posts)。现在,检查以#开头的值。将它们添加为数组索引并增加计数!现在,你有增加计数的代码!现在,你可以这样做:

<?php 
$posts = explode(" ", "blabla #one wew #two nice #one great #one excellent #one"); 
$rank = array(); 
foreach ($posts as $post) 
    if (strpos("#", $post)) 
    if (!isset($rank[$post])) 
     $rank[$post] = 1; 
    else 
     $rank[$post]++; 
?> 

甚至可以通过这样的方式来使用功能array_count_values($hashes)

<?php 
$posts = explode(" ", "blabla #one wew #two nice #one great #one excellent #one"); 
$hashes = array(); 
foreach ($posts as $post) 
    if (strpos("#", $post)) 
    $hashes[] = $post; 
?> 

而现在使用array_sort()或东西由降序对数组进行排序,并用它排名!

Downvoters,你能检查并恢复那些吗?

+1

这是如何告诉你最流行的标签? – Barmar 2014-09-01 10:29:26

+0

也许使用'array_count_values'而不是自己计算它们? – Barmar 2014-09-01 10:31:16

+0

@Barmar看看这个! – 2014-09-01 10:37:06

相关问题