Page 1 of 1

Yet another tag cloud.

Posted: Fri Oct 03, 2008 4:39 am
by stakes
Hello. I put together some code yesterday that will generate so called tag clouds from a given query, I'd really like some feedback on how the code could be improved in a OOP aspect, but also if things could be done simpler, because i feel i've overdone it somewhat. Here's the code:

Code: Select all

 
class TagVisualizer { 
     
    private $query;     //Query passed to object 
    private $levels;     //The number of "level" we want to spread out or tags on 
    private $db;        //Our database Connection 
    private $id;        //Identifier for every instance 
    private $result;     //The Query result 
    private $high;        //The higest qunatity in the resultset 
    private $low;        //The lowest qunatity in the resultset 
    private $increment;    //The Increment for font-size for each level 
     
    private $tags = array(); //Tag array of tags 
    private $css = array();     //The array of css values 
     
    private $tagOutput;    //The finished HTML 
    private $cssOutput;    //The finished CSS 
     
    //Constructor initializes properties and runQuery method.  
    public function __construct($query, $levels, $db, $id, $increment) { 
 
        //Initialize properties 
        $this->query = $query; 
        $this->levels = $levels; 
        $this->db = $db; 
        $this->id = $id; 
        $this->increment = $increment; 
         
        //Run our given query 
        $this->runQuery(); 
    } 
     
         
    //Return the tags in HTML 
    public function getTags() { 
         
        return $this->tagOutput; 
    } 
     
    //Return the CSS 
    public function getCSS() { 
         
        return $this->cssOutput; 
    } 
     
    private function runQuery() { 
     
        //Run Query 
        $this->result = $this->db->query($this->query); 
        //Generate Tags, HTML and CSS 
        $this->tagArray = $this->buildArray(); 
        $this->tagOutput= $this->buildTags(); 
        $this->cssOutput = $this->buildCSS(); 
    } 
 
    //Build an array from our query resultset 
    private function buildArray() { 
     
        $i = 0; 
         
        while($row = mysqli_fetch_assoc($this->result)) { 
             
            // The quantity of current tag 
            $q = $row['quantity'];  
             
            //Create an array with current tag values 
            $tags[$i]['q'] = $q; 
            $tags[$i]['tag'] = $row['label']; 
             
            //If current quantity higest value 
            if($q > $this->high) 
                $this->high = $q; 
            //If lowest value is not set. 
            if(!$this->low) 
                $this->low = $q; 
            //Else if current quantity is lowest value 
            elseif($q < $this->low) 
                $this->low = $q; 
                 
            $i++; 
        } 
         
        return $tags; 
    } 
     
    //Build our tags with a HTML ul list  
    private function buildTags() { 
         
        //Calculate the range between each level 
        $range = ($this->high - $this->low) / $this->levels; 
         
        //Loop through our tag array and start adding tags 
        $taglist = "<ul>\n"; 
        foreach($this->tagArray as $tag) { 
             
            //Iterate through every level 
            for($i=1; $i <= $this->levels; $i++) { 
             
                //If current tag quantity matches the range of the current level 
                //break out of the loop, and go onto next tag.  
                $size = ($range * $i) + $this->low; 
                if($tag['q'] <= $size) { 
                     
                    //Start building our list of css font-size values 
                    //We only need one value for each font-size 
                    if(!in_array($i, $this->css)) { 
                        $this->css[] = $i; 
                    } 
                     
                    //Build the taglist  
                    $taglist .= "\t<li><a class=\"{$this->id}{$i}\" href=\"#\">"; 
                    $taglist .= $tag['tag']; 
                    $taglist .= "({$tag['q']})</a></li>\n"; 
                    break; 
                } 
            } 
        } 
        $taglist .= "</ul>\n"; 
        return $taglist; 
    } 
     
    //Iterate through the CSS array that we built in the buildTags 
    //method and build some css font-size properties 
    private function buildCSS() { 
         
        $csslist = ""; 
         
        foreach($this->css as $css) { 
             
            $size = 100 + ($this->increment * $css); 
            $csslist .= ".{$this->id}{$css} {"; 
            $csslist .= "font-size: $size%"; 
            $csslist .= "}\n";     
        } 
         
        return $csslist; 
    } 
} 
 
Here's how i create an instance of the class:

Code: Select all

$db = new mysqli("localhost", "user", "password", "db-name"); 
$query = "SELECT COUNT(id) as quantity, label FROM tags GROUP by label"; 
$cloud = New TagVisualizer($query, 9, $db, "a", 5);
and here's how i fetch code for that instance

Code: Select all

echo $cloud->getCSS(); 
echo $cloud->getTags();
I put up a demo page illustrating the use of different queries on this class.

Thanks in advance looking forward to some critique :)