Tag cloud implementation code generated by PHP and MySQL

Source: Internet
Author: User
Tags strtok
User input text and input text are stored in the previous tag cloud. Tag Cloud is a visual description of a user generated tag, or is just the text content of a website, it is usually used to describe the content of a website. To this end, we will create an HTML table that accepts user text and allows users... user input text and input text are stored in the previous tag cloud. Tag Cloud is a visual description of a user generated tag, or is just the text content of a website, it 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 allow the user to see the tag cloud generated from the MySQL database, which contains the text entered in the past. the code is as follows:

 
OR

see the current tag cloud here

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:

 
  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 . ",'NA')"; if (!mysql_query($query, $connection)) { if (mysql_errno($connection) == 1062) { update_database_entry($connection, $table, $keyword, $weight); } }}mysql_close($connection);?>

Create an anetherfile and name it style.css. run the following code:

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 label cloud appearance beautiful, and save it as style.css. once again, 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, and then calculate the weight and minimum tag size for each of them, which is assumed to be the tag size, it is also a link for every tag to be retrieved from the database or linked to Google. If no link exists, it is a "not applicable" link. the code is as follows:

        Tag Cloud Generator    
    
    
     
 
  Tag Cloud using php and mysql 
  

$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.

Tutorial link:

Reprint at will ~ However, please keep the tutorial address★

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.