php标签云的实现代码,将php数组输出html表格的方

2019-07-25 作者:yzc216.com官网   |   浏览(151)

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

复制代码 代码如下:

复制代码 代码如下:

<?php
class xtable
{
 private $tit,$arr,$fons,$sextra;
 public function __construct()
 {
  $this->tit=array();       // strings with titles for first row
  $this->arr=array();       // data to show on cells
  $this->fons=array("#EEEEEE","#CCEEEE");  // background colors for odd and even rows
  $this->sextra="";       // extra html code for table tag
 }

/**
* 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='".$ablink."tag?tag=".$EachTag."' target='_self' > ".$EachTag."(".$EachCount.")"." ";
$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 "
";
?>

 public function extra($s)      // add some html code for the tag table
 {
  $this->sextra=$s;
 }
 public function background($arr) {if (is_array($arr)) $this->fons=$arr; else $this->fons=array($arr,$arr);}
 public function titles($text,$style="") {$this->tit=$text; $this->sesttit=$style;}
 public function addrow($a) {$this->arr[]=$a;}
 public function addrows($arr) {$n=count($arr); for($i=0;$i<$n;$i ) $this->addrow($arr[$i]);}
 public function html()
 {
  $cfondos=$this->fons;
  $titulos="<tr>";
  $t=count($this->tit);
  for($k=0;$k<$t;$k )
  {
   $titulos.=sprintf("<th>%s</th>",$this->tit[$k]);
  }
  $titulos.="</tr>";

OK,DONE!

  $celdas="";
  $n=count($this->arr);
  for($i=0;$i<$n;$i )
  {
   $celdas.=sprintf("<tr style='background-color:%s'>",$this->fons[$i%2]);
   $linea=$this->arr[$i];
   $m=count($linea);
   for($j=0;$j<$m;$j )
    $celdas.=sprintf("<td  %s>%s</td>","",$linea[$j]);
   $celdas.="</tr>";
  }
  return sprintf("<table cellpadding='0' cellspacing='0' border='1' %s>%s%s</table>",$this->sextra,$titulos,$celdas);
 }
 public function example()
 {
  $tit=array("Apellidos","Nombre","Telefono");
  $r1=array("Garcia","Ivan","888");
  $r2=array("Marco","Alfonso","555");
  $x=new xtable();
  $x->titles($tit);      //take titles array
  $x->addrows(array($r1,$r2));   // take all rows at same time
  return $x->html();     //return html code to get/show/save it
 }
}

您可能感兴趣的文章:

  • 用JS实现3D球状标签云示例代码
  • PHP 创建标签云函数代码
  • jquery 3D 标签云示例代码
  • Android自定义ViewGroup打造各种风格的SlidingMenu
  • Android编程重写ViewGroup实现卡片布局的方法
  • 从源码解析Android中View的容器ViewGroup
  • Android应用开发中自定义ViewGroup视图容器的教程
  • Android应用开发中自定义ViewGroup的究极攻略
  • Android自定义ViewGroup实现标签浮动效果
  • Android自定义控件ViewGroup实现标签云(四)

// Example
$t1=new xtable();
echo $t1->example()."<hr />";

$t2=new xtable();
for($i=1;$i<=10;$i =2)
 {
  $t2->addrow(array("ODD",$i));
  $t2->addrow(array("EVEN",$i 1));
 }
$t2->background(array("pink","gold"));
$t2->titles(array("TYPE","#"));
$t2->extra(" style='width:500px; background-color:cyan; color:navy;'");
echo $t2->html()."<hr />";

$t3=new xtable();
for($i=1;$i<=6;$i )
 {
  $t3->addrow(array("5x".$i,5*$i));

 }
$t3->background(array("olive","maroon"));
$t3->titles(array("Multiplication table","5"));
$t3->extra("style='border:dotted red 10px; padding-left:4px;padding-right:4px; text-align:right;width:500px; background-color:black; color:white;'");
echo $t3->html()."<hr />";

$t4=new xtable();
$a=array("#");
for($i=1;$i<=10;$i )
 {
  $a[]=$i;
 }
$t4->addrow($a);
$t4->background(array("pink","gold"));
$tit=array(); $tit[]="Numbers";
for($i=1;$i<=10;$i ) $tit[]="#";
$t4->titles($tit);
$t4->extra("style='border:solid 1px silver; padding-left:4px;padding-right:4px; text-align:center;width:500px; background-color:cyan; color:navy;'");
echo $t4->html()."<hr />";
?>

您可能感兴趣的文章:

  • php输出表格的实现代码(修正版)
  • php将HTML表格每行每列转为数组实现采集表格数据的方法
  • php动态实现表格跨行跨列实现代码
  • PHP处理excel cvs表格的方法实例介绍
  • PHP处理CSV表格文件的常用操作方法总结
  • PHP 简易输出CSV表格文件的方法详解
  • php 分页原理详解
  • php实现的漂亮分页方法
  • PHP分页函数代码(简单实用型)
  • PHP通用分页类page.php[仿google分页]
  • php实现的后台表格分页功能示例

本文由yzc216亚洲城发布于yzc216.com官网,转载请注明出处:php标签云的实现代码,将php数组输出html表格的方

关键词: yzc216亚洲城 yzc216.com官网