2014-09-01 64 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