Skip to content

Instantly share code, notes, and snippets.

@gaffling
Last active December 1, 2020 16:38
Show Gist options
  • Save gaffling/8a5fca36a1c493ed555a89d0577549a4 to your computer and use it in GitHub Desktop.
Save gaffling/8a5fca36a1c493ed555a89d0577549a4 to your computer and use it in GitHub Desktop.
[Array 2 Table] HTML Generator #php #class #table
<?php
/* ---------------------------------------------------- */
/* [Array 2 Table] HTML Generator #php #function #table */
/* ---------------------------------------------------- */
/**
* Generate a table from an array
* @see https://stackoverflow.com/questions/4746079/how-to-create-a-html-table-from-a-php-array
*
* @param array | null $array input array
* @param string | bool $css if true then it uses the build in style
* if false then it doesn't use the build in style
* if string then it is uses the string as a class name
* @return string
*/
$multi['PHP'] = array(
"First Release" => "1995",
"Latest Release" => "7.3.11",
"Designed by" => "Rasmus Lerdorf",
"Description" => array(
"Extension" => ".php",
"Typing Discipline" => "Dynamic, weak",
"License" => "PHP License (most of Zend engine under Zend Engine License)"
)
);
$multi['Python'] = array(
"First Release" => "1991",
"Latest Release" => "3.8.0",
"Designed by" => "Guido van Rossum",
"Description" => array(
"Extension" => ".py",
"Typing Discipline" => "Duck, dynamic, gradual",
"License" => "Python Software Foundation License"
)
);
$array = array(
array('first'=>'tom', 'last'=>'smith', 'email'=>'tom@example.org', 'company'=>'example ltd'),
array('first'=>'hugh', 'last'=>'blogs', 'email'=>'hugh@example.org', 'company'=>'example ltd'),
array('first'=>'steph', 'last'=>'brown', 'email'=>'steph@example.org', 'company'=>'example ltd')
);
echo generateTable($multi);
echo generateTable($array);
function generateTable($array, $css=true, $tab="\t", $nl=PHP_EOL) {
// INI
if (!isset($array[0])) {
$tmp = $array;
$array = array();
$array[0] = $tmp;
}
// CREATE AN ARRAY WITH A SINGLE ELEMENT
if ($array[0] === null) {
return 'NULL<br>';
}
if ($css === true) {
$html = '
<style>
.generateTable {
border-collapse: collapse;
width: 100%;
}
.generateTable td, .generateTable th {
font-family: sans-serif;
border: 1px solid #ddd;
padding: 8px;
}
.generateTable tr:nth-child(even){
background-color: #f2f2f2;
}
.generateTable tr:hover {
background-color: rgba(9,30,66,.08);
}
.generateTable th {
font-family: monospace;
padding-top: 12px;
padding-bottom: 12px;
text-align: left;
background-color: #f8f8f8;
color: #444;
}
</style>
';
} else {
$html = '';
}
$html .= '<table class="' . (is_string($css) ? $css : 'generateTable') . '">'.$nl;
// HEADER ROW
$html .= $tab.'<thead>'.$nl.$tab.$tab.'<tr>'.$nl;
foreach ($array[0] as $key => $value) {
$html .= $tab.$tab.$tab.'<th>' . htmlspecialchars($key) . '</th>'.$nl;
}
$html .= $tab.$tab.'</tr>'.$nl.$tab.'</thead>'.$nl;
// DATA ROWS
$html .= $tab.'<tbody>'.$nl;
foreach ($array as $key => $value) {
$html .= $tab.$tab.'<tr>'.$nl;
foreach ($value as $key2 => $value2) {
if (is_array($value2)) {
$html .= $tab.$tab.$tab.'<td>' . generateTable($value2, false, '', '') . '</td>'.$nl;
} else {
$html .= $tab.$tab.$tab.'<td>' . htmlspecialchars($value2) . '</td>'.$nl;
}
}
$html .= $tab.$tab.'</tr>'.$nl;
}
$html .= $tab.'</tbody>'.$nl;
// FINISH TABLE AND RETURN IT
$html .= '</table>'.$nl;
return $html;
}
<?php
/* ------------------------------------------------- */
/* [Array 2 Table] HTML Generator #php #class #table */
/* ------------------------------------------------- */
class table {
protected function attr( array $value ) {
$vals = '';
foreach ($value as $key => $val)
$vals .= $key . '="' . $val . '" ';
return rtrim($vals, ' ');
}
public function make( array $data, $config = array('border' => 1) ) {
$attribute =! is_null($config) ? ' '.$this->attr($config) : '';
$html = '';
if ( isset($config['class']) and $config['class']=='table' )
$html .= '<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">';
$html .= '<table' . $attribute . '>' . PHP_EOL . '<tr>';
if ( isset($config['class']) and $config['class']=='table' )
$html .= '<thead class="thead-dark">';
foreach ($data['header'] as $header)
$html .= '<th scope="col">' . $header . '</th>';
$html .= '</tr>' . PHP_EOL;
if ( isset($config['class']) and $config['class']=='table' )
$html .= '</thead><tbody>';
foreach ($data['data'] as $row) {
if (is_array($row)) {
$html .= '<tr>';
foreach ($row as $data)
$html .= '<td>' . $data . '</td>';
$html .= '</tr>' . PHP_EOL;
}
}
if ( isset($config['class']) and $config['class']=='table' )
$html .= '</tbody>';
$html .= '</table>';
return $html;
}
}
$table = new table;
$data = array(
'header' => array('ID', 'Name', 'Department'),
'data' => array(
array(12, 'John', '555'),
array(16, 'Marc', '777'),
array(28, 'Tom', '222'),
array(45, 'Jill', '000'),
)
);
// IF 'class' => 'table' IS SET WE USE BOOTSTRAP
$config = array('border' => 1, 'width' => 200, 'class' => 'table');
echo '<body style="margin: 7rem auto;width:50%">';
echo $table->make($data, $config);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment