php資料轉換為html table或者csv檔案,

來源:互聯網
上載者:User

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 < $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>";}}?>


樣本:

    $real_server_list = array();    foreach($server_list as $server_key)    {        $temp_array = explode(":", $server_key);        $filed_node = new row_field_node();        $filed_node->data = $temp_array[1];        $filed_node->action = "cupteam/get_guild_list_output.php";        $filed_node->param = "input_id=".$filed_node->data;        $filed_node->target = "detail";        $row_data = array($filed_node);        array_push($real_server_list, $row_data);    }    $t1=new xtable();    echo $t1->set_output(array("server_id"), $real_server_list);



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.