php数据转换为html table或者csv文件

php中文网
发布: 2016-06-23 13:44:59
原创
961人浏览过

应用场景:游戏中的很多业务已经迁移到了redis上,但是redis中的数据在运维层面很难展现,通过php-redis将redis中的set或者hash转换为php中的array,然后映射为html中的table


具体展现:


主要设计点:table的基本单位是单元格row_field_node,多个row_field_node组成row_data, tile + 多个row_data = table。因为每个单元格都可能有对应的超链接响应,

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

所以row_field_node数据结构是这样的:

千面视频动捕
千面视频动捕

千面视频动捕是一个AI视频动捕解决方案,专注于将视频中的人体关节二维信息转化为三维模型动作。

千面视频动捕 173
查看详情 千面视频动捕
class row_field_node{        public $data;           //显示的数据        public $action;         //对应的超链接        public $param;          //超链接需要传入的参数        public $target;         //超链接要展现在那个frame中        public function __construct($data, $action, $param, $target)        {                $this->data     = $data;                $this->action   = $action;                $this->param    = $param;                $this->target   = $target;        }}
登录后复制

array中的数组与hash可以输出为html table或者是csv文件,具体实现见代码:

<?phpheader("Content-type: text/html; charset=utf-8");$domain_name = "192.168.1.1";class row_field_node{	public $data;	public $action;	public $param;	public $target;	public function __construct($data, $action, $param, $target)	{		$this->data 	= $data;                      		$this->action 	= $action;                   		$this->param 	= $param;     		$this->target 	= $target;                      	}}class xtable{	private $title_list,$row_list,$fonts,$table_tag;	public function __construct()	{		$this->title_list=array();                          // strings with titles for first row 		$this->row_list=array();                          // data to show on cells		$this->fonts=array("#EEEEEE","#CCEEEE");      // background colors for odd and even rows		$this->table_tag="";                            // extra html code for table tag	}	public function set_table_tag($table_tag)                       // add some html code for the tag table	{		$this->table_tag=$table_tag;	}	public function background($fonts) 	{		if (is_array($fonts)) 		{			$this->fonts=$fonts; 		}		else		{ 			$this->fonts=array($fonts,$fonts);		}	}	public function addrow($row_data) 	{		$this->row_list[]=$row_data;	}	public function hash_output($data)	{		$this->title_list=array("field", "value");		foreach($data as $key=> $value)		{			$row_data = array();			$field_data = new row_field_node($key, "", "", "");			array_push($row_data, $field_data);			$field_data = new row_field_node($value, "", "", "");			array_push($row_data, $field_data);			$this->addrow($row_data);		}		return $this->html();	}	public function set_output($title, $data, $desc = '')	{		$this->title_list = $title;		$this->row_list = $data;		echo "total:".count($data).' '.$desc . "<br>";		return $this->html();	}	public function html()	{		$cfondos=$this->fonts;		//output title		$output_content="<tr>";		$t_count=count($this->title_list);		for($t_idx = 0; $t_idx < $t_count; $t_idx++)		{			$output_content.=sprintf("<th>%s</th>",$this->title_list[$t_idx]);		}		$output_content.="</tr>";		//start outputing data rows		$table_row_content="";		$row_count=count($this->row_list);		for($row_idx = 0; $row_idx < $row_count; $row_idx++)		{			$table_row_content .= sprintf("<tr style='background-color:%s'>", $this->fonts[$row_idx % 2]);						$line_data_list = $this->row_list[$row_idx];						$col_count = count($line_data_list);			if($col_count != $t_count)			{				echo "row field count not match title count|col:".$col_count."|title:".$t_count;				exit;			}			for($col_idx = 0; $col_idx < $col_count; $col_idx++)			{				Global $domain_name;				if($line_data_list[$col_idx]->action != "")				{					//echo $line_data_list[$col_idx]->action."----".$line_data_list[$col_idx]->param."----".$line_data_list[$col_idx]->target."----".$line_data_list[$col_idx]->data."===============";					$table_row_content.=sprintf("<td align=\"center\"><a href=\"http://$domain_name/%s?%s\" target='%s'>  %s  </a></td>",						$line_data_list[$col_idx]->action, $line_data_list[$col_idx]->param, $line_data_list[$col_idx]->target,						$line_data_list[$col_idx]->data);									}				else				{					$table_row_content.=sprintf("<td align=\"center\">   %s  </a>        </td>", $line_data_list[$col_idx]->data);				}			}			$table_row_content.="</tr>";		}		return sprintf("<table cellpadding='0' cellspacing='0' border='1' %s>%s%s</table>",$this->table_tag,$output_content,$table_row_content);	}	public function csv_output($title, $data)	{		$this->title_list = $title;		$this->row_list = $data;		echo "total:".count($data)."<br>";		return $this->csv();	}	public function csv()	{		$file_name = time(0).".rar";		$fp = fopen($file_name, 'w');		$t_count=count($this->title_list);		//start outputing data rows		$row_count=count($this->row_list);		$file_data = "";		$csv_row_data = "";    for($t_idx = 0; $t_idx < $t_count; $t_idx++)		{		  $csv_row_data = $csv_row_data." ".$this->title_list[$t_idx];		}				$file_data = $file_data.$csv_row_data."\n";				for($row_idx = 0; $row_idx < $row_count; $row_idx++)		{						$line_data_list = $this->row_list[$row_idx];						$col_count = count($line_data_list);			if($col_count != $t_count)			{				echo "row field count not match title count|col:".$col_count."|title:".$t_count;				exit;			}			$csv_row_data = "";			for($col_idx = 0; $col_idx < $col_count; $col_idx++)			{				Global $domain_name;				$csv_row_data = $csv_row_data." ".$line_data_list[$col_idx]->data;			}			$file_data = $file_data.$csv_row_data."\n";		}		fwrite($fp, $file_data);		Global $domain_name;		echo "<br><a href='http://$domain_name/cupteam_info/$file_name' target='detail'>csvfile:$file_name</a>";	}}?>
登录后复制




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号