Skip to content

Instantly share code, notes, and snippets.

@cp6
Created September 24, 2020 03:41
Show Gist options
  • Save cp6/8841314b4eda99c9c561162b880632f6 to your computer and use it in GitHub Desktop.
Save cp6/8841314b4eda99c9c561162b880632f6 to your computer and use it in GitHub Desktop.
PHP function for making HTML tables
<?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);
@ekariz
Copy link

ekariz commented Dec 20, 2022

custom make table php class

`<?php

ini_set('display_errors', 1);

class Table
{

private $table;
private $rows;

public function __construct()
{

}



function valueof($var, $key, $default_return_value = null, $run_value_in_this_function = '')
{

    $return_value = $default_return_value;

    if (is_object($var)) {
        if (isset($var->$key)) {
            $return_value = trim($var->$key);
        }
    } elseif (is_array($var)) {
        if (isset($var[$key])) {
            $value = $var[$key];
            $return_value = is_string($value) ? trim($value) : $value;
        }
    } else {
        $return_value = $default_return_value;
    }

    if (!empty($return_value) && !empty($run_value_in_this_function)) {
        if (function_exists($run_value_in_this_function)) {
            $return_value = $run_value_in_this_function($return_value);
        }
    }

    return $return_value;
}

function addRow($index)
{

    $this->rows[$index] = [];

    $this->table['rows'][$index] = [];

}

function addRows($rows)
{
    $rows = (int) $rows;
    for ($r = 0; $r <= $rows; ++$r) {

        $this->rows[$r] = [];

        $this->table['rows'][$r] = [];
    }

}

function addCell($row_index, $cell_index, $cell_value)
{
    $this->table['rows'][$row_index][$cell_index] = $cell_value;
}

function updateCell($row_index, $cell_index, $cell_value)
{
    $this->table['rows'][$row_index][$cell_index] = $cell_value;
}

function updateColumn($column_index, $cell_values = [])
{

    if (isset($this->table['rows'])) {
        if (count($this->table['rows']) > 0) {
            foreach ($this->table['rows'] as $row_index => $row_cells) {

                $value_index = $row_index;
                $new_value =  $this->valueof($cell_values, $value_index);

                if (!is_null($new_value)) {
                    $this->updateCell($row_index, $column_index, $new_value);
                }
            }
        }
    }

}

function updateRow($row_index, $row_values = [])
{

    if (isset($this->table['rows'][$row_index])) {

        $columns = count($this->table['rows'][$row_index]);

        foreach ($this->table['rows'][$row_index] as $column_index => $column_value) {

            $value_index = $column_index-1;
            $new_value =  $this->valueof($row_values, $value_index);

            if (!is_null($new_value)) {
                $this->updateCell($row_index, $column_index, $new_value);
            }
        }
    }

}
function addHeader($row_values = [])
{

    if (!empty($row_values)) {
        $this->updateRow(0, $row_values);
    }

}

function addColum($column_index)
{

    if (isset($this->table['rows'])) {
        if (count($this->table['rows']) > 0) {
            foreach ($this->table['rows'] as $row_index => $row_cells) {
                $this->table['rows'][$row_index][$column_index] = null;
            }
        }
    }
}
function addColums($columns)
{

    $columns = (int) $columns;

    for ($col = 1; $col <= $columns; ++$col) {
        if (isset($this->table['rows'])) {
            if (count($this->table['rows']) > 0) {
                foreach ($this->table['rows'] as $row_index => $row_cells) {
                    $this->table['rows'][$row_index][$col] = null;
                }
            }
        }
    }
}

public function getTable()
{


    if (isset($this->table['rows']) && is_countable($this->table['rows'])) {

        $table = "<table border=\"1\" width=\"50%\">";

        foreach ($this->table['rows'] as $row_index => $row_cells) {

            $table .= "<tr>";

            if (is_countable($row_cells)) {
                foreach ($row_cells as $cell_index => $cell_value) {

                    if (empty($cell_value)) {
                        $cell_value = '&nbsp;';
                    }

                    $table .= "<td class=\"data\"> {$cell_value}</td>";

                }
            }

            $table .= "</tr>";

        }

        $table .= "</table>";

    }

    return $table;

}

function makeTable()
{

    $this->addRows(5);
    $this->addColums(4);

    $this->addHeader(['No', 'Name', 'Unit', 'Number']);
    $this->updateColumn(1, [null, '1', '2', '3', '4', '5' ]);
    $this->updateColumn(2, [null, 'Cat', 'Dog', 'Horse', 'Cow', 'Hen']);
    $this->updateColumn(3, [null, 'Each', 'Each', 'Each', 'Each' ]);
    $this->updateColumn(3, [null, 10,20, 5, 50, 45]);

    // echo '<pre>';
    // print_r($this->table);
    // echo '</pre>';

    $table = $this->getTable();

    return $table;


}

}

$table = new Table();

print $table->makeTable();`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment