///////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* 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 & 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 would use a css styling for deciding the size of the tag according to its weight,
* both of which would 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.",'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 .做出anether文件和将其命名为style.css文件。 Put the following code in it.把下面的代码。
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);