萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

PHP 陣列轉 HTML 表格類別:支援自定義樣式、隔列換色、多種輸出模式

xtable 類別完整實作

<?php
class XTable {
    private $titles = [];
    private $rows = [];
    private $colors = ['#EEEEEE', '#CCEEEE']; // 奇/偶列背景色
    private $tableExtra = ''; // table 標籤額外屬性
    private $titleStyle = '';
    
    public function __construct() {}
    
    /** 設定表格額外屬性(如 style、class、id) */
    public function extra($attr) { $this->tableExtra = $attr; }
    
    /** 設定隔列換色 */
    public function background($colors) {
        $this->colors = is_array($colors) ? $colors : [$colors, $colors];
    }
    
    /** 設定表頭 */
    public function titles(array $titles, $style = '') {
        $this->titles = $titles;
        $this->titleStyle = $style;
    }
    
    /** 新增單列 */
    public function addRow(array $row) { $this->rows[] = $row; }
    
    /** 批次新增多列 */
    public function addRows(array $rows) { 
        foreach ($rows as $row) $this->addRow($row); 
    }
    
    /** 生成 HTML */
    public function html() {
        // 表頭
        $thead = '<tr' . ($this->titleStyle ? " style='{$this->titleStyle}'" : '') . '>';
        foreach ($this->titles as $title) {
            $thead .= "<th>$title</th>";
        }
        $thead .= '</tr>';
        
        // 資料列
        $tbody = '';
        foreach ($this->rows as $i => $row) {
            $bg = $this->colors[$i % 2];
            $tbody .= "<tr style='background-color:$bg'>";
            foreach ($row as $cell) {
                $tbody .= "<td>$cell</td>";
            }
            $tbody .= '</tr>';
        }
        
        return sprintf(
            "<table cellpadding='0' cellspacing='0' border='1' %s>%s%s</table>",
            $this->tableExtra, $thead, $tbody
        );
    }
    
    /** 示範用法 */
    public static function demo() {
        // 範例 1:基本用法
        $t1 = new XTable();
        $t1->titles(['姓氏', '名字', '電話']);
        $t1->addRows([
            ['Garcia', 'Ivan', '888'],
            ['Marco', 'Alfonso', '555'],
        ]);
        echo $t1->html() . '<hr>';
        
        // 範例 2:自定義顏色與表格樣式
        $t2 = new XTable();
        for ($i = 1; $i <= 10; $i += 2) {
            $t2->addRow(['ODD', $i]);
            $t2->addRow(['EVEN', $i + 1]);
        }
        $t2->background(['pink', 'gold']);
        $t2->titles(['TYPE', '#']);
        $t2->extra("style='width:500px; background-color:cyan; color:navy;'");
        echo $t2->html() . '<hr>';
        
        // 範例 3:乘法表
        $t3 = new XTable();
        for ($i = 1; $i <= 6; $i++) {
            $t3->addRow(['5x' . $i, 5 * $i]);
        }
        $t3->background(['olive', 'maroon']);
        $t3->titles(['乘法表', '5']);
        $t3->extra("style='border:dotted red 10px; text-align:right; width:500px; background-color:black; color:white;'");
        echo $t3->html() . '<hr>';
        
        // 範例 4:單列多欄(橫向表格)
        $t4 = new XTable();
        $header = ['#'];
        $data = ['#'];
        for ($i = 1; $i <= 10; $i++) {
            $header[] = $i;
            $data[] = $i;
        }
        $t4->addRow($data);
        $t4->background(['pink', 'gold']);
        $t4->titles($header);
        $t4->extra("style='text-align:center; width:500px; background-color:cyan; color:navy;'");
        echo $t4->html();
    }
}

// 執行示範
XTable::demo();
?>

輸出效果預覽

  • 基本表格:標準邊框、隔列換色、表頭粗體。
  • 自定義樣式:可透過 extra() 注入任意 CSS,如寬度、邊框、背景色、文字色。
  • 靈活資料來源:支援逐列 addRow() 或批次 addRows(),資料來源可為資料庫查詢結果、CSV 解析陣列等。

擴充建議

  • 加入 setCellAttributes() 支援單元格層級 style/class。
  • 支援 thead/tbody/tfoot 語義化標籤。
  • 整合資料排序、分頁、搜尋功能(配合 DataTables.js)。
  • 支援匯出 CSV/Excel 功能。
copyright © 萬盛學電腦網 all rights reserved