Created
September 24, 2020 03:41
-
-
Save cp6/8841314b4eda99c9c561162b880632f6 to your computer and use it in GitHub Desktop.
PHP function for making HTML tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function outputString(string $string) | |
{ | |
echo $string; | |
} | |
function tableBuilder(array $theads, array $data, string $table_class = 'table', string $thead_class = '') | |
{ | |
(empty($table_class)) ? $tbl = "" : $tbl = " class='$table_class'"; | |
(empty($thead_class)) ? $th = "" : $th = " class='$thead_class'"; | |
outputString("<table$tbl>"); | |
outputString("<thead$th><tr>"); | |
foreach ($theads as $column) { | |
outputString("<th>$column</th>"); | |
} | |
outputString("</tr></thead><tbody>"); | |
$columns = count($theads); | |
$col_count = 0; | |
foreach ($data as $content) { | |
if (($col_count % $columns) === 0) { | |
outputString("<tr>"); | |
} | |
$col_count++; | |
outputString("<td>$content</td>"); | |
if (($col_count % $columns) === 0) { | |
outputString("</tr>"); | |
} | |
} | |
outputString("</tbody></table>"); | |
} | |
//Usage: | |
$theads = ['Player', 'Score', 'Bonus']; | |
$rows = ['George', '44', '8', 'Ben', '39', '6', 'Tom', '41', '5']; | |
tableBuilder($theads, $rows); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
custom make table php class
`<?php
ini_set('display_errors', 1);
class Table
{
}
$table = new Table();
print $table->makeTable();`