Php tag cloud implementation code

Source: Internet
Author: User
The following implementation code searches for tags from the database and formats the tags so that they are displayed based on the number of occurrences to connect to the database, the table storing the article contains the "Tag" field, which is used to store tags. 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:
The 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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.