Support Forums

Full Version: Create Tags Cloud
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Credits and Source: http://www.roscripts.com/

Name: Tags Cloud
Description: You've seen it for the first time (as far as I know) on Flickr and you're seeing it on major websites as well. The "tag cloud" or "weighted words list" is nothing more than another way of displaying a navigation. It's funny, weird, new to the internet, attracts clicks, easy to do and it's also free. It's so easy to do a tag cloud using this simple PHP functions that I've found. Make your site more interesting and step into the web 2.0 world that everyone's talking about. Enjoy!
Snippet:
PHP Code:
function printTagCloud($tags) {
        
// $tags is the array
       
        
arsort($tags);
       
        
$max_size 32// max font size in pixels
        
$min_size 12// min font size in pixels
       
        // largest and smallest array values
        
$max_qty max(array_values($tags));
        
$min_qty min(array_values($tags));
       
        
// find the range of values
        
$spread $max_qty $min_qty;
        if (
$spread == 0) { // we don't want to divide by zero
                
$spread 1;
        }
       
        
// set the font-size increment
        
$step = ($max_size $min_size) / ($spread);
       
        
// loop through the tag array
        
foreach ($tags as $key => $value) {
                
// calculate 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 round($min_size + (($value $min_qty) * $step));
       
                echo 
'<a href="#" style="font-size: ' $size 'px" 
title="' 
$value ' things tagged with ' $key '">' $key '</a> ';
        }
}

$tags = array('weddings' => 32'birthdays' => 41'landscapes' => 62
'ham' => 51'chicken' => 23'food' => 91'turkey' => 47'windows' => 82'apple' => 27);

printTagCloud($tags); 

Code:
<style type="text/css">
.tag_cloud { padding: 3px; text-decoration: none; }
.tag_cloud:link  { color: #81d601; }
.tag_cloud:visited { color: #019c05; }
.tag_cloud:hover { color: #ffffff; background: #69da03; }
.tag_cloud:active { color: #ffffff; background: #ACFC65; }
</style>

PHP Code:
function get_tag_data() {
    
$arr = Array('Actionscript' => 35'Adobe' => 22'Array' => 44'Background' => 43
    
'Blur' => 18'Canvas' => 33'Class' => 15'Color Palette' => 11'Crop' => 42
    
'Delimiter' => 13'Depth' => 34'Design' => 8'Encode' => 12'Encryption' => 30
    
'Extract' => 28'Filters' => 42'Flash' => 32'Functions' => 19
    
'Gaussian Blur' => 44'Grafix' => 49'Graphics' => 35'Hue' => 47'Illustrator' => 8
    
'Image Ready' => 12'Javascript' => 47'Jpeg' => 15,     'Keyboard' => 18'Level' => 28
    
'Liquify' => 30'Listener' => 10'Logo' => 12'Loops' => 22'Macromedia' => 26
    
'Method' => 28'MySQL' => 18'Obfuscation' => 13'Object' => 39'Optimize' => 25
    
'PDF' => 37'PHP' => 44'PSD' => 17'Photography' => 28'Photoshop' => 46
    
'Revert' => 50'Saturation' => 35'Save as' => 28'Scope' => 11'Scripting' => 9
    
'Security' => 41'Sharpen' => 49'Switch' => 41'Templates' => 11'Texture' => 22
    
'Tool Palette' => 30'Variables' => 50);
    return 
$arr;

function 
get_tag_cloud() {
// Default font sizes
$min_font_size 12;
$max_font_size 30;

// Pull in tag data
$tags get_tag_data();


//Finally we start the HTML building process to display our tags. For this demo the tag simply searches Google using the provided tag.
$cloud_html '';
$cloud_tags = array(); // create an array to hold tag code
foreach ($tags as $tag => $count) {
    
$size $min_font_size + ($count $minimum_count
        * (
$max_font_size $min_font_size) / $spread;
    
$cloud_tags[] = '<a style="font-size: 'floor($size) . 'px' 
        
'" class="tag_cloud" href="http://www.google.com/search?q=' $tag 
        
'" title="\'' $tag  '\' returned a count of ' $count '">' 
        
htmlspecialchars(stripslashes($tag)) . '</a>';
}
$cloud_html join("\n"$cloud_tags) . "\n";
return 
$cloud_html;


Usage:
Code:
<h3>Sample Tag Cloud results</h3>
<div id="wrapper"
<!-- BEGIN Tag Cloud -->
<?php print get_tag_cloud(); ?>
<!-- END Tag Cloud -->
</div>

If you want to use a databse to retrieve your data replace function get_tag_data from above with this one:

PHP Code:
function get_tag_data() { 
  
mysql_connect('localhost''username''password');
  
mysql_select_db('database');
  
$result mysql_query("SELECT * FROM tags GROUP BY tag ORDER BY count DESC"); 
  while(
$row mysql_fetch_array($result)) { 
    
$arr[$row['tag']] = $row['count '];
  } 
  
ksort($arr); 
  return 
$arr


MORE DETAILS: http://www.roscripts.com/Create_tag_cloud-71.html


Thankyou for reading. Be happy always Smile