Function: table()

function table(array &$array, array $parameters = [], bool $echo = true): string|null;

Function for rendering tables.

Details

This function renders a tables based on the values of an array.

Parameters

Name Pass type Value type Default value Description
$array by reference array

The array of the rows.

$parameters by value array []

The array of parameters as an associative array in the form key => value:

  • $parameters["captions"] - the associative array of column captions. If it is not specified, the table is rendered without captions.

  • $parameters["col_class_from_keys"] - if it is specified and set to true, the keys of the captions are used as the class names.

  • $parameters["no_escape_html"] - if it is specified and set to true, the values of the data array are not escaped and passed as is. It is useful when html codes of other elements are passed. Per default, the table data is escaped for html.

  • $parameters["formatter"] - this function is used for the value formatting if specified. It is called for evelry cell of the table. The signature of this function is:

function ($rownum, $colnum, $colname, $val) : string;
  • $rownum - the row number.

  • $colnum - the colnum number.

  • $colname - the column name if the $parameters["captions"] are provided. The keys of the cpation array are used. If the $parameters["captions"] are not provided, $colname is equal to $colnum.

  • $val - original value of the table cell.

If necessary, format the original value and return it. Otherwise, return the original value without changes.

Any other attributes may be specified.

Example:

$rows = [
   ["name" => "DB design", "employee" => "Alex", ... ],
   ["name" => "Mask implementation", "employee" => "Boris", ... ],
   ["name" => "Validation", "employee" => "Alex", ... ],
   ["name" => "Settings", "employee" => "Boris", ... ],
   ["name" => "About", "employee" => "Robert", ... ],
   ["name" => "Initialization", "employee" => "Alon", ... ]
];

$captions = [
  "name" => "Task name",
  "employee" => "Employee",
  "time_estimation" => "Time estimation",
  "deadline" => "Deadline",
  "comments" => "Conmments"
];

$formatter = function ($rownum, $colnum, $colname, $val) {
  if($colname == "time_estimation") return format_number($val, 2);
  if($colname == "deadline") return date("Y-m-d H:i", $val);
  return $val;
};

table($rows,
      ["captions" => $captions,
       "class" => "my_table",
       "style" => "background-color: #dddddd",
       "col_class_from_keys" => true,
       "no_escape_html" => false,
       "formatter" => $formatter
      ]);
$echo by value bool true

If the value of $echo is true, the html code of the table is directly echoed. Otherwise it is returned as string. It might be useful to pass this code to other rendering functions instead of echoing.

Returns

string|null

If the paramter $echo is true, the string of the table html code is returned, otherwise null.

Authors

Oleg Schildt

Package

Source code