Skip to main content
  • Home
  • Tags
OpenView360
Think Smart, Be Free, Choose Open Source
Home » Blogs » CoolGoose's blog

Simple PHP Tag Cloud (CodeIgniter)

In:
  • codeigniter
  • php
  • tag cloud
28Aug2008

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. }

  • CoolGoose's blog

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options

Entries (RSS)

About

Just your average goose.

Activity Stream

  • Fri, 30/07/2010 - 10:07

  • Digg CoolGoose dugg Droid X vs. iPhone 4: Spec Smackdown 10:07am#
  • Digg CoolGoose dugg YouTube Increases Video Upload Length to 15 Minutes 10:07am#
  • Digg CoolGoose dugg Brilliant Adobe Photoshop CS5 Tutorials From 2010 10:07am#
  • Digg CoolGoose dugg Ballmer: Killing the iPad is a &quot;job one urgency&quot; 10:07am#
  • Thu, 29/07/2010 - 22:23

  • Digg CoolGoose dugg When phones are too big for pockets 10:23pm#
  • Digg CoolGoose dugg iPad Owners Are Elitists, Non-Owners Are Independent Geeks 10:23pm#
  • Digg CoolGoose dugg Quite possibly the best Robots.txt file ever 5:18pm#
  • Digg CoolGoose dugg Dell expands Ubuntu Linux desktop offerings 5:18pm#

Tags in Tags

boris akunin carti css demon design devils digg erast fandorin gand general gnome iubire joey goebel kde linux love melancolie open source party php plugins poem power user problem protest quest random random thoughts regex regina noptii remorse renastere site tristete ubuntu windows
more tags

Search

Community

belletristisch.com - Online Literature
OS Revolution - Living in a matrix
At-byte.com - Stay Sharp
Linux PHP IDE

Who's online

There are currently 0 users and 2 guests online.

Syndicate

Syndicate content
Copyright alexandrubucur.com

Drupal port by 3rdWorld : Created by Design Disease