首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用h1通过h6从数组生成标记云的最佳方式是什么?

使用h1通过h6从数组生成标记云的最佳方式是什么?
EN

Stack Overflow用户
提问于 2008-08-01 21:40:56
回答 8查看 8.7K关注 0票数 78

我有以下数组:

代码语言:javascript
复制
$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标签中。

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2010-05-31 19:53:58

你也需要添加一个对数函数到它上面。(摘自tagadelic,我的创建标签云http://drupal.org/project/tagadelic的Drupal模块):

代码语言:javascript
复制
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);
}

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

代码语言:javascript
复制
foreach ($tags as $tag) {
    $output .= "<h$tag->weight>$tag->name</h$tag->weight>"
}
票数 41
EN

Stack Overflow用户

发布于 2008-08-01 23:10:57

从我的脑海中...

代码语言:javascript
复制
$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.">";
}
票数 32
EN

Stack Overflow用户

发布于 2008-08-03 13:01:24

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

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

票数 26
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/227

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档