The basic idea this time is to present a way to form a tag cloud from user input text and text entered in the past. A tag cloud is a visual depiction of user-generated tags, or simply the word content of a site, used typically to describe the content of web sites.
for this we will create an HTML form that will accept user text & also allow user to see tag cloud generated from mysql database which contains the text entered in the past.
<textarea name=”tag_input” rows=”20″ cols=”80″>
The entered text will be tokenized into single words with php function strtok(), each of which will have its frequency counted and the pair will go into an array. This array will then be stored into a mysql database, we can optionally keep a coloumn in the mysql database table to store links if any for future expansion of this project.
1) tag_id —- int,primary key,auto increament
2) keyword — varchar(20),unique
3) weight — int
4) link — varchar(256).
Next make a php file and name it tag_cloud_gen.php . The php code written in following lines just make an array ‘$words’ which has keyword in lower case & its frequency association from input text. The pairs from the array are then feed into the mysql database ‘tagcloud_db’ which has a table callet ‘tags’ whose columns are listed above. On encountering error the mysql _errrno() returns error number. While feeding the word array to the tags table duplication may occour, because of the previous entries, so we check whether this is the case by comparing the number returned to ’1062′ which indicates duplicate field present in the table (the keyword coloumn of table has unique constraint). On encountering this we simply update the mysql database to include the count of this input word/tag too.
Make another file and name it style.css . Put the following code in it.
This will make our tag cloud look pretty, save it as style.css .
again make a new php file and name it show_tag_cloud.php .
In the php code that follows we connect to mysql database, fetch back all the tags, its weight and link.
Then it calculates the size for each tag using its weight & minimum assumed size for tags, it also associates each tag the link retrieved from the database or with a google link if no link was there i.e. ‘NA’
Tag Cloud using php
now put them all in your webserver’s root directory and watch the results. Each query will give you new results over time as the database grows.
a sample output from my tag cloug looks like this:
This tag cloud was generated using the following data: