-
Notifications
You must be signed in to change notification settings - Fork 1
/
draw_table.php
146 lines (123 loc) · 3.51 KB
/
draw_table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
const SPACING_X = 1;
const SPACING_Y = 0;
const JOINT_CHAR = '+';
const LINE_X_CHAR = '-';
const LINE_Y_CHAR = '|';
$table = array(
array(
'Name' => 'Trixie',
'Color' => 'Green',
'Element' => 'Earth',
'Likes' => 'Flowers'
),
array(
'Name' => 'Tinkerbell',
'Element' => 'Air',
'Likes' => 'Singing',
'Color' => 'Blue'
),
array(
'Element' => 'Water',
'Likes' => 'Dancing',
'Name' => 'Blum',
'Color' => 'Pink'
),
);
function draw_table($table)
{
$nl = "\n";
$columns_headers = columns_headers($table);
$columns_lengths = columns_lengths($table, $columns_headers);
$row_separator = row_seperator($columns_lengths);
$row_spacer = row_spacer($columns_lengths);
$row_headers = row_headers($columns_headers, $columns_lengths);
#sanity check for null array
if ($table == false) {
return;
}
#echo '<pre>';
echo $row_separator . $nl;
echo str_repeat($row_spacer . $nl, SPACING_Y);
echo $row_headers . $nl;
echo str_repeat($row_spacer . $nl, SPACING_Y);
echo $row_separator . $nl;
echo str_repeat($row_spacer . $nl, SPACING_Y);
foreach ($table as $row_cells) {
$row_cells = row_cells($row_cells, $columns_headers, $columns_lengths);
echo $row_cells . $nl;
echo str_repeat($row_spacer . $nl, SPACING_Y);
}
echo $row_separator . $nl;
#echo '</pre>';
}
function columns_headers($table)
{
return array_keys(reset($table));
}
function columns_lengths($table, $columns_headers)
{
$lengths = [];
foreach ($columns_headers as $header) {
$header_length = strlen($header);
$max = $header_length;
foreach ($table as $row) {
$length = strlen($row[$header]);
if ($length > $max) {
$max = $length;
}
}
if (($max % 2) != ($header_length % 2)) {
$max += 1;
}
$lengths[$header] = $max;
}
return $lengths;
}
function row_seperator($columns_lengths)
{
$row = '';
foreach ($columns_lengths as $column_length) {
$row .= JOINT_CHAR . str_repeat(LINE_X_CHAR, (SPACING_X * 2) + $column_length);
}
$row .= JOINT_CHAR;
return $row;
}
function row_spacer($columns_lengths)
{
$row = '';
foreach ($columns_lengths as $column_length) {
$row .= LINE_Y_CHAR . str_repeat(' ', (SPACING_X * 2) + $column_length);
}
$row .= LINE_Y_CHAR;
return $row;
}
function row_headers($columns_headers, $columns_lengths)
{
$row = '';
foreach ($columns_headers as $header) {
$row .= LINE_Y_CHAR . str_pad($header, (SPACING_X * 2) + $columns_lengths[$header], ' ', STR_PAD_BOTH);
}
$row .= LINE_Y_CHAR;
return $row;
}
function row_cells($row_cells, $columns_headers, $columns_lengths)
{
$row = '';
foreach ($columns_headers as $header) {
$row .= LINE_Y_CHAR . str_repeat(' ', SPACING_X) . str_pad($row_cells[$header], SPACING_X + $columns_lengths[$header], ' ', STR_PAD_RIGHT);
}
$row .= LINE_Y_CHAR;
return $row;
}
//draw_table($table);
/*
* Example output:
* +------------+-------+---------+---------+
* | Name | Color | Element | Likes |
* +------------+-------+---------+---------+
* | Trixie | Green | Earth | Flowers |
* | Tinkerbell | Blue | Air | Singing |
* | Blum | Pink | Water | Dancing |
* +------------+-------+---------+---------+
*/