PHP和MySQL產生的標籤雲實現代碼_PHP教程

來源:互聯網
上載者:User
本文章來介紹一篇關於標籤雲的生活方法,我們利用php和mysql做實現,有需要的同學可參考一下。

使用者輸入文本和輸入的文本在過去的一個標籤雲 。標籤雲是一個使用者產生的標籤的可視化描述,或只是一個網站的文字內容,通常用來描述網站的內容。

為此,我們將建立一個HTML表格,將接受使用者文本,也讓使用者可以看到從 MySQL資料庫,其中包含在過去輸入的文本產生的標籤雲。

代碼如下 複製代碼
echo '';
?>


OR




see the current tag cloud here


echo '';
?>

其中每個計算其頻率和對將進入一個數組,輸入的文本將被表徵為單個詞。然後將這個數組儲存到一個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)。

代碼如下 複製代碼

///////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* 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.把下面的代碼。

代碼如下 複製代碼

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連結,如果沒有連結存在,即“不適用”的連結

代碼如下 複製代碼

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


現在把他們所有在您的Web伺服器的根目錄,並觀看結果。 每個查詢會給你新的結果,隨著時間的推移,資料庫的增長。

http://www.bkjia.com/PHPjc/631619.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631619.htmlTechArticle本文章來介紹一篇關於標籤雲的生活方法,我們利用php和mysql做實現,有需要的同學可參考一下。 使用者輸入文本和輸入的文本在過去的一個...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.