php专区

 首页 > php专区 > PHP应用 > 常用功能 > PHP和MySQL生成的标签云实现代码 - php高级应用

PHP和MySQL生成的标签云实现代码 - php高级应用

分享到:
【字体:大 中 小】
导读:
          用户输入文本和输入的文本在过去的一个标签云,标签云是一个用户生成的标签的可视化描述,或只是一个网站的文字内容,通常用来描述网站的内容...

PHP和MySQL生成的标签云实现代码

用户输入文本和输入的文本在过去的一个标签云,标签云是一个用户生成的标签的可视化描述,或只是一个网站的文字内容,通常用来描述网站的内容。

为此,我们将创建一个HTML表格,将接受用户文本,也让用户可以看到从 MySQL数据库,其中包含在过去输入的文本生成的标签云,代码如下:

  1.  echo ''; 
  2.  echo '

    Input your text here:

    '
    ; 
  3.  echo ''; 
  4.  echo ''; 
  5. ?> 
  6.  
  7. OR

     
  8.  
  9. see the current tag cloud here

     
  10.  echo ''; 
  11.  echo ''; 
  12.  echo ''; 
  13. ?> 

其中每个计算其频率和对将进入一个数组,输入的文本将被表征为单个词。然后将这个数组存储到一个MySQL数据库,我们可以选择保存在MySQL数据库表coloumn存储任何链接,如果这个项目未来的扩展。

1) tag_id —- int,primary key,auto increament 1)tag_id - 整型,主键,自动increament

2) keyword — varchar(20),unique 2)关键字 - 数据类型为varchar(20),独特的

3) weight — int 3)重量 - 诠释

4) link — varchar(256). 4)链接 - 为varchar(256)。

代码如下:

  1. /** 
  2. * this function will update the mysql database table to reflect the new count of the keyword 
  3. * i.e. the sum of current count in the mysql database & current count in the input. 
  4. */ 
  5. function update_database_entry($connection,$table,$keyword,$weight){ 
  6.  $string=$_POST['tag_input']; 
  7.  $connection = mysql_connect("localhost", "root", ""); 
  8.  /** 
  9.  * now comes the main part of generating the tag cloud 
  10.  * we would use a css styling for deciding the size of the tag according to its weight, 
  11.  * both of which would be fetched from mysql database. 
  12.  */ 
  13.  $query="select * from `tagcloud_db`.`tags` where keyword like '%$keyword%'"; 
  14.  $resultset=mysql_query($query,$connection); 
  15.  if(!$resultset){ 
  16.   die('Invalid query: ' . mysql_error()); 
  17.  } else { 
  18.   while($row=mysql_fetch_array($resultset)){ 
  19.   $query="UPDATE `tagcloud_db`.`tags` SET weight=".($row[2]+$weight)." where tag_id=".$row[0].";"; 
  20.   mysql_query($query,$connection); 
  21.  } 
  22. } 
  23. } 
  24. ?> 
  25. /* 
  26. * get the input string from the post and then tokenize it to get each word, save the words in an array 
  27. * in case the word is repeated add '1' to the existing words counter 
  28. */ 
  29.  $count=0; 
  30.  $tok = strtok($string, " t,;.'"!&-`nr");//considering line-return,line-feed,white space,comma,ampersand,tab,etc... as word separator 
  31.  if(strlen($tok)>0) $tok=strtolower($tok); 
  32.  $words=array(); 
  33.  $words[$tok]=1; 
  34.  while ($tok !== false) { 
  35.   echo "Word=$tok"; 
  36.   $tok = strtok(" t,;.'"!&-`nr"); 
  37.   if(strlen($tok)>0) { 
  38.   $tok=strtolower($tok); 
  39.   if($words[$tok]>=1){ 
  40.    $words[$tok]=$words[$tok] + 1; 
  41.   } else { 
  42.    $words[$tok]=1; 
  43.   } 
  44.  } 
  45. } 
  46. print_r($words); 
  47. echo ''; 
  48. /** 
  49. * now enter the above array of word and corresponding count values into the database table 
  50. * in case the keyword already exist in the table then update the database table using the function 'update_database_entry(...)' 
  51. */ 
  52. $table="tagcloud_db"; 
  53. mysql_select_db($table,$connection); 
  54. foreach($words as $keyword=>$weight){ 
  55.  $query="INSERT INTO `tagcloud_db`.`tags` (keyword,weight,link) values ('".$keyword."',".$weight.",'NA')"; 
  56.  if(!mysql_query($query,$connection)){ 
  57.   if(mysql_errno($connection)==1062){ 
  58.    update_database_entry($connection,$table,$keyword,$weight); 
  59.   } 
  60.  } 
  61. } 
  62. mysql_close($connection); 
  63. ?> 

做出anether文件和将其命名为style.css文件,把下面的代码:

  1. HTML, BODY 
  2. { 
  3. padding: 0; 
  4. border: 0px none; 
  5. font-family: Verdana; 
  6. font-weight: none; 
  7. } 
  8. .tags_div 
  9. { 
  10. padding: 3px; 
  11. border: 1px solid #A8A8C3; 
  12. background-color: white; 
  13. width: 500px; 
  14. -moz-border-radius: 5px; 
  15. } 
  16. H1 
  17. { 
  18. font-size: 16px; 
  19. font-weight: none; 
  20. } 
  21. A:link 
  22. { 
  23. color: #676F9D; 
  24. text-decoration: none; 
  25. } 
  26. A:hover 
  27. { 
  28. text-decoration: none; 
  29. background-color: #4F5AA1; 
  30. color: white; 
  31. } 

这将使我们的标签云外观漂亮,它保存为style.css的,再次,使一个新的PHP文件,并命名它show_tag_cloud.php。

在PHP代码中,如下我们连接到MySQL数据库,获取所有的标签,其重量和纽带,然后计算每个使用它的重量及最小的标签大小假定为标签的大小,它也是每一个标签从数据库中检索或与Google链接,如果没有链接存在,即“不适用”的链接,代码如下:

  1.  $connection = mysql_connect("localhost", "root", ""); 
  2.  $table="tagcloud_db"; 
  3.  $words=array(); 
  4.  $words_link=array(); 
  5.  mysql_select_db($table,$connection); 
  6.  $query="SELECT keyword,weight,link FROM `tagcloud_db`.`tags`;"; 
  7.  if($resultset=mysql_query($query,$connection)){ 
  8.   while($row=mysql_fetch_row($resultset)){ 
  9.    $words[$row[0]]=$row[1]; 
  10.    $words_link[$row[0]]=$row[2]; 
  11.   } 
  12.  } 
  13. // Incresing this number will make the words bigger; Decreasing will do reverse 
  14. $factor = 0.5; 
  15. // Smallest font size possible 
  16. $starting_font_size = 12; 
  17. // Tag Separator 
  18. $tag_separator = '     '; 
  19. $max_count = array_sum($words); 
  20. ?> 
  21. "-//W3C//DTD HTML 4.0 Transitional//EN"> 
  22.  
  23.   
  24.    Tag Cloud Generator  
  25.   "Keywords" CONTENT="tag, cloud, php, mysql"> 
  26.   "Description" CONTENT="A Tag Cloud using php and mysql"> 
  27.   "stylesheet" HREF="style.css" TYPE="text/css"> 
  28.   
  29.  
  30. Tag Cloud using php and mysql 

    'center' class='tags_div'> 
  31. foreach($words as $tag => $weight ) 
  32. { 
  33.  $x = round(($weight * 100) / $max_count) * $factor; 
  34.  $font_size = $starting_font_size + $x.'px'; 
  35.  if($words_link[$tag]=='NA') echo ".$font_size."; color: #676F9D;'>.$tag."&meta='>".$tag."".$tag_separator; 
  36.  else echo ".$font_size."; color: #676F9D;'>.$words_link[$tag]."/'>".$tag."".$tag_separator; 
  37. } 
  38. ?> 
 
  •  
  •  
  • 现在把他们所有在您的Web服务器的根目录,并观看结果。 每个查询会给你新的结果,随着时间的推移,数据库的增长。

    分享到:
    php生成EXCEL文档实例程序 - php高级应用
    php生成EXCEL文档实例程序 原生态的写法 原始方式:发送header,用附件的表头发送到用户浏览器表示是要下载的,然后读出数据库中的数据,一条一条的解析,写入excel格式的文件中,代码如下:   完整实例代码如下:
    php 实现jquery+ajax 跨域数据调用实例 -...
    php 实现jquery+ajax 跨域数据调用实例 本文章是基于jquery的ajax来实现数据跨域调用,主要是利用php实时返回json数据,这样就方便的实现的ajax跨域数据调用了。 jquery文件我这里不提供下载了,大家可以直接到google去下载哦,可以在页面定义一个调用方法,代码如下: function getData(...
    •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
    • 在这里……