MaggsWeb:7

www.maggsweb.com

I have been looking for a ‘simple’ tag cloud generator that will parse content from MySQL for ages and couldn’t find one. So I adapted – and heavily modified (reduced) – and CSS’d – the best one that I could find. It turned out nice and simple.

  .size9 {	color: #000;	font-size: 26px;	 }
  .size8 { 	color: #111;	font-size: 24px;	 }
  .size7 {	color: #222;	font-size: 22px;	 }
  .size6 {	color: #333;	font-size: 20px;	}
  .size5 {	color: #444;	font-size: 18px;	}
  .size4 {	color: #555;	font-size: 16px;	  }
  .size3 {	color: #666;	font-size: 14px;	  }
  .size2 {	color: #777;	font-size: 12px;	  }
  .size1 {	color: #888;	font-size: 8px;	  }
  .size0 {	color: #999;	font-size: 6px;	  }


<?php
 
$terms = array();
$maximum = 0;
 
$query = mysql_query("SELECT word, count FROM [tablename] WHERE display = '1'");
// could add LIMIT, SORT, etc.. if required
 
while ($row = mysql_fetch_array($query)) {
//create vars
    $word = $row['word'];
    $count = $row['count'];
 
    // set 'maximum' from counter
    if ($count > $maximum) $maximum = $count;
 
//create array
    $elements[] = array('word' => $word, 'count' => $count);
 
}
//random
//shuffle($elements);
//alphabetical
sort($elements);
 
foreach ($elements as $e){ // start looping through the tags
 
    // determine the popularity of this term as a percentage
    $percent = floor(($e['count'] / $maximum) * 100);
 
    // determine the class for this term based on the percentage
	if 	   ($percent >= 99)		$class = 9;
	elseif ($percent >= 70)		$class = 8;
	elseif ($percent >= 60)		$class = 7;
	elseif ($percent >= 50)		$class = 6;
	elseif ($percent >= 40)		$class = 5;
	elseif ($percent >= 30)		$class = 4;
	elseif ($percent >= 20)		$class = 3;
	elseif ($percent >= 10)		$class = 2;
	elseif ($percent >= 5)		$class = 1;
	else						$class = 0;
 
    // output this term
    //with links...
    //echo "<span class=\"$class\"><a href=\"search.php?search=" . urlencode($k['term']) . "\">" . $k['term'] . "</a></span>\n ";
  //without links.../
    echo "<span class='word size".$class."'>" . $e['word'] . "</span>\n ";
 
}
// for debugging....
//echo "<hr />";
//echo "<pre>";
//print_r($elements);
//echo "

“;
?>

Share this:
  • Print
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • LinkedIn
  • Twitter

Write a Comment

Let me know what you think?