php标签云的实现代码

高洛峰
发布: 2016-12-01 14:53:58
原创
1382人浏览过

数据库中,存放文章的表中有“tag”字段,用来存放标签。标签之间用“,”分隔。比如“php,vb,随笔”。 
下面的实现代码,将标签从数据库中搜出来,并格式化处理,使其以出现的次数为依据显示出不同大小的文字连接。 
其中的细节,不做解释了! 
观念陈、方法笨、效率低的标签云的实现代码如下: 
复制代码 代码如下: 
/** 
* wniaoblog tag template showtag 

* @package wniaoblog 

* @subpackage tag 
*/ 
//connect the database 
//include('../include/config.php'); 
/** 
* counttag() - statistics labels appear the number,and the data to be stored in the two array 

* gettag() - access the tag's labels from the database 
*/ 
function counttag($string){ 
$tagstring = $string; 
//echo $tagstring." 
"; 
$tags = explode(",",$tagstring); 
$n = 1; 
$i = 0; 
$continue = true; 
//echo $tags[1]." 
"; 
//in case no-label's article 
while($tags[$n] or $tags[++$n] or $tags[++$n] ){ 
$eachtag = $tags[$n++]; 
//echo $eachtag." 
"; 
$continue = true; 
for($i=0;$continue;$i++){ 
if( $eachtagstr[$i][0] ) { 
if( $eachtagstr[$i][0] == $eachtag ){ 
$eachtagstr[$i][1]++; 
$continue = false; 

else { 
if( $eachtagstr[$i+1][0] ) $continue = true; 
else { 
$eachtagstr[$i+1][0] = $eachtag; 
$eachtagstr[$i+1][1] = 1; 
$continue = false; 


} else { //initialize the array $eachtagstr[][] 
$eachtagstr[$i][0] = $eachtag; 
$eachtagstr[$i][1] = 1; 
$continue = false; 



return $eachtagstr; 

function showtag($row,$ablink){ 
$i = 0; 
while($row[$i][0]){ 
$eachtag = $row[$i][0]; 
$eachcount = $row[$i][1]; 
$size = setsize($eachcount); 
echo " < a style='color:blue ; font-size:".$size." ' onmouseover=this.style.color='#900000' onmouseout=this.style.color='blue' href="http://www.php1.cn/">$i++; 


function gettag(){ 
$queryset = mysql_query("select * from article"); 
while($row = mysql_fetch_array($queryset)){ 
$tag = $row['tag']; 
$tagstring = $tagstring.",".$tag; 

return $tagstring; 

function setsize($size){ 
$size += 10; 
if($size > 30) 
$size = 30; 
return $size; 

//go 
echo " 
"; 
echo "标签云"; 
$string = gettag(); 
$row = counttag($string); 
showtag($row,$ablink); 
echo " 
"; 
?> 

ok,done!

腾讯云AI代码助手
腾讯云AI代码助手

基于混元代码大模型的AI辅助编码工具

腾讯云AI代码助手 98
查看详情 腾讯云AI代码助手
相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号