All pastes #105815 Raw Edit

tag cloud

public php v1 · immutable
#105815 ·published 2006-07-29 22:26 UTC
rendered paste body
/****************************************************************************************/// Sorts a list of tags by their count ascending.function tag_asort($tag1, $tag2){   if($tag1[tag_count] == $tag2[tag_count])   {       return 0;   }   return ($tag1[tag_count] < $tag2[tag_count]) ? -1 : 1;}/****************************************************************************************/// Sorts a list of tags alphabetically by tag_namefunction tag_alphasort($tag1, $tag2){   if($tag1[tag_name] == $tag2[tag_name])   {       return 0;   }   return ($tag1[tag_name] < $tag2[tag_name]) ? -1 : 1;}/****************************************************************************************/// Assigns classes to each given tag.function cloud_tags($tags){	$tag_sizes = 7;	usort($tags, "tag_asort");	if(count($tags) > 0)	{		// Start with the sorted list of tags and divide by the number of font sizes (buckets).		// Then proceed to put an even number of tags into each bucket.  The only restriction is		// that tags of the same count can't span 2 buckets, so some buckets may have more tags		// than others.  Because of this, the sorted list of remaining tags is divided by the		// remaining 'buckets' to evenly distribute the remainder of the tags and to fill as		// many 'buckets' as possible up to the largest font size.		$total_tags = count($tags);		$min_tags = $total_tags / $tag_sizes;		$bucket_count = 1;		$bucket_items = 0;		$tags_set = 0;		foreach($tags as $key => $tag)		{			$tag_count = $tag[tag_count];			// If we've met the minimum number of tags for this class and the current tag			// does not equal the last tag, we can proceed to the next class.			if(($bucket_items >= $min_tags) and $last_count != $tag_count and $bucket_count < $tag_sizes)			{				$bucket_count++;				$bucket_items = 0;				// Calculate a new minimum number of tags for the remaining classes.				$remaining_tags = $total_tags - $tags_set;				$min_tags = $remaining_tags / $bucket_count;			}			// Set the tag to the current class.			$tags[$key][tag_class] = 'tag'.$bucket_count;			$bucket_items++;			$tags_set++;			$last_count = $tag_count;		}		usort($tags, 'tag_alphasort');	}	return $tags;}