PHP Tag Cloud





13
Date Submitted Thu. Oct. 5th, 2006 4:06 PM
Revision 1 of 1
Scripter wiz1705
Tags tags
Comments 9 comments
Creates one of those neat Web 2.0-like tag clouds!
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);

Zachary Howe

autoignition.net

Comments

Comments Generate a random string
Mon. Oct. 9th, 2006 4:55 AM    Helper serpentskiss
Comments using mysql
Sun. Oct. 8th, 2006 6:11 PM    Newbie raptrex
  Comments SQL Tables
Mon. Oct. 9th, 2006 2:29 PM    Scripter wiz1705
    Comments thank you wiz
Wed. Oct. 11th, 2006 6:16 PM    Newbie raptrex
Comments Help in getting data!
Sun. May. 6th, 2007 10:23 PM    Newbie ganeshnrao
Comments PHP Tag Cloud Random
Thu. Aug. 16th, 2007 6:35 PM    Newbie eolmos
  Comments nice
Fri. Feb. 29th, 2008 10:31 AM    Newbie Catalin
Comments PHP Tag Cloud Random
Thu. Aug. 16th, 2007 6:35 PM    Newbie eolmos
Comments Random Cloud
Wed. Jun. 25th, 2008 7:40 AM    Newbie gamershirts_nl

Voting