php数据转换为html table或者csv文件
应用场景:游戏中的很多业务已经迁移到了redis上,但是redis中的数据在运维层面很难展现,通过php-redis将redis中的set或者hash转换为php中的array,然后映射为html中的table
具体展现:
主要设计点:table的基本单位是单元格row_field_node,多个row_field_node组成row_data, tile + 多个row_data = table。因为每个单元格都可能有对应的超链接响应,
所以row_field_node数据结构是这样的:
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 %s",$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 ", $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 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="%5C%22http://%24domain_name/%s?%s%5C%22" 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 </td>", $line_data_list[$col_idx]->data); } } $table_row_content.=""; } return sprintf("
"; 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 title_list[$t_idx]; } $file_data = $file_data.$csv_row_data."\n"; for($row_idx = 0; $row_idx 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 data; } $file_data = $file_data.$csv_row_data."\n"; } fwrite($fp, $file_data); Global $domain_name; echo "
csvfile:$file_name"; }}?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.
