<?php
类 表格
{
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 = ' ';
}
$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]);
$table = $this->getTable();
return $table;
}
}
$table = new Table();
print $table->makeTable();
$table = 新的Table();
print $table->makeTable();