php与mysql结合实现的标签云效果,完整代码,如下:
<?php /** * php与mysql实现的标签云效果 * by www.jb200.com */ $db_host = "localhost"; $db_user = "db_user"; $db_pass = "pass"; $db_name = "db_name"; mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error()); mysql_select_db($db_name); function tag_info() { $result = mysql_query("SELECT * FROM tags GROUP BY tag ORDER BY count DESC"); while($row = mysql_fetch_array($result)) { $arr[$row['tag']] = $row['count']; } ksort($arr); return $arr; } function tag_cloud() { $min_size = 10; $max_size = 30; $tags = tag_info(); $minimum_count = min(array_values($tags)); $maximum_count = max(array_values($tags)); $spread = $maximum_count - $minimum_count; if($spread == 0) { $spread = 1; } $cloud_html = ''; $cloud_tags = array(); // create an array to hold tag code foreach ($tags as $tag => $count) { $size = $min_size + ($count - $minimum_count) * ($max_size - $min_size) / $spread; $cloud_tags[] = '<a style="font-size: '. floor($size) . 'px' . '" class="tag_cloud" target="_blank" href="http://www.jb200.com/index.php?s=' . $tag . '" title="'' . $tag . '' returned a count of ' . $count . '">' . htmlspecialchars(stripslashes($tag)) . '</a>'; } $cloud_html = join("n", $cloud_tags) . "n"; return $cloud_html; } ?>
2,css样式表
3,显示标签云的区域