PHP and MySQL label cloud implementation code _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags strtok
The tag cloud implementation code generated by PHP and MySQL. This article introduces how to use tag cloud. We use php and mysql for implementation. if you need it, please refer to it. In the past, the text and text entered by the user introduced a way of life about tag cloud. We use php and mysql for implementation. if you need it, please refer to it.

The text entered by the user and the text entered are in the previous tag cloud. A tag cloud is a visual description of a user-generated tag, or a text content of a website, which is usually used to describe the content of a website.

To this end, we will create an HTML table that will accept the user text and let the user see the tag cloud generated from the MySQL database, which contains the text entered in the past.

The code is as follows:
Echo '';
?>


OR


See the current tag cloud here


Echo '';
?>

Each calculation frequency and pair will enter an array, and the input text will be represented as a single word. Then store the array to a MySQL database. we can choose to store any link in the MySQL database table coloumn if the project will be extended in the future.

1) tag_id -- int, primary key, auto increament 1) tag_id-integer, primary key, automatic increament

2) keyword-varchar (20), unique 2) keyword-data type: varchar (20), unique

3) weight-int 3) weight-interpretation

4) link-varchar (256). 4) link-varchar (256 ).

The code is as follows:

//////////////////////////////////////// //////////////////////////////////////// ///////////////////////
/**
* This function will update the mysql database table to reflect the new count of the keyword
* I. e. the sum of current count in the mysql database & amp; current count in the input.
*/
Function update_database_entry ($ connection, $ table, $ keyword, $ weight ){

$ String = $ _ POST ['tag _ input'];
$ Connection = mysql_connect ("localhost", "root ","");
/**
* Now comes the main part of generating the tag cloud
* We wocould use a css styling for deciding the size of the tag according to its weight,
* Both of which wocould be fetched from mysql database.
*/

$ Query = "select * from 'tagcloud _ db'. 'tags' where keyword like '% $ keyword % '";
$ Resultset = mysql_query ($ query, $ connection );

If (! $ Resultset ){
Die ('invalid query: '. mysql_error ());
} Else {
While ($ row = mysql_fetch_array ($ resultset )){
$ Query = "UPDATE 'tagcloud _ db '. 'tags' SET weight = ". ($ row [2] + $ weight ). "where tag_id = ". $ row [0]. ";";
Mysql_query ($ query, $ connection );
}
}
}
?>
/*
* Get the input string from the post and then tokenize it to get each word, save the words in an array
* In case the word is repeated add'1' to the existing words counter
*/
$ Count = 0;
$ Tok = strtok ($ string, "t ,;.'"! &-'NR "); // considering line-return, line-feed, white space, comma, ampersand, tab, etc... as word separator
If (strlen ($ tok)> 0) $ tok = strtolower ($ tok );
$ Words = array ();
$ Words [$ tok] = 1;
While ($ tok! = False ){
Echo "Word = $ tok
";
$ Tok = strtok ("t ,;.'"! &-'NR ");
If (strlen ($ tok)> 0 ){
$ Tok = strtolower ($ tok );
If ($ words [$ tok]> = 1 ){
$ Words [$ tok] = $ words [$ tok] + 1;
} Else {
$ Words [$ tok] = 1;
}
}
}
Print_r ($ words );
Echo'

';
/**
* Now enter the above array of word and corresponding count values into the database table
* In case the keyword already exist in the table then update the database table using the function 'update _ database_entry (...)'
*/
$ Table = "tagcloud_db ";
Mysql_select_db ($ table, $ connection );
Foreach ($ words as $ keyword => $ weight ){
$ Query = "insert into 'tagcloud _ db '. 'tags' (keyword, weight, link) values ('". $ keyword. "',". $ weight. ", 'A ')";
If (! Mysql_query ($ query, $ connection )){
If (mysql_errno ($ connection) = 1062 ){
Update_database_entry ($ connection, $ table, $ keyword, $ weight );
}
}
}
Mysql_close ($ connection );
?>

Make anether file and name it style.css. Put the following code in it. Put the following code.

The code is as follows:

HTML, BODY
{
Padding: 0;
Border: 0px none;
Font-family: Verdana;
Font-weight: none;
}
. Tags_p
{
Padding: 3px;
Border: 1px solid # A8A8C3;
Background-color: white;
Width: 500px;
-Moz-border-radius: 5px;
}
H1
{
Font-size: 16px;
Font-weight: none;
}
A: link
{
Color: # 676F9D;
Text-decoration: none;
}
A: hover
{
Text-decoration: none;
Background-color: #4F5AA1;
Color: white;
}

This will make our labels look beautiful on the cloud and save them as style.css.
Make a new php file and name it show_tag_cloud.php.
In the PHP code, we connect to the MySQL database as follows to obtain all the labels, their weights and links.

Calculate the weight of each tag and the minimum tag size. assume that the tag is the size of each tag. it is also retrieved from the database or linked to Google. If no link exists, that is, "not applicable" links

The code is as follows:

$ Connection = mysql_connect ("localhost", "root ","");
$ Table = "tagcloud_db ";
$ Words = array ();
$ Words_link = array ();
Mysql_select_db ($ table, $ connection );
$ Query = "SELECT keyword, weight, link FROM 'tagcloud _ db'. 'tags ';";

If ($ resultset = mysql_query ($ query, $ connection )){
While ($ row = mysql_fetch_row ($ resultset )){
$ Words [$ row [0] = $ row [1];
$ Words_link [$ row [0] = $ row [2];
}
}
// Incresing this number will make the words bigger; Decreasing will do reverse
$ Factor = 0.5;

// Smallest font size possible
$ Starting_font_size = 12;

// Tag Separator
$ Tag_separator = '';
$ Max_count = array_sum ($ words );

?>



Tag Cloud Generator





Tag Cloud using php and mysql


Foreach ($ words as $ tag => $ weight)
{
$ X = round ($ weight * 100)/$ max_count) * $ factor;
$ Font_size = $ starting_font_size + $ x. 'px ';
If ($ words_link [$ tag] = 'na') echo "". $ tag. "". $ tag_separator;
Else echo "". $ tag. "". $ tag_separator;
}
?>



Now put all of them in the root directory of your Web server and view the results. Each query will give you new results, and the database will grow over time.

Bytes. User input text and input text in the past...

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.