Commit a0c1bb30 authored by Alexandr Sitchenko's avatar Alexandr Sitchenko

First working version

parents
div {
}
table {
table-layout: fixed;
width: 300px;
height: 300px;
border: 1px solid blue;
border-collapse: collapse;
}
td {
border: 1px solid grey;
/*color: red;
background-color: white;
text-align: center;*/
}
\ No newline at end of file
<?php
$input =
[
[
"text" => "Sample text",
"cells" => "1,2,3",
"align" => "left",
"valign" => "top",
"color" => "fad413",
"bgcolor" => "17541f"
],
[
"text" => "Another text",
"cells" => "5,6,8,9",
"align" => "right",
"valign" => "bottom",
"color" => "17541f",
"bgcolor" => "fad413"
],
[
"text" => "Hi!",
"cells" => "7",
"align" => "center",
"valign" => "middle",
"color" => "fff",
"bgcolor" => "000"
],
];
?>
<?php
$sample =
[
"text" => "Sample text",
"cells" => "1,2,3",
"align" => "left",
"valign" => "top",
"color" => "fad413",
"bgcolor" => "17541f"
]
?>
\ No newline at end of file
<?php
require_once './data/input.php';
require_once './lib/class.php';
require_once './data/sample.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="robots" content="index, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="./css/style.css">
<title>Square</title>
</head>
<body>
<div>
<?php
$tbl = new Check ($input);
if (empty($data->err) == 0) {echo $data->err;}
else
{
echo "<br>";
foreach ($tbl->data as $key => $value)
{
echo "[$key] = $value<br>";
foreach ($value as $a => $b)
{
echo "{$a} => $b<br>";
}
}
$tbl->html_matrix()
?>
</table>
</div>
</div>
</body>
</html>
<?php } ?>
<?php
class Check
{
public $data;
public $sim;
public $matrix;
public $alibaba;
public $err;
public $cnt;
public $max;
public $temp;
public $stoplist;
public $k;
public function __construct($data) // output $cnt - значений квадратов в таблице, $max - сторона квадрат, $sim['keys'] $err -проверка на ошибки
{
// assign var $data value $input
$this->data = $data;
$this->sim = [];
// first level calculation
$this->cnt = count($this->data);
// check $data for array
if (!is_array($this->data))
{
$this->err = "Please, enter the correct information!";
}
elseif ($this->cnt > 0)
{
// this is a data tamplate
$this->sim['sample'] =
[
"text" => "",
"cells" => "",
"align" => "",
"valign" => "",
"color" => "",
"bgcolor" => ""
];
// keys for html swage
$k = $this->cnt;
$this->temp['temp_keys'] = [];
$this->temp['cnt'] = [];
while (--$k >= 0)
{
if (count(array_intersect_key($this->sim['sample'],$this->data{$k})) !== 6)
{
$this->err = "Variable names in the array are not valid.";
break;
}
// убираем лишние пробелы
str_replace(" ", "", $this->data{$k}['cells']);
// извлекаем цифры в массив
$this->temp['temp_keys'] = explode(",", $this->data{$k}['cells']);
$this->sim['keys'][$k] = $this->temp['temp_keys']; //html table`s key
$this->temp['cnt'] = array_merge($this->temp['cnt'], $this->temp['temp_keys']); // складываем массивы, получаем все значение целлс входные
}
ksort ($this->sim['keys']); //сортирует массив по ключам
unset($k);
$this->temp['count']['1'] = count($this->temp['cnt']); //считаем
$this->temp['cnt'] = array_flip($this->temp['cnt']);
$this->temp['count']['2'] = count(array_flip($this->temp['cnt']));
if ($this->temp['count']['1'] !== $this->temp['count']['2']) {$this->err = "The numbers of the squares coincide in 'cells'. Please enter correct data";}
else
{
sort($this->temp['cnt']);
$this->max = end($this->temp['cnt']);
$this->max = ceil(sqrt($this->max));
}
unset($temp);
}
}
public function html_matrix()
{
$this->street = 1;
$this->sim['k'] = [];
$this->sim['street'] = [];
$max = $this->max;
for ($mm = 0; $mm < $this->cnt; ++$mm)
{
$sch = count($this->sim['keys'][$mm]);
//print_r ($this->sim['keys'][$mm]);
for ($p = 0; $p < $sch; ++$p)
{
$this->sim['k'] = array($this->sim['keys'][$mm][$p] => $mm);
$this->sim['street'] = $this->sim['street'] + $this->sim['k']; // складываем массивы, получаем все значение целлс входные
}
}
// создаём матрицу
for ($str = 1; $str <= $max; ++$str)
{
for ($col=1; $col <= $max; ++$col)
{
if (array_key_exists($this->street, $this->sim['street']) == 1)
{
foreach ($this->sim['street'] as $key => $value) {
if ($key == $this->street) {$this->temp['matrix'] = $value; break;}}
$this->matrix[$str][$col] = $this->temp['matrix'];
++$this->street;
}
else
{
$this->matrix[$str][$col] = "blank";
++$this->street;
}
}
}
unset ($street, $temp);
// generatу rowspan & colspan
foreach ($this->data as $key => $value)
{
$this->data[$key] = $this->data[$key] + ['colspan' => 0,
'rowspan' => 0,
'colspan_flag' => 0,
'rowspan_flag' => 0,
'table_flag' =>0
];
}
for ($str = 1; $str <= $max; ++$str)
{
for ($col=1; $col <= $max; ++$col)
{
if ($this->matrix[$str][$col] !== "blank") #verification on empty cells in matrix
{
if ($this->data[$this->matrix[$str][$col]]['rowspan_flag'] === 1)
{
++$this->data[$this->matrix[$str][$col]]['colspan'];
}
elseif ($this->data[$this->matrix[$str][$col]]['rowspan_flag'] === 0 &&
$this->data[$this->matrix[$str][$col]]['colspan_flag'] === 0)
{
++$this->data[$this->matrix[$str][$col]]['rowspan'];
++$this->data[$this->matrix[$str][$col]]['colspan'];
$this->data[$this->matrix[$str][$col]]['rowspan_flag'] = 1;
$this->data[$this->matrix[$str][$col]]['colspan_flag'] = 1;
}
elseif ($this->data[$this->matrix[$str][$col]]['rowspan_flag'] === 2)
{
++$this->data[$this->matrix[$str][$col]]['rowspan'];
$this->data[$this->matrix[$str][$col]]['rowspan_flag'] = 3;
}
}
}
foreach ($this->data as $key => $value)
{
if ($this->data[$key]['rowspan_flag'] === 1 || $this->data[$key]['rowspan_flag'] === 3)
{
$this->data[$key]['rowspan_flag'] = 2;
}
}
}
$this->t = "#";
echo "<table>";
for ($str = 1; $str <= $max; ++$str)
{ echo "<tr>";
for ($col=1; $col <= $max; ++$col)
{
if ($this->matrix[$str][$col] === "blank")
{
echo "<td></td>";
}
elseif ($this->data[$this->matrix[$str][$col]]['table_flag'] === 0)
{
echo "<td colspan=" . $this->data[$this->matrix[$str][$col]]['colspan'] . " rowspan=" . $this->data[$this->matrix[$str][$col]]['rowspan'] . " align=" . $this->data[$this->matrix[$str][$col]]['align'] . " valign=" . $this->data[$this->matrix[$str][$col]]['valign'] . " bgcolor= " . $this->t.$this->data[$this->matrix[$str][$col]]['bgcolor'] . "><font color=" . $this->t.$this->data[$this->matrix[$str][$col]]['color'] . ">" . $this->data[$this->matrix[$str][$col]]['text'] ."</font></td>";
$this->data[$this->matrix[$str][$col]]['table_flag'] = 1;
}
}
echo "</tr>";
}
echo "<table>";
}
}
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment