Here's my simple tagCloud Codeigniter php script
video_to_tag table is a many_to_many relationship table.
I know that this code can be improved but right now it's a nice starting point.
- public function tagCloud($nr_entries)
- {
- $this->db->select('COUNT(video_to_tag.tag_id) as tag_entries,
- tags.tag_name')
- ->from('video_to_tag')
- ->join('tags', 'tags.tag_id = video_to_tag.tag_id', 'inner')
- ->group_by('tags.tag_name')
- ->order_by('tags.tag_id')
- ->limit($nr_entries,0);
- $result = $this->db->get();
- if (0 < $result->num_rows())
- {
- $result = $result->result();
- // order the tags array
- foreach ($result as $entry)
- {
- $tags[$entry->tag_name] = $entry->tag_entries;
- }
- $max_size = 250; // max font size in %
- $min_size = 100; // min font size in %
- // get the largest and smallest array values
- // find the range of values
- $spread = $max_qty - $min_qty;
- if (0 == $spread) // we don't want to divide by zero
- {
- $spread = 1;
- }
- // determine the font-size increment
- // this is the increase per tag quantity (times used)
- $step = ($max_size - $min_size)/($spread);
- // init the url helper
- $this->load->helper('url');
- // init the tag cloud
- $tag_cloud = '';
- // loop through our tag array
- foreach ($tags as $key => $value)
- {
- // calculate CSS font-size
- // find the $value in excess of $min_qty
- // multiply by the font-size increment ($size)
- // and add the $min_size set above
- $size = $min_size + (($value - $min_qty) * $step);
- 'style' => "font-size: {$size}%");
- $tag_cloud .= anchor('tag/'.url_title($key), $key, $url_attributes) . ' ';
- }
- return $tag_cloud;
- }
- return false;
- }
Post new comment