跳转到主内容
websoft网络软件专家 - 深耕网络技术,打造实用软件!

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

本文章来介绍一篇关于标签云的生活方法,我们利用php和mysql做实现,有需要的同学可参考一下。

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

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

代码如下复制代码echo '';echo 'Input your text here:';echo '';echo '';?>ORsee the current tag cloud hereecho '';echo '';echo '';?>其中每个计算其频率和对将进入一个数组,输入的文本将被表征为单个词。然后将这个数组存储到一个MySQL数据库,我们可以选择保存在MySQL数据库表coloumn存储任何链接,如果这个项目未来的扩展。

立即学习“PHP免费学习笔记(深入)

”;

1) tag_id —- int,primary key,auto increament 1)tag_id - 整型,主键,自动increament 2) keyword — varchar(20),unique 2)关键字 - 数据类型为varchar(20),独特的PHP 8.5.5 PHP 8.5.5 是 PHP 8.5 分支的维护更新版本。该版本延续了“小步快跑”的迭代逻辑,通过深度错误修复、底层性能微调以及安全加固,旨在为开发者提供一个更健壮、更高效的运行环境。该版本严格遵守语义化版本规范,不包含破坏性变更。

下载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 separatorif(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 GeneratorTag Cloud using php and mysqlforeach($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.html www.bkjia.com true http://www.bkjia.com/PHPjc/631619.html TechArticle本文章来介绍一篇关于标签云的生活方法,我们利用php和mysql做实现,有需要的同学可参考一下。 用户输入文本和输入的文本在过去的一个...

相关文章