从数组中使用h1到h6来生成标签云的最佳方法是什么? [英] What's the best way to generate a tag cloud from an array using h1 through h6 for sizing?
本文介绍了从数组中使用h1到h6来生成标签云的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数组:
$artist = array("the roots", "michael jackson", "billy idol", "more", "and more", "and_YET_MORE");
$count = array(5, 3, 9, 1, 1, 3);
我想生成一个标签云,其中将包含
和最低的 h6
标签中的<$ count h1
>
I want to generate a tag cloud that will have artists with a higher number in $count
enclosed in h6
tags and the lowest enclosed h1
tags.
推荐答案
你也想添加一个对数函数。 (取自tagadelic,我的Drupal模块创建标签云 http://drupal.org/project/tagadelic):
You will want to add a logarithmic function to it too. (taken from tagadelic, my Drupal module to create tag clouds 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);
}
然后在您的视图或模板中:
Then in your view or template:
foreach ($tags as $tag) {
$output .= "<h$tag->weight>$tag->name</h$tag->weight>"
}
这篇关于从数组中使用h1到h6来生成标签云的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文