使用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 标记和最小的封装的 h1 标记中包含 $ count 中的艺术家。

0
额外 编辑
意见: 2

6 答案

@Ryan

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

$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 "{$artist[$x]}";
}
0
额外

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

也许 spanol 具有合适的类属性(加上一些CSS)?

0
额外

离开我的头顶...

$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 "".$artist[$x]."";
}
0
额外

你也会想为它添加一个对数函数。 (取自tagadelic,我的Drupal模块创建标签云 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 .= "weight>$tag->nameweight>"
}
0
额外

此方法适用于 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): ?>
    
        <?php echo sprintf('', 
            $tag['slug'], $tag['tag']
        ); ?>
    
<?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

就这样

0
额外

我知道它的旧帖子。张贴我的观点,因为它可能有助于未来的人。

Here is the tagcloud I used in my website 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 "" . $input[$rand_tags[$x]] . " " . "";
}

echo "
"; $rand_tags = array_rand($input, 7); for ($x = 0; $x <= 6; $x++) { $size = rand ( 1 , 4 ); echo "" . $input[$rand_tags[$x]] . " " . ""; } echo "
"; $rand_tags = array_rand($input, 5); for ($x = 0; $x <= 4; $x++) { $size = rand ( 1 , 4 ); echo "" . $input[$rand_tags[$x]] . " " . ""; } ?>
0
额外