📜 ⬆️ ⬇️

Simple PHP complex HTML table generator

Hello. I want to share freshly written HTML table generator.

There are frequent cases of collecting various statistics and compiling it into complex tables with different groupings.

image
')
Having noticed such a tendency, I decided to automate the drawing of tables.

As a result:


image

Simplest structure


Actually the class itself is static, it has 1 public method, which takes 2 parameters and returns the HTML code of the table:

Table::html($data, $tableInfo = false); 


The $data array has the following structure:



 Array ( [0] => Array // 1  ( [0] => Cell_00 // 1  1  [1] => Cell_01 // 2  1  [2] => Cell_02 // 3  1  ) [1] => Array // 2  ( [0] => Cell_10 // 1  [1] => Cell_11 // 2  [2] => Cell_12 // 3  ) ); 



 for($i=0;$i<5;$i++) for($j=0;$j<5;$j++) $matrix[$i][$j] = $i.$j; 


Nested arrays


But these are examples of the simplest matrices. The main feature of the class is the automatic calculation of rowspan and data grouping.

If instead of a row of data (cells) we insert a similar array, then all the data of this sub-array will be placed in the row of the parent:

image
Code
 Array ( [0] => Array ( [0] => Cell_00 [1] => Cell_01 [2] => Cell_02 ) [1] => Array ( [0] => Cell_10 [1] => Cell_11 [2] => Array ( [0] => Array ( [0] => Cell_120 ) [1] => Array ( [0] => Cell_121 ) ) ) ) 

Slightly deeper:
image
Code
 Array ( [0] => Array ( [0] => Cell_00 [1] => Cell_01 [2] => Cell_02 ) [1] => Array ( [0] => Cell_10 [1] => Array ( [0] => Array ( [0] => Cell_110 [1] => Array ( [0] => Array ( [0] => Cell_1100 ) [1] => Array ( [0] => Cell_1101 ) ) ) [1] => Array ( [0] => Cell_111 [1] => Cell_112 ) ) ) ) 


Customization


So, the class can automatically draw complex tables and count rowspan' . We now turn to manual table setup.
The $data array may contain a subarray ['tableInfo'] - in it you can specify various settings, depending on its ( 'tableInfo' ) location.
It may be:
  1. At the root of the array, when listing lines

    Example
     Array ( [0] => Array(...) [1] => Array(...) ... [n] => Array(...) [tableInfo] => Array (...) ) 

    'tableInfo' located in the root of the array allows you to set general settings and parameters for the entire table:

     Array ( [rowspan] => false //  .     . [cols] => Array //  ( [0] => Array ( [title] =>  //title   (  th). [key] => name //key        . ) [1] => Array //title  key     ( //  cols    title,   key [title] =>  //     ,       , [key] => tel //    ) ) // HTML   : [id] => tableId [class] => tableClass [style] => color:#000; [args] => align=left width=80% // args    ,    . ) 

    The same array can be passed to the second parameter of the method:

     Table::html($data, $tableInfo = false); 

    If both methods are used simultaneously ( isset($data['tableInfo'] && isset($tableInfo) ), then in controversial cases, priority is given to the rules from the second $tableInfo .

     array_replace_recursive($data['tableInfo'], $tableInfo); 

    Example of using title
    image
    Code
     Array ( [0] => Array ( [0] => Cell_00 [1] => Cell_01 [2] => Cell_02 ) [1] => Array ( [0] => Cell_10 [1] => Cell_11 [2] => Cell_12 ) [tableInfo] => Array ( [cols] => Array ( [0] => Array ( [title] => Title 1 ) [1] => Array ( [title] => Title 2 ) [2] => Array ( [title] => Title 3 ) ) ) ) 

    key usage example
    image
    Code
     Array ( [0] => Array ( [0] => Cell_00 [1] => Cell_01 [2] => Cell_02 ) [1] => Array ( [0] => Cell_10 [1] => Cell_11 [2] => Cell_12 [3] => Cell_13 // , ..    key ) [tableInfo] => Array ( [cols] => Array ( [0] => Array ( [key] => 2 ) [1] => Array ( [key] => 0 ) [2] => Array ( [key] => 1 ) ) ) ) 

    image
    Code
     $exmpl3 = array( 'tableInfo' => array( 'cols' => array( 0 => array('key' => 'cell0'), 1 => array('key' => 'cell1'), 2 => array('key' => 'cell2'), ) ), 0 => array( 'cell0' => 'Cell_00', 'cell2' => 'Cell_02', 'cell1' => 'Cell_01', ), 1 => array( 'cell0' => 'Cell_10', 'sub' => array( 0 => array( 'cell1' => 'Cell_110', 'sub' => array( 0 => array( 'cell2' => 'Cell_1100', ), 1 => array( 'cell0' => 'asdasd_10', //  , ..    'cell1' => 'asdasd_110',//    'cell2' => 'Cell_1101' ) ) ), 'sub' => array('cell1' => 'Cell_111', 'cell2' => 'Cell_112'), ) ) ); 

    It is worth mentioning here that if key is not used at all, then the array of output keys is populated with all keys of cells (not subarrays) that are in this line. For example in this case:

    Code
     $exmpl = array( 0 => array( 0 => 'Cell_00', 1 => 'Cell_01', 2 => 'Cell_02', ), 1 => array( 0 => 'Cell_10', 1 => 'Cell_11', 2 => 'Cell_12', ), ); 

    For both lines, the $colKeys ( ) will look like this:

     $colKeys = array(0, 1, 2); 

  2. In any array of cells (at any level)

    Example
     Array ( [0] => Array( [qwe] =>  [asd] =>  [tableInfo] => Array (...) ) [1] => Array(...) ... ) 

     Array ( [0] => Array( [qwe] =>  [asd] =>  [zxc] => Array( [0] => Array( [jkl] =>  [tableInfo] => Array (...) ) [1] => Array() ) ) [1] => Array(...) ... ) 

    'tableInfo' located in the array of cells may contain settings for the row in which it is located and / or settings for each individual cell:

     'tableInfo' => array( 'rowspan' => false, //  rowspan    'rowspan' => true, //  rowspan    'rowspan' => 5, //   rowspan    'keys' => array( //      'delete' => 'all', //     'delete' => array('cell2'), //  ( 'cell2') 'add' => array('cell3'), //    ( 'cell3') 'forwarding' => array( //    .     0 => array( 'src' => 'cell1', //     cell1 'dst' => 'cell2' //   cell2 ), ), //  ,           ), //..            'cells' => array( //   'cell2' => array( //    'cell2' 'rowspan' => false, // 'rowspan' => true, // 'rowspan' => 5, //   'colspan' => 6, //    // HTML   : 'id' => 'cellId', 'class' => 'cellClass', 'style' => 'color:#000;', 'args' => 'align=left width=80%' ) ), // HTML   : 'id' => 'rowId', 'class' => 'rowClass', 'style' => 'color:#000;', 'args' => 'align=left width=80%' ); 


Rowspan priorities

  1. The rule for the cell (the most important rule);
  2. Rule for string;
  3. Rule for table;
  4. By default, rowspan is always considered if a subarray is present.


Well that's all. I decided not to consider some moments in detail, if I have questions, I'll sign for it.
Download class Table

Source: https://habr.com/ru/post/253207/


All Articles