Convert php Data to HTML table or csv files

Source: Internet
Author: User
Php Data conversion to htmltable or csv file application scenarios: Many businesses in the game have been migrated to redis, but redis data is hard to be displayed at the O & M level, use php-redis to convert the set or hash in redis to the array in php, and then map it to the table in html.


Details:


Main design points: the basic unit of the table is the cell row_field_node. multiple row_field_nodes form row_data, tile + multiple row_data = table. Because each cell can have a corresponding hyperlink response,

Therefore, the row_field_node data structure is as follows:

Class row_field_node {public $ data; // The displayed data public $ action; // The corresponding hyperlink public $ param; // The public $ target parameter to be input for the hyperlink; // The public function _ construct ($ data, $ action, $ param, $ target) {$ this-> data = $ data; $ this-> action = $ action; $ this-> param = $ param; $ this-> target = $ target ;}}

Arrays and hash in array can be output as html tables or csv files. for details, see the code:

 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 . "
";return $this->html();}public function html(){$cfondos=$this->fonts;//output title$output_content="";$t_count=count($this->title_list);for($t_idx = 0; $t_idx < $t_count; $t_idx++){$output_content.=sprintf("%s",$this->title_list[$t_idx]);}$output_content.="";//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("", $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(" %s ",$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(" %s ", $line_data_list[$col_idx]->data);}}$table_row_content.="";}return sprintf(" %s%s
",$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)."
";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 "
csvfile:$file_name";}}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.