This method is for SQL/PostgreSQL
fanatics. It does the entire job in the database, and it prints text with "slugified" link. It uses Doctrine ORM
just for the sql call, I'm not using objects.
Suppose we have 10 sizes:
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);
}
then you print them with some CSS class, from .tagranking10 (the best) to .tagranking1 (the worst):
<?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; ?>
and this is the 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 */
This method displays all tags. If you have a lot of them, you probably don't want your tag cloud to become a tag storm. In that case you would append an HAVING TO
clause to your SQL query:
-- minimum tag count is 8 --
HAVING count(tag) > 7
That's all
<h6>
. In the sample set:[5, 3, 9, 1, 1, 3]
, your snippet assigns weight9
as<h6>
(good), but then5
(the next highest) as<h5>
despite5
being the midpoint between min value1
and max value9
. Without doing any math, I would assume that weight5
should be either<h3>
or<h4>
(a mid-range tag). You might review my conversion of your answer: 3v4l.org/dv3gv – Wilen