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: |
Copy code |
<? Php Echo '<form method = "post" action = "tag_cloud_gen.php" name = "gen_tag_db"> '; Echo '<p> Input your text here: <br/> <textarea name = "tag_input" rows = "20" cols = "80"> </textarea> </p> '; Echo '<input type = "submit" name = "submit"> '; Echo '</form> '; ?> <Br/> <H3> OR <Br/> <P> see the current tag cloud here </p> <? Php Echo '<form name = "show_tag_cloud" method = "post" action = "show_tag_cloud.php"> '; Echo '<input type = "submit" value = "show current tag cloud"> '; Echo '</form> '; ?>
|
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: |
Copy code |
<? Php //////////////////////////////////////// //////////////////////////////////////// /////////////////////// /** * 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 ); } } } ?> <? Php /* * 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 <br/> "; $ 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 '<br/> '; /** * 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: |
Copy code |
HTML, BODY { Padding: 0; Border: 0px none; Font-family: Verdana; Font-weight: none; } . Tags_div { 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: |
Copy code |
<? Php $ 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 = '& nbsp ;'; $ Max_count = array_sum ($ words ); ?> <! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN"> <HTML> <HEAD> <TITLE> Tag Cloud Generator </TITLE> <Meta name = "Keywords" CONTENT = "tag, cloud, php, mysql"> <Meta name = "Description" CONTENT = "A Tag Cloud using php and mysql"> <Link rel = "stylesheet" HREF = "style.css" TYPE = "text/css"> </HEAD> <BODY> <Center> <? Php 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 "<span style = 'font-size :". $ font_size. "; color: # 676F9D; '> <a href = 'http: // www. google. co. in/search? Hl = en & q = ". $ tag." & meta = '> ". $ tag." </a> </span> ". $ tag_separator; Else echo "<span style = 'font-size :". $ font_size. "; color: # 676F9D; '> <a href = 'http ://". $ words_link [$ tag]. "/'> ". $ tag. "</a> </span> ". $ tag_separator; } ?> </Div> </center> </BODY> </HTML> |
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.