用戶輸入文本和輸入的文本在過去的一個標簽云,標簽云是一個用戶生成的標簽的可視化描述,或只是一個網站的文字內容,通常用來描述網站的內容。
為此,我們將創建一個HTML表格,將接受用戶文本,也讓用戶可以看到從 MySQL數據庫,其中包含在過去輸入的文本生成的標簽云,代碼如下:
- <?php
- echo '<form method="post" action="tag_cloud_gen.php" name="gen_tag_db">';
- echo '<p>Input your text here:<br /><textarea name="tag_input" rows="20" cols="80"></textarea></p>';
- echo '<input type="submit" name="submit">';
- echo '</form>';
- ?>
- <br />
- <h3>OR</h3>
- <br />
- <p>see the current tag cloud here</p>
- <?php
- echo '<form name="show_tag_cloud" method="post" action="show_tag_cloud.php">';
- echo '<input type="submit" value="show current tag cloud" >';
- echo '</form>';
- ?>
其中每個計算其頻率和對將進入一個數組,輸入的文本將被表征為單個詞。然后將這個數組存儲到一個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)。
代碼如下:
- <?php
- /**
- * 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 &amp;amp;amp;amp; 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);
- }
- }
- }
- ?>
- <?php
- /*
- * 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<br />";
- $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 '<br /><br />';
- /**
- * 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);
- ?>
做出anether文件和將其命名為style.css文件,把下面的代碼:
- 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;
- }
這將使我們的標簽云外觀漂亮,它保存為style.css的,再次,使一個新的PHP文件,并命名它show_tag_cloud.php。
在PHP代碼中,如下我們連接到MySQL數據庫,獲取所有的標簽,其重量和紐帶,然后計算每個使用它的重量及最小的標簽大小假定為標簽的大小,它也是每一個標簽從數據庫中檢索或與Google鏈接,如果沒有鏈接存在,即“不適用”的鏈接,代碼如下:
- <?php
- $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);
- ?>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <HTML>
- <HEAD>
- <TITLE> Tag Cloud Generator </TITLE>
- <META NAME="Keywords" CONTENT="tag, cloud, php, mysql">
- <META NAME="Description" CONTENT="A Tag Cloud using php and mysql">
- <LINK REL="stylesheet" HREF="style.css" TYPE="text/css">
- </HEAD>
- <BODY>
- <center><h1>Tag Cloud using php and mysql </h1><div align='center' class='tags_div'>
- <?php
- 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 "<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;
- else echo "<span style='font-size: ".$font_size."; color: #676F9D;'><a href='http://".$words_link[$tag]."/'>".$tag."</a></span>".$tag_separator;
- }
- ?>
- </div></center>
- </BODY>
- </HTML>
現在把他們所有在您的Web服務器的根目錄,并觀看結果。 每個查詢會給你新的結果,隨著時間的推移,數據庫的增長。
新聞熱點
疑難解答