I want to know about Zend Tag Component.
What is it? How to use this in my project?
Suppose I have following three tables: posts - id,name tags - id,name posts_tags - id,post_id,tag_id
How can I use this component to create a tag cloud? What is weight?
I have checked these:
http://framework.zend.com/manual/current/en/modules/zend.tag.introduction.html
http://framework.zend.com/manual/current/en/modules/zend.tag.cloud.html
As an example, you have two tags: Tag A
occuring 1000 times, Tag B
occuring only once, and Tag C
occuring 50 times. The first idea might be have the number of occurences to be the font size. Now that ain't very helpful as we'd have Tag A
at font size 1000
, and Tag B
at font size 1
. Instead you probably want something like:
25
10
2
This is where this module comes into place. The basic problem this module tries to solve is the mapping of how often a tag occurs to how large you want it to be displayed in relation to other tags
. Or to be put into a function: calculateBoundedFontSize(count($tag))
.
In Zend\\Tag\\
this is handled by weights (how much a tag occurs), and values (the font size value in relation to a tag's weight):
The second is done by this line in the documenation:
$list->spreadWeightValues(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
Which basically tells the module to give the tag occoring most a font-size of 10
, the one occoring least a font size of 1
. Everything in between is spread occordingly. To come back to the initial example, Tag A
would get a font size of 10
, Tag B
a font-size of 1
, and Tag C
a font-size of 2
.
The reason the documentation isn't particularry talking about count
and font size
is for abstraction. Instead of taking the number of occurances, we could use some other factor for determining the importance of an item (eg importance). We also could use some more advanced calculated value, for example we could say Featured Tags
are more important and add an additional 1000
to their weight in addition to the count. For values the same applies, it doesn't need to be a font-size but could be used for ordering instead as well. Only Zend\\Tag\\Cloud
uses these weights and values as font-sizes. But we could easily create another module that uses them for rendering an ordered list instead.
For your situation, the simple example would suffice already though:
// Create a ItemList
$list = new Zend\Tag\ItemList();
// Add all tags and their count to it
foreach ($allTags as $tag) {
$list[] = new Zend\Tag\Item(array('title' => $tag, 'weight' => countTagOccurences($tag));
}
// Assign font size values
$list->spreadWeightValues(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
// Or to use the Zend\Tag\Cloud directly
$cloud = new Zend\Tag\Cloud(
array(
'tagDecorator' => array(
'decorator' => 'htmltag',
'options' => array(
'minFontSize' => '1',
'maxFontSize' => '10',
),
),
'tags' => $list
)
);
The documentation is pretty clear (although not all that obvious); perhaps a better question is "What is Zend_Tag_Cloud?":
Zend_Tag_Cloud is the rendering part of Zend_Tag. By default it comes with a set of HTML decorators, which allow you to create tag clouds for a website, but also supplies you with two abstract classes to create your own decorators, to create tag clouds in PDF documents for example.
http://framework.zend.com/manual/1.12/en/zend.tag.cloud.html
https://en.wikipedia.org/wiki/Tag_cloud
HTH.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.