php热门标签,PHP显示最流行的标签
我有這樣一個(gè)數(shù)據(jù)庫:
+----+---------------------+
| id | tags |
+----+---------------------+
| 1 | test1, test2, test3 |
| 2 | test1, test2, test3 |
| 3 | test1, test2, test3 |
| 4 | test1, test2, test3 |
| 5 | buh1, buh2, buh3 |
+----+---------------------+
現(xiàn)在我想顯示此數(shù)據(jù)庫中最受歡迎的標(biāo)簽.我有一個(gè)函數(shù),它適用于這樣的數(shù)組:
$tag_array = array(
‘test1, test2 test, test3’,
‘test2, test4, test2’,
‘buh, buh2, buh3’ );
功能:
function popularTags($tag_array) {
$p = array();
foreach($tag_array as $tags) {
$tags_arr = array_map('trim', explode(',', $tags));
foreach($tags_arr as $tag) {
$p[$tag] = array_key_exists($tag, $p) ? $p[$tag]+1 : 1;
}
}
arsort($p);
return $p;
}
這是顯示最流行的標(biāo)簽的方法:
foreach(popularTags($tag_array) as $tag=>$num)
{
echo $tag, " (", $num, ")
";
}
到目前為止,這是正常的數(shù)組.
現(xiàn)在,我想從數(shù)據(jù)庫中獲取標(biāo)簽,所以我從數(shù)據(jù)庫中提取值并運(yùn)行如下函數(shù):
$result = mysql_query("select * from DB ORDER BY date DESC");
while($row = mysql_fetch_array($result)){
$tag_array = $row["$tags"];
foreach(popularTags($tag_array) as $tag=>$num)
{
echo $tag, " (", $num, ")
";
}
}
這給了我一個(gè)錯(cuò)誤:
Warning: Invalid argument supplied for foreach()
所以我的問題是如何使用此功能顯示數(shù)據(jù)庫中最受歡迎的標(biāo)簽?
謝謝
解決方法:
我的建議是你的數(shù)據(jù)庫是normalize.然后像這樣的查詢變得微不足道,并且表現(xiàn)要好得多.
select TagID, count(*)
from EntityTag
group by TagID
order by count(*) descending
limit 5
標(biāo)簽:php,function,mysql,tags
來源: https://codeday.me/bug/20190726/1541714.html
總結(jié)
以上是生活随笔為你收集整理的php热门标签,PHP显示最流行的标签的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 语音怎么进入滤波器matlab,基于Ma
- 下一篇: php 值不进行解码,无法解码PHP中的