国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

PHP和MySQL生成的標簽云實現(xiàn)代碼

2024-05-04 21:47:27
字體:
供稿:網(wǎng)友

用戶輸入文本和輸入的文本在過去的一個標簽云,標簽云是一個用戶生成的標簽的可視化描述,或只是一個網(wǎng)站的文字內(nèi)容,通常用來描述網(wǎng)站的內(nèi)容。

為此,我們將創(chuàng)建一個HTML表格,將接受用戶文本,也讓用戶可以看到從 MySQL數(shù)據(jù)庫,其中包含在過去輸入的文本生成的標簽云,代碼如下:

  1. <?php 
  2.  echo '<form method="post" action="tag_cloud_gen.php" name="gen_tag_db">'
  3.  echo '<p>Input your text here:<br /><textarea name="tag_input" rows="20" cols="80"></textarea></p>'
  4.  echo '<input type="submit" name="submit">'
  5.  echo '</form>'
  6. ?> 
  7. <br /> 
  8. <h3>OR</h3> 
  9. <br /> 
  10. <p>see the current tag cloud here</p> 
  11. <?php 
  12.  echo '<form name="show_tag_cloud" method="post" action="show_tag_cloud.php">'
  13.  echo '<input type="submit" value="show current tag cloud" >'
  14.  echo '</form>'
  15. ?> 

其中每個計算其頻率和對將進入一個數(shù)組,輸入的文本將被表征為單個詞。然后將這個數(shù)組存儲到一個MySQL數(shù)據(jù)庫,我們可以選擇保存在MySQL數(shù)據(jù)庫表coloumn存儲任何鏈接,如果這個項目未來的擴展。

1) tag_id —- int,primary key,auto increament 1)tag_id - 整型,主鍵,自動increament

2) keyword — varchar(20),unique 2)關鍵字 - 數(shù)據(jù)類型為varchar(20),獨特的

3) weight — int 3)重量 - 詮釋

4) link — varchar(256). 4)鏈接 - 為varchar(256)。

代碼如下:

  1. <?php 
  2. /** 
  3. * this function will update the mysql database table to reflect the new count of the keyword 
  4. * i.e. the sum of current count in the mysql database &amp;amp;amp;amp;amp; current count in the input. 
  5. */ 
  6. function update_database_entry($connection,$table,$keyword,$weight){ 
  7.  $string=$_POST['tag_input']; 
  8.  $connection = mysql_connect("localhost""root"""); 
  9.  /** 
  10.  * now comes the main part of generating the tag cloud 
  11.  * we would use a css styling for deciding the size of the tag according to its weight, 
  12.  * both of which would be fetched from mysql database. 
  13.  */ 
  14.  $query="select * from `tagcloud_db`.`tags` where keyword like '%$keyword%'"
  15.  $resultset=mysql_query($query,$connection); 
  16.  if(!$resultset){ 
  17.   die('Invalid query: ' . mysql_error()); 
  18.  } else { 
  19.   while($row=mysql_fetch_array($resultset)){ 
  20.   $query="UPDATE `tagcloud_db`.`tags` SET weight=".($row[2]+$weight)." where tag_id=".$row[0].";"
  21.   mysql_query($query,$connection); 
  22.  } 
  23. ?> 
  24. <?php 
  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<br />"
  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. print_r($words); 
  46. echo '<br /><br />'
  47. /** 
  48. * now enter the above array of word and corresponding count values into the database table 
  49. * in case the keyword already exist in the table then update the database table using the function 'update_database_entry(...)' 
  50. */ 
  51. $table="tagcloud_db"
  52. mysql_select_db($table,$connection); 
  53. foreach($words as $keyword=>$weight){ 
  54.  $query="INSERT INTO `tagcloud_db`.`tags` (keyword,weight,link) values ('".$keyword."',".$weight.",'NA')"
  55.  if(!mysql_query($query,$connection)){ 
  56.   if(mysql_errno($connection)==1062){ 
  57.    update_database_entry($connection,$table,$keyword,$weight); 
  58.   } 
  59.  } 
  60. mysql_close($connection); 
  61. ?> 

做出anether文件和將其命名為style.css文件,把下面的代碼:

  1. HTML, BODY 
  2. padding0
  3. border0px none
  4. font-familyVerdana
  5. font-weightnone
  6. .tags_div 
  7. padding3px
  8. border1px solid #A8A8C3
  9. background-colorwhite
  10. width500px
  11. -moz-border-radius: 5px
  12. H1 
  13. font-size16px
  14. font-weightnone
  15. A:link 
  16. color#676F9D
  17. text-decorationnone
  18. A:hover 
  19. text-decorationnone
  20. background-color#4F5AA1
  21. colorwhite

這將使我們的標簽云外觀漂亮,它保存為style.css的,再次,使一個新的PHP文件,并命名它show_tag_cloud.php。

在PHP代碼中,如下我們連接到MySQL數(shù)據(jù)庫,獲取所有的標簽,其重量和紐帶,然后計算每個使用它的重量及最小的標簽大小假定為標簽的大小,它也是每一個標簽從數(shù)據(jù)庫中檢索或與Google鏈接,如果沒有鏈接存在,即“不適用”的鏈接,代碼如下:

  1. <?php 
  2.  $connection = mysql_connect("localhost""root"""); 
  3.  $table="tagcloud_db"
  4.  $words=array(); 
  5.  $words_link=array(); 
  6.  mysql_select_db($table,$connection); 
  7.  $query="SELECT keyword,weight,link FROM `tagcloud_db`.`tags`;"
  8.  if($resultset=mysql_query($query,$connection)){ 
  9.   while($row=mysql_fetch_row($resultset)){ 
  10.    $words[$row[0]]=$row[1]; 
  11.    $words_link[$row[0]]=$row[2]; 
  12.   } 
  13.  } 
  14. // Incresing this number will make the words bigger; Decreasing will do reverse 
  15. $factor = 0.5; 
  16. // Smallest font size possible 
  17. $starting_font_size = 12; 
  18. // Tag Separator 
  19. $tag_separator = '&nbsp; &nbsp; &nbsp;'
  20. $max_count = array_sum($words); 
  21. ?> 
  22. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
  23. <HTML> 
  24.  <HEAD> 
  25.   <TITLE> Tag Cloud Generator </TITLE> 
  26.   <META NAME="Keywords" CONTENT="tag, cloud, php, mysql"
  27.   <META NAME="Description" CONTENT="A Tag Cloud using php and mysql"
  28.   <LINK REL="stylesheet" HREF="style.css" TYPE="text/css"
  29.  </HEAD> 
  30. <BODY> 
  31. <center><h1>Tag Cloud using php and mysql </h1><div align='center' class='tags_div'
  32. <?php 
  33. foreach($words as $tag => $weight ) 
  34.  $x = round(($weight * 100) / $max_count) * $factor
  35.  $font_size = $starting_font_size + $x.'px'
  36.  if($words_link[$tag]=='NA'echo "<span style='font-size: ".$font_size."; color: #676F9D;'><a href='http://www.google.co.in/search?hl=en&q=".$tag."&meta='>".$tag."</a></span>".$tag_separator
  37.  else echo "<span style='font-size: ".$font_size."; color: #676F9D;'><a href='http://".$words_link[$tag]."/'>".$tag."</a></span>".$tag_separator
  38. ?> 
  39. </div></center> 
  40. </BODY> 
  41. </HTML> 

現(xiàn)在把他們所有在您的Web服務器的根目錄,并觀看結(jié)果。 每個查詢會給你新的結(jié)果,隨著時間的推移,數(shù)據(jù)庫的增長。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 奉新县| 乐都县| 明星| 阳春市| 东阳市| 砀山县| 从化市| 高青县| 辉南县| 砀山县| 阿勒泰市| 栾城县| 沙坪坝区| 勃利县| 长沙县| 清涧县| 团风县| 上饶市| 柳林县| 榆中县| 轮台县| 永顺县| 武冈市| 山丹县| 海晏县| 扶余县| 甘谷县| 吉木萨尔县| 伊吾县| 任丘市| 贺州市| 上犹县| 永安市| 饶阳县| 武定县| 衡水市| 安平县| 布拖县| 广西| 武宁县| 长沙县|