tagcloud.php
- <!-- Based on code found here: -->
- <!-- http://scriptplayground.com/tutorials/php/Tag-Cloud/ -->
- <style type="text/css">
- .tag_cloud { padding: 3px; text-decoration: none; }
- .tag_cloud:link { color: #930; }
- .tag_cloud:visited { color: #430; }
- .tag_cloud:hover
- {
- border-width:1px;
- border-style:solid;
- border-color: #930;
- text-decoration:none;
- background:#fff;
- }
- .tag_cloud:active
- {
- color: #ffffff;
- background: #ACFC65;
- }
- </style>
- <?php
- // Function get_tag_data modified for use on southrustern.com
- // Function sub_dir created solely for use on southrustern.com
- //
- // The functions walk a directory structure like:
- //
- // info/AZ/tucson/img
- // info/AZ/dolan_springs/img
- // info/CA/inyokern/img
- //
- // and count the images found in each. They return an array
- // of directory (key) => num (value) pairs.
- //
- // How the code would respond on a different directory structure
- // is unknown.
- //
- // As usual, I accept no responsibility for your use of this code.
- // If it results in your computer becoming a writhing pit of viruses
- // or a smoking lump of charred silicon, youre on your own.
- //
- // Mike Martinet <mmrtnt eachat southrustern daught com>
-
- // Return an array of city_state keys with image count values
- function get_tag_data() {
- $count = array();
- $results = array();
- foreach( array( 'AZ', 'CA', 'NV' ) as $dir ) {
- $count = &sub_dir( "info/".$dir, &$count );
- foreach ( $count as $key=>$num ) {
- // EXAMPLE:
- // key: info/CA/inyokern/img
- // num: 1
- $state = substr( $key, 5, 2 );
- $city = preg_replace( '/info\/(.{3})(.*)\/.*/', '$2', $key );
- // Skip the zipcode symlinks
- if( preg_replace( '/\d{5}/', '', $city ) ) {
- $city = $city."_".strtolower( $state );
- $results[ $city ] = $num;
- }
- }
- }
- return( $results );
- }
- // Recurse looking for img directories
- // and return count of files found within
- function sub_dir( $indir, $count ) {
- $idx = 0;
- // In short, the code starts off trying to open $indir
- // It looks at files until it finds one titled 'img'
- // When it finds img, it sets the current path as a key
- // in the count array and sets the value to 0.
- // If it already is in the img directory, it counts the files
- // and updates the value of the current key
- if( is_dir( $indir ) ) {
- if( $dh = opendir( $indir ) ) {
- while( ( $file = readdir( $dh ) ) !== false ) {
- if( ( $file != "." ) && ( $file != ".." ) ) {
- if( stripos( $indir, "img" ) ) {
- $count[ $indir ] = ++$idx;
- }
- $nextdir = $indir."/".$file;
- if( $file == "img" ) {
- }
- if( is_dir ( $nextdir) ) {
- sub_dir( $nextdir, &$count);
- }
- }
- }
- }
- }
- return( $count );
- }
- function get_tag_cloud() {
- // First off we define the min and max font sizes.
- // Default font sizes
- $min_font_size = 12;
- $max_font_size = 30;
- // Call our "get_tag_data" function to retrieve an array of tags.
- // Pull in tag data
- $tags = get_tag_data();
- // We pull out the minimum and maximum count from our tags.
- // Creating a spread to be used in our font size calculations.
- // Check to see if we have a good spread, if not, set to one.
- $minimum_count = min(array_values($tags));
- $maximum_count = max(array_values($tags));
- $spread = $maximum_count - $minimum_count;
- // Build the HTML and return the HTML code.
- $cloud_html = '';
- $cloud_tags = array(); // create an array to hold tag code
- foreach ($tags as $tag => $count) {
- // Remove the state append
- $tagsub = substr( $tag, 0, -3 );
- $size = $min_font_size + ($count - $minimum_count)
- * ($max_font_size - $min_font_size) / $spread;
- $cloud_tags[] = '<a style="font-size: '. floor($size) . 'px'
- . '" class="tag_cloud" href="/node/88&info=' . $tag
- . '" title="' . $tagsub. ' : ' . $count . '">'
- . htmlspecialchars(stripslashes($tagsub)) . '</a>';
- }
- $cloud_html = join("\n", $cloud_tags) . "\n";
- return $cloud_html;
- }
- // Here is some example HTML to call this tag builder.
- ?>
- <!-- <h3>Sample Tag Cloud results</h3> -->
- <div id="wrapper"
- <!-- BEGIN Tag Cloud -->
- <?php
- print get_tag_cloud();
- print "<br style=\"font-size:12px\">Tag Cloud by <a href=\"http://scriptplayground.com/tutorials/php/Tag-Cloud/\">Script Playground</a>";
- ?>
- <!-- END Tag Cloud -->
- </div>