【问题标题】:PHP show the most popular tagsPHP 显示最流行的标签
【发布时间】:2023-03-23 09:03:02
【问题描述】:

我有一个这样的数据库:

+----+---------------------+
| id | tags                |
+----+---------------------+
| 1  | test1, test2, test3 |
| 2  | test1, test2, test3 |
| 3  | test1, test2, test3 |
| 4  | test1, test2, test3 |
| 5  | buh1, buh2, buh3    |
+----+---------------------+

现在我想显示这个数据库中最流行的标签。我有一个函数,它适用于这样的数组:

$tag_array = 数组( '测试1,测试2测试,测试3', '测试2,测试4,测试2', '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;
}

这是如何显示最受欢迎的标签:

foreach(popularTags($tag_array) as $tag=>$num)
{
    echo $tag, " (", $num, ")<br />";
}

到目前为止,这适用于普通数组。

现在,我想从数据库中获取标签,所以我从数据库中提取值并运行如下函数:

$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, ")<br />";
}

}

这给了我一个错误:

警告:为 foreach() 提供的参数无效

所以我的问题是如何用这个函数显示数据库中最流行的标签?

谢谢

【问题讨论】:

    标签: php mysql function tags


    【解决方案1】:

    我的建议是你 normalize 你的数据库。然后像这样的查询变得微不足道,而且性能更好。

    select TagID, count(*)
    from EntityTag
    group by TagID
    order by count(*) descending
    limit 5
    

    【讨论】:

      【解决方案2】:

      mysql_fetch_array 返回所有行。这样做:

      $rows = mysql_fetch_array($result);
      foreach($rows as $row) {
          $tag_array = $row["tags"]; // note removing the $
          foreach(...) {
          }
      }
      

      【讨论】:

      • 它以某种方式工作,但它不显示任何标签,而是显示如下内容:(1) (2) (3) (4) (5) (6) (7) (8) (9) (10)
      • 抱歉我回复的太仓促了。我会在一分钟内解决这个问题。
      • 谢谢您,您的新回复出错:-) Warning: preg_split() [function.preg-split]: No ending delimiter ',' found in
      • 我还是不行,我试试这样:$rows = mysql_fetch_array($result); foreach($rows as $row) { $tag_array = $row["tags"]; // note removing the $ foreach(popularTags($tag_array) as $tag=&gt;$num) { echo $tag, " (", $num, ")&lt;br /&gt;"; } }
      • 它给了我这个错误:Warning: Invalid argument supplied for foreach()
      【解决方案3】:

      这个对我有用:

      $result = mysql_query("select tags from DATABASE LIMIT 20");
      
      $tags = array();
      
      while ($row = mysql_fetch_array($result)) {
          $row_tag_array = split(",", $row[0]);
          foreach ($row_tag_array as $newtag) {
          asort($row_tag_array);
              if (array_key_exists($newtag, $tags)) {
                  if ($tags[$newtag] < 200) {
                      $tags[$newtag] = $tags[$newtag] + 20;
      
                  }
              }
              else {
                  $tags[$newtag] = 100;
              }
          }
      }
      
      foreach ($tags as $tag => $size) {
          echo "<a style=\"font-size: $size%;\" href=\"?t=$tag\">$tag</a> ";
      
      }
      

      谢谢你的帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多