Wednesday, 24 October 2018

Generating Tag cloud with PHP and MySQL

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.
';
 echo 'Input your text here:';
 echo '';
 echo '';
?>

OR

see the current tag cloud here
';
 echo '';
 echo '';
?>
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.

0) $tok=strtolower($tok);
 $words=array();
 $words[$tok]=1;
 while ($tok !== false) {
  echo "Word=$tok";
  $tok = strtok(" \t,;.\'\"!&-`\n\r");
  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);
?>
Make anether file and name it style.css . Put the following code in it.
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 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 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 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:
a sample tag cloud on my computer
This tag cloud was generated using the following data:
                     tag_id;keyword;weight;link
“1”;”vimal”;”7″;”www.vimalkumarpatel.blogspot.com”
“2”;”scet”;”5″;”NA”
“3”;”engg”;”2″;”NA”
“4”;”0″;”1″;”NA”
“7”;”google”;”5″;”NA”
“8”;”cool”;”2″;”NA”
“9”;”orkut”;”3″;”NA”

0 comments:

Post a Comment