January 19, 2021
Hot Topics:

Create Tag Clouds with the Zend Framework's Zend_Tag_Cloud Component

  • By Jason Gilmore
  • Send Email »
  • More Articles »

Associating Tags with Content

Of course, tags serve no purpose without the ability to associate them with content such as products or blog entries. Currently, neither the Zend_Tag_Cloud nor the sibling Zend_Tag component offers a native way to save or retrieve tags. They don't provide a way for directly associating them with a particular model either, meaning you'll need to devise your own solution to this task. However, because the only real requirement in terms of rendering the cloud is that you pass the expected array to the Zend_Cloud constructor, my preferred solution at this point involves creating a tag model/table and a corresponding model/table for mapping the tags to the desired model. Using Zend_Db's model relationship features, the rest is pretty trivial. For instance, if we wanted to tag blog entries, the tags table might look like this:

mysql> describe tag;+-----------+------------------+------+-----+---------+----------------+| Field     | Type             | Null | Key | Default | Extra          |+-----------+------------------+------+-----+---------+----------------+| id        | int(10) unsigned | NO   | PRI | NULL    | auto_increment | | title     | varchar(200)     | NO   |     | NULL    |                | | permalink | varchar(200)     | NO   |     | NULL    |                | +-----------+------------------+------+-----+---------+----------------+

A mapping table used to associate blog entries with tags would look like this:

mysql> describe blogs_to_tags;+---------+------------------+------+-----+---------+----------------+| Field   | Type             | Null | Key | Default | Extra          |+---------+------------------+------+-----+---------+----------------+| id      | int(10) unsigned | NO   | PRI | NULL    | auto_increment | | blog_id | int(10) unsigned | NO   |     | NULL    |                | | tag_id  | int(10) unsigned | NO   |     | NULL    |                | +---------+------------------+------+-----+---------+----------------+

From here, retrieving an all-encompassing tag cloud complete with weightings based on frequency, or a blog entry's tags, or a list of blog entries according to a tag is as easy as writing a few SQL queries!


The Web's remarkable ability to self-organize in spite of lacking a centralized controlling body has taught us volumes about sociology, economics and the marketplace. It has also taught businesses that maintaining white-knuckle control over their products and content isn't always the easiest path to profitability -- let alone the way to win over customers.

This transformation has taken place gradually, beginning with something as simple as allowing customers to post product reviews, both good and bad. After confirming that an immediate cessation of business operations wasn't inevitable after all, control was loosened in other areas, including offering support forums and maintaining open product wikis. Perhaps the customer is king after all!

By implementing tag clouds, you provide customers with an efficient way to organize website content according to terms that make the most sense to the marketplace, while simultaneously removing the burden of organization from the website owner. Are you using tag clouds in interesting and compelling ways to organize your material? Tell us about it in the comments!

About the Author

Jason Gilmore is founder of WJGilmore.com. He also is the authorof several popular books, including "Easy PHP Websites with the Zend Framework","Easy PayPal with PHP", and"Beginning PHP and MySQL, Third Edition".

Page 2 of 2

This article was originally published on July 16, 2010

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date