PHP data converted to HTML table or CSV file

Source: Internet
Author: User
Scenario: Many of the services in the game have been migrated to Redis, but data in Redis is difficult to present in the operations dimension, by Php-redis the set or hash in Redis to an array in PHP, and then mapping to table in HTML


Specific presentation:


Main design point: The basic unit of table is cell Row_field_node, multiple row_field_node compose row_data, tile + multiple row_data = table. Because each cell may have a corresponding hyperlink response,

So the ROW_FIELD_NODE data structure is this:

Class row_field_node{public        $data;           The data displayed is public        $action;         The corresponding hyperlink public        $param;          Hyperlinks need to pass in the parameters public        $target;         Hyperlinks to show in that frame public        function __construct ($data, $action, $param, $target)        {                $this->data     = $data;                $this->action   = $action;                $this->param    = $param;                $this->target   = $target;        }}

Array and hash can be exported as HTML table or CSV file, the implementation of the code is as follows:

 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 the 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" Tota L: ". 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.