<?php
/**************************************************************
*
* DataFeedFile.com (DFF) Framework API
* Copyright (c) 2008 DataFeedFile.com
*
* DFF Framework API uses CreativeCommons.org by-sa license.
* This license lets others remix, tweak, and build upon your work even for commercial reasons,
* as long as they CREDIT DataFeedFile.com and license their new creations under the identical terms.
* This license is often compared to open source software licenses.
* All new works based on yours will carry the same license, so any derivatives will also allow commercial use.
* Please read our license.txt file for more detail.
*
**************************************************************/
/*
* Input $params_array:
* value_csv = TRUE / FALSE (if true means array contains array of tags separated by commas)
* max_tags = integer ( 0 = unlimited )
* max_font_percent = integer (in percentage)
* tag_word = string (example: deal, item, product, music title, etc...)
* target_url_prefix = URL string (example: http://www.yoursite.com/search.php?keyword=)
*/
function DFF_create_tag_cloud($params_array,$values){
//GATHER AND COUNT TAGS FIRST
$tags_all_array = array();
if($values){
foreach($values as $val){
if($params_array['value_csv']===TRUE){
//PARSE COMMA DELIMITED
$tags_array = explode(',',$val);
foreach($tags_array as $ta){
if(strlen($ta)>=3){
$ta = preg_replace("/[^0-9a-z-]/", '', strtolower($ta));
if($tags_all_array[$ta]>0) $tags_all_array[$ta]++;
else $tags_all_array[$ta] = 1;
}
}
} else {
if(strlen($ta)>=3){
$ta = preg_replace("/[^0-9a-z-]/", '', strtolower($ta));
if($tags_all_array[$ta]>0) $tags_all_array[$ta]++;
else $tags_all_array[$ta] = 1;
}
}
}
}
//echo 'tags_array:<pre>'.print_r($tags_all_array,TRUE).'</pre><br>';
// SET PERCENTAGES
$max_size = $params_array['max_font_percent']; // max font size in %
$min_size = 100; // min font size in %
// SET VALUE MAX AND MIN
$max_qty = max(array_values($tags_all_array));
$min_qty = min(array_values($tags_all_array));
// SET RANGE SPREAD
$spread = $max_qty - $min_qty;
if ($spread == 0) { // we don't want to divide by zero
$spread = 1;
}
// determine the font-size increment
// this is the increase per tag quantity (times used)
$step = ($max_size - $min_size)/($spread);
// LIMIT RESULT
if($params_array['max_tags']>0) $tags_all_array = array_splice($tags_all_array,0,$params_array['max_tags']);
// loop through our tag array
foreach ($tags_all_array as $key => $value) {
// calculate CSS 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 = $min_size + (($value - $min_qty) * $step);
// uncomment if you want sizes in whole %:
// $size = ceil($size);
// you'll need to put the link destination in place of the #
// (assuming your tag links to some sort of details page)
echo '<a href="'.$params_array['target_url_prefix'].$key.'" style="font-size:'.$size.'%"';
// perhaps adjust this title attribute for the things that are tagged
echo ' title="'.$value.' '.$params_array['tag_word'].' tagged with '.$key.'"';
echo '>'.$key.'</a> ';
// notice the space at the end of the link
}
} //end function DFF_create_tag_cloud
?>