【问题标题】:What's the best way to generate a tag cloud from an array using h1 through h6 for sizing?使用 h1 到 h6 从数组生成标签云的最佳方法是什么?
【发布时间】:2010-09-05 05:46:06
【问题描述】:

我有以下数组:

$artist = array("the roots", "michael jackson", "billy idol", "more", "and more", "and_YET_MORE");
$count = array(5, 3, 9, 1, 1, 3);

我想生成一个标签云,其中$count 中编号较高的艺术家包含在h6 标签中,而h1 标签中包含的最低编号。

【问题讨论】:

    标签: php arrays tag-cloud


    【解决方案1】:

    您也需要向其添加对数函数。 (取自我的 Drupal 模块 tagadelic,用于创建标签云 http://drupal.org/project/tagadelic):

    db_query('SELECT COUNT(*) AS count, id, name FROM ... ORDER BY count DESC');
    
    $steps = 6;
    $tags = array();
    $min = 1e9;
    $max = -1e9;
    
    while ($tag = db_fetch_object($result)) {
        $tag->number_of_posts = $tag->count; #sets the amount of items a certain tag has attached to it
        $tag->count = log($tag->count);
        $min = min($min, $tag->count);
        $max = max($max, $tag->count);
        $tags[$tag->tid] = $tag;
    }
    // Note: we need to ensure the range is slightly too large to make sure even
    // the largest element is rounded down.
    $range = max(.01, $max - $min) * 1.0001;
    
    foreach ($tags as $key => $value) {
        $tags[$key]->weight = 1 + floor($steps * ($value->count - $min) / $range);
    }
    

    然后在您的视图或模板中:

    foreach ($tags as $tag) {
        $output .= "<h$tag->weight>$tag->name</h$tag->weight>"
    }
    

    【讨论】:

      【解决方案2】:

      在我的头顶上......

      $artist = array("the roots","michael jackson","billy idol","more","and more","and_YET_MORE");
      $count = array(5,3,9,1,1,3);
      $highest = max($count);
      for (int $x = 0; $x < count($artist); $x++)
      {
          $normalized = $count[$x] / $highest;
          $heading = ceil($normalized * 6); // 6 heading types
          echo "<h".$heading.">".$artist[$x]."</h".$heading.">";
      }
      

      【讨论】:

        【解决方案3】:

        也许这有点学术和离题,但由于文档结构等原因,hX 标签可能不是标签云的最佳选择。

        也许spans 或ol 具有适当的类属性(加上一些CSS)?

        【讨论】:

          【解决方案4】:

          使用这个 sn-p 有一段时间了,感谢 prism-perfect.net。虽然不使用 H 标签

          <div id="tags">
              <div class="title">Popular Searches</div>
              <?php
                  // Snippet taken from [prism-perfect.net]
          
                  include "/path/to/public_html/search/settings/database.php";
                  include "/path/to/public_html/search/settings/conf.php";
          
                  $query = "SELECT query AS tag, COUNT(*) AS quantity
                  FROM sphider_query_log
                  WHERE results > 0
                  GROUP BY query
                  ORDER BY query ASC
                  LIMIT 10";
          
                  $result = mysql_query($query) or die(mysql_error());
          
                  while ($row = mysql_fetch_array($result)) {
          
                      $tags[$row['tag']] = $row['quantity'];
                  }
          
                  // change these font sizes if you will
                  $max_size = 30; // max font size in %
                  $min_size = 11; // min font size in %
          
                  // get the largest and smallest array values
                  $max_qty = max(array_values($tags));
                  $min_qty = min(array_values($tags));
          
                  // find the range of values
                  $spread = $max_qty - $min_qty;
                  if (0 == $spread) { // we don't want to divide by zero
                      $spread = 1;
                  }
          
                  // determine the font-size increment
                  // this is the increase per tag quantity (times used)
                  $step = ($max_size - $min_size)/($spread);
          
                  // loop through our tag array
                  foreach ($tags as $key => $value) {
          
                      // calculate CSS font-size
                      // find the $value in excess of $min_qty
                      // multiply by the font-size increment ($size)
                      // and add the $min_size set above
                      $size = $min_size + (($value - $min_qty) * $step);
                      // uncomment if you want sizes in whole %:
                      // $size = ceil($size);
          
                      // you'll need to put the link destination in place of the /search/search.php...
                      // (assuming your tag links to some sort of details page)
                      echo '<a href="/search/search.php?query='.$key.'&search=1" style="font-size: '.$size.'px"';
                      // perhaps adjust this title attribute for the things that are tagged
                      echo ' title="'.$value.' things tagged with '.$key.'"';
                      echo '>'.$key.'</a> ';
                      // notice the space at the end of the link
                  }
              ?>
          </div>
          

          【讨论】:

          • 这对我来说似乎是个好方法。如果您的数据在数组中,只需跳过数据库部分。我建议您将艺术家姓名和计数存储在单个关联数组中。要使用上面的代码使用类似的东西: $tags = array("theroots" => 5,"michael jackson" = 3,"billy icon" => 9,"madonna" => 1);我同意不要使用 H 标签,因为它会破坏您的语义。跨度将是我的选择。最后,Zend 框架中存在一个帮助器,它可以满足您的需要。见framework.zend.com/manual/en/zend.tag.html
          【解决方案5】:

          @瑞安

          这是正确的,但它实际上使编号最少的标签更大。此代码已经过测试:

          $artist = array("the roots","michael jackson","billy idol","more","and more","and_YET_MORE");
          $count = array(5,3,9,1,1,3);
          $highest = max($count);
          for ($x = 0; $x < count($artist); $x++) {
              $normalized =  ($highest - $count[$x]+1) / $highest;
              $heading = ceil($normalized * 6); // 6 heading types
              echo "<h$heading>{$artist[$x]}</h$heading>";
          }
          

          【讨论】:

            【解决方案6】:

            此方法适用于SQL/PostgreSQL 狂热者。它在数据库中完成整个工作,并打印带有“slugified”链接的文本。它使用 Doctrine ORM 仅用于 sql 调用,我没有使用对象。 假设我们有 10 种尺寸:

            public function getAllForTagCloud($fontSizes = 10)
            {
                $sql = sprintf("SELECT count(tag) as tagcount,tag,slug, 
                floor((count(*) * %d )/(select max(t) from 
                    (select count(tag) as t from magazine_tag group by tag) t)::numeric(6,2)) 
                     as ranking 
                     from magazine_tag mt group by tag,slug", $fontSizes);
            
                $q = Doctrine_Manager::getInstance()->getCurrentConnection();
                return $q->execute($sql);
            }
            

            然后你用一些 CSS 类打印它们,从 .tagranking10(最好的)到 .tagranking1(最差的):

            <?php foreach ($allTags as $tag): ?>
                <span class="<?php echo 'tagrank'.$tag['ranking'] ?>">
                    <?php echo sprintf('<a rel="tag" href="/search/by/tag/%s">%s</a>', 
                        $tag['slug'], $tag['tag']
                    ); ?>
                </span>
            <?php endforeach; ?>
            

            这是CSS

            /* put your size of choice */
            .tagrank1{font-size: 0.3em;}
            .tagrank2{font-size: 0.4em;}
            .tagrank3{font-size: 0.5em;} 
            /* go on till tagrank10 */
            

            此方法显示所有标签。如果你有很多,你可能不希望你的标签云成为标签风暴。在这种情况下,您将在 SQL 查询中附加一个 HAVING TO 子句:

            -- minimum tag count is 8 --
            
            HAVING count(tag) > 7
            

            就是这样

            【讨论】:

              【解决方案7】:

              作为 Rails 中的助手:

              def tag_cloud (strings, counts)
                  max = counts.max
                  strings.map { |a| "<span style='font-size:#{((counts[strings.index(a)] * 4.0)/max).ceil}em'>#{a}</span> "  }
              end
              

              从视图中调用它:

              <%= tag_cloud($artists, $counts) %>
              

              这会输出数组中的&lt;span style='font-size:_em'&gt; 元素,该数组将在视图中转换为字符串,最终呈现如下:

              <span style='font-size:3em'>the roots</span>
              <span style='font-size:2em'>michael jackson</span> 
              <span style='font-size:4em'>billy idol</span> 
              <span style='font-size:1em'>more</span> 
              <span style='font-size:1em'>and more</span> 
              <span style='font-size:2em'>and_YET_MORE</span> 
              

              最好有一个class 属性并引用上面Brendan 提到的样式表中的类。比在语义上使用h1-h6 好得多,而且&lt;span&gt; 的风格包袱更少。

              【讨论】:

              • 为什么有人给它一个-1?
              【解决方案8】:

              我知道这是一篇很老的帖子,但我仍在发布我的观点,因为它可能对将来的人有所帮助。

              这是我在我的网站中使用的标记云: http://www.vbausefulcodes.in/

              <?php
              $input= array("vba","macros","excel","outlook","powerpoint","access","database","interview questions","sendkeys","word","excel projects","visual basic projects","excel vba","macro","excel visual basic","tutorial","programming","learn macros","vba examples");
              
              $rand_tags = array_rand($input, 5);
              for ($x = 0; $x <= 4; $x++) {
                  $size = rand ( 1 , 4 );
                  echo "<font size='$size'>" . $input[$rand_tags[$x]] . " " . "</font>";
              }
              
              echo "<br>";
              $rand_tags = array_rand($input, 7);
              for ($x = 0; $x <= 6; $x++) {
                  $size = rand ( 1 , 4 );
                  echo "<font size='$size'>" . $input[$rand_tags[$x]] . " " . "</font>";
              }
              
              echo "<br>";
              $rand_tags = array_rand($input, 5);
              for ($x = 0; $x <= 4; $x++) {
                  $size = rand ( 1 , 4 );
                  echo "<font size='$size'>" . $input[$rand_tags[$x]] . " " . "</font>";
              }
              ?>
              

              【讨论】:

                猜你喜欢
                • 2011-09-02
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2023-03-15
                • 1970-01-01
                • 2021-05-31
                • 1970-01-01
                相关资源
                最近更新 更多