php mysql实现标签云的实例代码

发布时间:2019-10-26编辑:脚本学堂
现在php标签云效果很流行的,这里分享一段php代码,构造出一段奇妙的php 标签云效果。有需要的朋友参考下吧。

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样式表
 

复制代码 代码示例:
<style type="text/css">
.tag_cloud
 {padding: 3px; text-decoration: none;
 font-family: verdana;  }
.tag_cloud:link  { color: #FF66CC; }
.tag_cloud:visited { color: #9900FF; }
.tag_cloud:hover { color: #FF66CC; background: #000000; }
.tag_cloud:active { color: #6699FF; background: #000000; }
</style>

3,显示标签云的区域
 

复制代码 代码示例:
<div id="wrapper">
 <?php print tag_cloud(); ?>
</div>