欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php标签云的实现代码

程序员文章站 2022-11-27 18:04:18
数据库中,存放文章的表中有“tag”字段,用来存放标签。标签之间用“,”分隔。比如“php,vb,随笔”。 下面的实现代码,将标签从数据库中搜出来,并格式化处理,使其以出现...
数据库中,存放文章的表中有“tag”字段,用来存放标签。标签之间用“,”分隔。比如“php,vb,随笔”。
下面的实现代码,将标签从数据库中搜出来,并格式化处理,使其以出现的次数为依据显示出不同大小的文字连接。
其中的细节,不做解释了!
观念陈、方法笨、效率低的标签云的实现代码如下:
复制代码 代码如下:

/**
* wniaoblog tag template showtag
*
* @package wniaoblog
*
* @subpackage tag
*/
//connect the database
//include('../include/config.php');
/**
* counttag() - statistics labels appear the number,and the data to be stored in the two array
*
* gettag() - access the tag's labels from the database
*/
function counttag($string){
$tagstring = $string;
//echo $tagstring."
";
$tags = explode(",",$tagstring);
$n = 1;
$i = 0;
$continue = true;
//echo $tags[1]."
";
//in case no-label's article
while($tags[$n] or $tags[++$n] or $tags[++$n] ){
$eachtag = $tags[$n++];
//echo $eachtag."
";
$continue = true;
for($i=0;$continue;$i++){
if( $eachtagstr[$i][0] ) {
if( $eachtagstr[$i][0] == $eachtag ){
$eachtagstr[$i][1]++;
$continue = false;
}
else {
if( $eachtagstr[$i+1][0] ) $continue = true;
else {
$eachtagstr[$i+1][0] = $eachtag;
$eachtagstr[$i+1][1] = 1;
$continue = false;
}
}
} else { //initialize the array $eachtagstr[][]
$eachtagstr[$i][0] = $eachtag;
$eachtagstr[$i][1] = 1;
$continue = false;
}
}
}
return $eachtagstr;
}
function showtag($row,$ablink){
$i = 0;
while($row[$i][0]){
$eachtag = $row[$i][0];
$eachcount = $row[$i][1];
$size = setsize($eachcount);
echo " < a style='color:blue ; font-size:".$size." ' onmouseover=this.style.color='#900000' onmouseout=this.style.color='blue' href='".$ablink."tag?tag=".$eachtag."' target='_self' > ".$eachtag."(".$eachcount.")"." ";
$i++;
}
}
function gettag(){
$queryset = mysql_query("select * from article");
while($row = mysql_fetch_array($queryset)){
$tag = $row['tag'];
$tagstring = $tagstring.",".$tag;
}
return $tagstring;
}
function setsize($size){
$size += 10;
if($size > 30)
$size = 30;
return $size;
}
//go
echo "
";
echo "标签云";
$string = gettag();
$row = counttag($string);
showtag($row,$ablink);
echo "
";
?>

ok,done!