資料庫中,存放文章的表中有「Tag」字段,用來存放標籤。標籤之間以“,”分隔。例如「PHP,VB,隨筆」。
下面的實作程式碼,將標籤從資料庫中搜出來,並格式化處理,使其以出現的次數為依據顯示出不同大小的文字連結。
其中的細節,不做解釋了!
觀念陳、方法笨、效率低的標籤雲的實作程式碼如下:
複製程式碼 程式碼如下:
/**
* WNiaoBlog 標籤範本 ShowTag
*
* @package WNiaoBlog
*
* @subpackage 標籤
*/
//Connect the database
//include('../include/config .php');
/**
* CountTag() - 統計標籤出現的數量,並將資料儲存在兩個陣列中
*
* GetTag() - 從資料庫中存取標籤的標籤
*/
function CountTag($String){
$TagString = $String;
//echo $TagString."
";
$Tags = explode(",",
$Tags = explode(",",
$Tags = explode(",",
$Tags = explode(",",
$Tags = explode(",",
$Tags = explode(",",
$Tags = explode(",",
$Tags = explode(",",
$Tags = explode(",",
$Tags = explode(",",
$5 $TagString);
$n = 1;
$i = 0;
$Continue = TRUE;
//echo $Tags[1]."
";
//in case no-label'sarticle [$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;Tag $i+1][0] = $EachTag;
$EachTagStr[$i+1][1] = 1;
$Continue = FALSE;
}
}
} else { //initialize theTagarray $Each}[Stry $ []
$EachTagStr[$i][0] = $EachTag;
$EachTagStr[$i][1] = 1;
$Continue = FALSE;
}
}
$Continue = FALSE;
}
} ShowTag($Row,$ablink){
$i = 0;
while($Row[$i][0]){
$EachTag = $Row[$i][0];
$EachCount = $Row[ $i][1];
$Size = SetSize($EachCount);
echo " $i++;
}
}
function GetTag(){
$QuerySet = mylect_query("select * from article");
while($Row = mysql_fetch_array($QuerySet)){
$Tag = $Row['tag'];
$TagString = $TagString.",".$Tag;
}Tagh
re ;
}
function SetSize($Size){
$Size += 10;
if($Size > 30)
$Size = 30;
return $Size;
}
//Go
echo "
";
echo "標籤雲";
$String = GetTag();
$Row = CountTag($String);