效果网站 - 自动化1102: http://huxuemail.web-104.com
乌鸟heart - 博客园: http://www.cnblogs.com/wuniaoheart/
数据库中,存放文章的表中有“Tag”字段,用来存放标签。标签之间用“,”分隔。比如“PHP,VB,随笔”。
下面的实现代码,将标签从数据库中搜出来,并格式化处理,使其以出现的次数为依据显示出不同大小的文字连接。
其中的细节,不做解释了!
观念陈、方法笨、效率低的标签云的实现代码如下:
1 <?PHP 2 /** 3 * WNiaoBlog Tag Template ShowTag 4 * 5 * @package WNiaoBlog 6 * 7 * @subpackage Tag 8 */ 9 10 //Connect the database 11 //include('../include/config.php'); 12 13 /** 14 * CountTag() - Statistics labels appear the number,and the data to be stored in the two array 15 * 16 * GetTag() - Access the Tag's Labels from the database 17 */ 18 19 function CountTag($String){ 20 $TagString = $String; 21 //echo $TagString."<br><br><br>"; 22 $Tags = explode(",",$TagString); 23 $n = 1; 24 $i = 0; 25 $Continue = TRUE; 26 //echo $Tags[1]."<br><br><br>"; 27 //in case no-label's article 28 while($Tags[$n] OR $Tags[++$n] OR $Tags[++$n] ){ 29 $EachTag = $Tags[$n++]; 30 //echo $EachTag."<br><br><br>"; 31 $Continue = TRUE; 32 for($i=0;$Continue;$i++){ 33 if( $EachTagStr[$i][0] ) { 34 if( $EachTagStr[$i][0] == $EachTag ){ 35 $EachTagStr[$i][1]++; 36 $Continue = FALSE; 37 } 38 else { 39 if( $EachTagStr[$i+1][0] ) $Continue = TRUE; 40 else { 41 $EachTagStr[$i+1][0] = $EachTag; 42 $EachTagStr[$i+1][1] = 1; 43 $Continue = FALSE; 44 } 45 } 46 } else { //initialize the array $EachTagStr[][] 47 $EachTagStr[$i][0] = $EachTag; 48 $EachTagStr[$i][1] = 1; 49 $Continue = FALSE; 50 } 51 } 52 } 53 return $EachTagStr; 54 } 55 56 function ShowTag($Row,$ablink){ 57 $i = 0; 58 while($Row[$i][0]){ 59 $EachTag = $Row[$i][0]; 60 $EachCount = $Row[$i][1]; 61 $Size = SetSize($EachCount); 62 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.")"." </a>"; 63 $i++; 64 } 65 } 66 67 function GetTag(){ 68 $QuerySet = mysql_query("select * from article"); 69 while($Row = mysql_fetch_array($QuerySet)){ 70 $Tag = $Row['tag']; 71 $TagString = $TagString.",".$Tag; 72 } 73 return $TagString; 74 } 75 76 function SetSize($Size){ 77 $Size += 10; 78 if($Size > 30) 79 $Size = 30; 80 return $Size; 81 } 82 83 //Go 84 echo "<div id=showtag>"; 85 echo "<p class='med1' style='color:#046111' >标签云</p>"; 86 $String = GetTag(); 87 $Row = CountTag($String); 88 ShowTag($Row,$ablink); 89 echo "</div>"; 90 91 ?>
OK,DONE!