Home  >  Article  >  Backend Development  >  Tag cloud implementation code generated by PHP and MySQL_PHP tutorial

Tag cloud implementation code generated by PHP and MySQL_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:56:17861browse

This article introduces a way of life about tag cloud. We use php and mysql to implement it. Students in need can refer to it.

The user enters text and the entered text goes over a tag cloud. A tag cloud is a visual depiction of user-generated tags, or simply the textual content of a website, often used to describe the content of the website.

To do this, we will create an HTML form that will accept user text and also allow the user to see a tag cloud generated from a MySQL database that contains text entered in the past.

 代码如下 复制代码
echo '
';
 echo '

Input your text here:

';
 echo '';
 echo '
';
?>


OR




see the current tag cloud here


echo '
';
 echo '';
 echo '
';
?>

Each of them calculates its frequency and the pairs will go into an array and the input text will be characterized as a single word. This array is then stored into a MySQL database and we can optionally store any links in the MySQL database table coloumn if this project is expanded in the future.

1) tag_id —- int, primary key, auto increament 1) tag_id - integer, primary key, auto increament

2) keyword — varchar(20),unique 2) keyword — the data type is varchar(20), unique

3) weight — int 3) weight — int

4) link — varchar(256). 4) link — varchar(256).

The code is as follows Copy code
 代码如下 复制代码

///////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* 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);
?>

////////////////////////////////////////////////////// ////////////////////////////////////////////////////// ///
/**
* 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. Make anether file and name it style.css file. Put the following code in it. Put the following code in it.

.tags_div {
The code is as follows
 代码如下 复制代码

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;
}

Copy code



HTML, BODY {

padding: 0;

border: 0px none;
 代码如下 复制代码

$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 = ' ';
$max_count = array_sum($words);

?>


 
   Tag Cloud Generator
  
  
  
 

Tag Cloud using php and mysql


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 "".$tag."".$tag_separator;
 else echo "".$tag."".$tag_separator;
}
?>


font-family: Verdana;

font-weight: none;

}
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 nice, save it as style.css. Again, make a new PHP file and name it show_tag_cloud.php. In the PHP code, as follows we connect to the MySQL database and get all the tags, their weights and ties. Then calculate the weight of each tag using it and the minimum tag size assumed to be the size of the tag, it is also the link for every tag retrieved from the database or linked with Google, if no link exists, i.e. "not applicable"
The code is as follows Copy code
$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];<🎜> }<🎜> }<🎜> // Increasing 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);<🎜> <🎜>?> Tag Cloud Generator

Tag Cloud using php and mysql

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 "".$tag."".$tag_separator; else echo "".$tag."".$tag_separator; } ?>
Now put them all in the root directory of your web server and watch the results. Each query will give you new results as time goes by and the database grows.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631619.htmlTechArticleThis article introduces an article about the life method of tag cloud. We use php and mysql to implement it. If necessary Students can refer to it. The user enters text and the entered text was in the last one...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn