标签云 php_php与css 标签云效果

这段代码展示了如何使用PHP创建一个动态的标签云。通过查询数据库获取每个标签关联的内容数量,然后根据数量比例调整标签的字体大小,实现视觉上的权重区分。CSS部分则定义了不同数量级标签的字体大小,使得标签云更具有层次感。
摘要由CSDN通过智能技术生成

以下代码,实现一个标签云的效果。

只给出核心代码,供大家学习参考。

1,创建标签云的函数

复制代码 代码示例:

function createTagCloud($tags)

{

//I pass through an array of tags

$i=0;

foreach($tags as $tag)

{

$id = $tag['id']; //the tag id, passed through

$name = $tag['tag']; //the tag name, also passed through in the array

//using the mysql count command to sum up the tutorials tagged with that id

$sql = "SELECT COUNT(*) AS totalnum FROM tutorials WHERE tags LIKE '%".$id."%' AND published = 1";

//create the resultset and return it

$res = mysql_query($sql);

$res = mysql_fetch_assoc($res);

//check there are results ;)

if($res)

{

//build an output array, with the tag-name and the number of results

$output[$i]['tag'] = $name;

$output[$i]['num'] = $res['totalnum'];

}

$i++;

}

/*this is just calling another function that does a similar SQL statement, but returns how many pieces of content I have*/

$total_tuts = $this->getNumberOfTutorials();

//ugh, XHTML in PHP?  Slap my hands - this isn't best practice, but I was obviously feeling lazy

$html = '

  • ';

//iterate through each item in the $output array (created above)

foreach($output as $tag)

{

//get the number-of-tag-occurances as a percentage of the overall number

$ratio = (100 / $total_tuts) * $tag['num'];

//round the number to the nearest 10

$ratio =  round($ratio,-1);

/*append that classname onto the list-item, so if the result was 20%, it comes out as cloud-20*/

$html.= '

'.$tag['tag'].'';

}

//close the UL

$html.= '

';

return $html;

}

2,css代码部分

/*删除默认的列表样式,使之成为一个普通的清单列表*/

复制代码 代码示例:

.home-item ul.tagcloud

{

list-style-type:none;

margin:0px;

padding:0px;

}

/*设置li的样式*/

.home-item ul.tagcloud li

{

display:inline !important;

margin-right:15px;

line-height:2em;

}

.home-item ul.tagcloud li a

{

display:inline;

}

/*标签云的效果*/

.home-item ul.tagcloud li.cloud-10 a

{

font-size:110%;

}

.home-item ul.tagcloud li.cloud-20 a

{

font-size:120%;

}

.home-item ul.tagcloud li.cloud-30 a

{

font-size:130%;

}

/*************************************

you get the idea, i'm skipping a few

*************************************/

.home-item ul.tagcloud li.cloud-90 a

{

font-size:190%;

}

.home-item ul.tagcloud li.cloud-100 a

{

font-size:200%;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值