Think Smart, Be Free, Choose Open Source

tag cloud

Simple PHP Tag Cloud (CodeIgniter)

Add new comment

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.

  1. public function tagCloud($nr_entries)
  2. {
  3.  
  4.     $this->db->select('COUNT(video_to_tag.tag_id) as tag_entries,
  5.  
  6.                       tags.tag_name')
  7.  
  8.              ->from('video_to_tag')
  9.  
  10.              ->join('tags', 'tags.tag_id = video_to_tag.tag_id', 'inner')
  11.  
  12.              ->group_by('tags.tag_name')
  13.  
  14.              ->order_by('tags.tag_id')
  15.  
  16.              ->limit($nr_entries,0);   
  17.  
  18.     $result = $this->db->get();
  19.  
  20.    
  21.  
  22.     if (0 < $result->num_rows())
  23.     {
  24.  
  25.         $result = $result->result();
  26.  
  27.         $tags = array();
  28.        
  29.         // order the tags array
  30.  
  31.         foreach ($result as $entry)
  32.         {
  33.             $tags[$entry->tag_name] = $entry->tag_entries;
  34.         }            
  35.  
  36.  
  37.  
  38.         $max_size = 250; // max font size in %
  39.  
  40.         $min_size = 100; // min font size in %
  41.  
  42.  
  43.  
  44.         // get the largest and smallest array values
  45.  
  46.         $max_qty = max(array_values($tags));
  47.  
  48.         $min_qty = min(array_values($tags));
  49.  
  50.  
  51.  
  52.         // find the range of values
  53.  
  54.         $spread = $max_qty - $min_qty;
  55.  
  56.         if (0 == $spread) // we don't want to divide by zero
  57.         {
  58.             $spread = 1;
  59.         }
  60.  
  61.  
  62.  
  63.         // determine the font-size increment
  64.  
  65.         // this is the increase per tag quantity (times used)
  66.  
  67.         $step = ($max_size - $min_size)/($spread);
  68.  
  69.        
  70.  
  71.         // init the url helper
  72.  
  73.         $this->load->helper('url');
  74.  
  75.         // init the tag cloud
  76.  
  77.         $tag_cloud = '';
  78.  
  79.         // loop through our tag array
  80.  
  81.         foreach ($tags as $key => $value)
  82.         {
  83.             // calculate CSS font-size
  84.  
  85.             // find the $value in excess of $min_qty
  86.  
  87.             // multiply by the font-size increment ($size)
  88.  
  89.             // and add the $min_size set above
  90.  
  91.             $size = $min_size + (($value - $min_qty) * $step);
  92.  
  93.  
  94.  
  95.             $url_attributes = array('title' => "{$value} things tagged with {$key}",
  96.  
  97.                                     'style' => "font-size: {$size}%");
  98.  
  99.             $tag_cloud .= anchor('tag/'.url_title($key), $key, $url_attributes) . '&nbsp;';
  100.  
  101.          }
  102.  
  103.          
  104.  
  105.          return $tag_cloud;
  106.  
  107.  
  108.  
  109.     }  
  110.  
  111.     return false;              
  112.  
  113. }

Syndicate content