PHP和MySQL生成的标签云实现代码
用户输入文本和输入的文本在过去的一个标签云,标签云是一个用户生成的标签的可视化描述,或只是一个网站的文字内容,通常用来描述网站的内容。
为此,我们将创建一个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 id="OR">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; 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 id="Tag-nbsp-Cloud-nbsp-using-nbsp-php-nbsp-and-nbsp-mysql-nbsp">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服务器的根目录,并观看结果。 每个查询会给你新的结果,随着时间的推移,数据库的增长。
教程链接:
随意转载~但请保留教程地址★

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

MySQL 數據庫中,用戶和數據庫的關係通過權限和表定義。用戶擁有用戶名和密碼,用於訪問數據庫。權限通過 GRANT 命令授予,而表由 CREATE TABLE 命令創建。要建立用戶和數據庫之間的關係,需創建數據庫、創建用戶,然後授予權限。

MySQL適合初學者使用,因為它安裝簡單、功能強大且易於管理數據。 1.安裝和配置簡單,適用於多種操作系統。 2.支持基本操作如創建數據庫和表、插入、查詢、更新和刪除數據。 3.提供高級功能如JOIN操作和子查詢。 4.可以通過索引、查詢優化和分錶分區來提升性能。 5.支持備份、恢復和安全措施,確保數據的安全和一致性。

要填寫 MySQL 用戶名和密碼,請:1. 確定用戶名和密碼;2. 連接到數據庫;3. 使用用戶名和密碼執行查詢和命令。

1.使用正確的索引索引通過減少掃描的數據量來加速數據檢索select*fromemployeeswherelast_name='smith';如果多次查詢表的某一列,則為該列創建索引如果您或您的應用根據條件需要來自多個列的數據,則創建複合索引2.避免選擇*僅選擇那些需要的列,如果您選擇所有不需要的列,這只會消耗更多的服務器內存並導致服務器在高負載或頻率時間下變慢例如,您的表包含諸如created_at和updated_at以及時間戳之類的列,然後避免選擇*,因為它們在正常情況下不需要低效查詢se

Navicat本身不存儲數據庫密碼,只能找回加密後的密碼。解決辦法:1. 檢查密碼管理器;2. 檢查Navicat的“記住密碼”功能;3. 重置數據庫密碼;4. 聯繫數據庫管理員。

通過以下命令查看 MySQL 數據庫:連接到服務器:mysql -u 用戶名 -p 密碼運行 SHOW DATABASES; 命令獲取所有現有數據庫選擇數據庫:USE 數據庫名;查看表:SHOW TABLES;查看表結構:DESCRIBE 表名;查看數據:SELECT * FROM 表名;

使用 Navicat Premium 創建數據庫:連接到數據庫服務器並輸入連接參數。右鍵單擊服務器並選擇“創建數據庫”。輸入新數據庫的名稱和指定字符集和排序規則。連接到新數據庫並在“對象瀏覽器”中創建表。右鍵單擊表並選擇“插入數據”來插入數據。

在 MySQL 中復製表需要創建新表、插入數據、設置外鍵、複製索引、觸發器、存儲過程和函數。具體步驟包括:創建具有相同結構的新表。將數據從原始表插入新表。設置相同的外鍵約束(如果原始表有)。創建相同索引。創建相同觸發器(如果原始表有)。創建相同存儲過程或函數(如果原始表使用了)。
