Tuesday, July 08, 2008

Tag Cloud for ftp blogger blogs

In the sidebar section for Blogger template, include:

<h2 class="sidebar-title">Labels</h2>
<p><?php include($_SERVER['DOCUMENT_ROOT']."/labels.php"); ?></p>



The following is a modified version of tag cloud source I found.

<?php

define('PREFIX', 'labels/'); // for url
define('SEARCH_DIR', '/home/username/www/labels'); // server location of labels subdirectory

define('THIS_FILE', 'labels.php'); // name of labels file (this file)

if(file_exists(SEARCH_DIR.'_cloud_include_cache.php') &&
filemtime(SEARCH_DIR.'_cloud_include_cache.php')>(time()-(60*60)))
{
echo file_get_contents(SEARCH_DIR.'_cloud_include_cache.php');
}
else
{
build_cloud();
}

/**
* build_cloud builds a tag cloud from the labels files.
* It actually uses labels file size to determine font size... which does
* not necessarily coincide with number of posts for a given label.
*
* smallest file is assigned 100% font-size. largest file - 200%
* everything else is proportional in between.
*/
function build_cloud()
{
$output = '';
$files = array();
$dir = opendir(SEARCH_DIR);
$low_end=PHP_INT_MAX;
$high_end=0;
while($file = readdir($dir))
if($file != '.' && $file != '..' && $file != THIS_FILE && $file != CACHE_FILE)
{
$files[$file] = filesize(SEARCH_DIR."/".$file);
$low_end = min($low_end, $files[$file]);
$high_end = max($high_end, $files[$file]);
}
closedir($dir);
ksort($files);
foreach($files as $name=>$size)
{
$output .= "<a style=\"". get_style($low_end, $high_end, $size) . "\"".
"href=\"".PREFIX.
htmlentities($name)."\">".
htmlentities(str_replace('.php','',$name))."</a> ";
}
echo $output;

$fp = fopen(SEARCH_DIR.'_cloud_include_cache.html','w');
fwrite($fp, $output);
fclose($fp);
}
function get_style($low_end, $high_end, $size)
{
$net = $high_end - $low_end;
$font_size = (($size - $low_end) * 100) / $net + 100;

if(!$interval) $interval++;

return "font-size: ".$font_size."%;";
}
?>

Labels: ,

3 Comments:

Blogger Falko said...

Thanks, exactly what I was looking for. However I needed to change

str_replace('.php','',$name)

to

str_replace('.html','',$name)

in order to strip the file name extension.

7:38 PM  
Blogger kandi111777 said...

Where do I put the php code? I tried inside the body tag but then the code was visible on the page. So then I tried above the open html tag but the code was visible on the page. My blog is www.drrock.com/blog/index.html. Any help would be appreciated. Thanks!!

12:53 PM  
Blogger Joseph said...

Thanks for sharing these codes. They're really very helpful.

dental web design

4:37 AM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home