In the database, there is a "Tag" field in the table storing the article to store the Tag. Tags are separated by commas. For example, "PHP, VB, essay ".
The following implementation code searches for tags from the database and formats them so that they can display text connections of different sizes based on the number of occurrences.
The details are not explained!
The implementation code of Tag Cloud is as follows:
Copy codeThe Code is as follows :/**
* 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 "tag cloud ";
$ String = GetTag ();
$ Row = CountTag ($ String );
ShowTag ($ Row, $ ablink );
Echo"
";
?>
OK, DONE!