-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTable_Class.php
141 lines (119 loc) · 3.41 KB
/
Table_Class.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
<?php
/***********************************************************************\
|
| Tabletizer Class
| ================
| Class for creating nice HTML table from PHP array.
|
| Author: malja <github.com/malja>
| Version: 1.0
| Created: 27. 8. 2013
|
| Licence: CC-BY ( http://creativecommons.org/licenses/by/3.0/ )
|
| ---------------------------------------------------------------------
|
| Example
| =======
|
| Note: For this example you need one array with name $var.
|
| $table = new Tabletizer();
| echo $table->fromArray($var);
|
\***********************************************************************/
class Tabletizer {
//Content the HTML code of created table
private $table;
/**
* Constructor
**/
public function __constructor() {
// Set HTML code to empty string
$this->table = "";
}
/**
* Call this function to create HTML table from given PHP array
*
* array $array - Array to create HTML table from
**/
public function fromArray($array) {
// If the first parameter is not an array
if (!is_array($array)) {
// Just return it
return $array;
}
// Prepare
$this->table = "<table class='tabletizer'>";
// Table title
//$this->table .= "<tr><th colspan='2'>" . gettype($array) . "</th></tr>";
// Loop
foreach ($array as $key => $value) {
$this->table .= "<tr>";
$this->table .= "<td>" . $key . "</td>";
// If the content of $array[$key] is not an array
if (!is_array($value)) {
// Just insert its value
$this->table .= "<td>" . $value . "</td>";
} else {
// Parse array -> create new inner table
$this->table .= $this->parseTable($value);
}
// End one line
$this->table .= "</tr>";
}
// End table
$this->table .= "</table>";
//Return the final HTML table code
return $this->table;
}
/**
* Private function which takes one parameter and returns HTML code of array.
*
* array $array - Array to generate HTML table code from
**/
private function parseTable($array) {
// If the given array is not an array in fact
if (!is_array($array)) {
// Return value
return $array;
}
// Get all keys of this array
$keys = array_keys($array);
// Just suppose that this array is indexed (every key is a number)
$indexed_array = true;
// Loop
for($i = 0; $i != count($keys)-1; $i++) {
// If the key is not a number
if (!is_numeric($keys[$i])) {
// It's not a indexed but associative array
$indexed_array = false;
// End loop, we don't have to check the rest
break;
}
}
// Prepare
$innerTable = "<td><table>";
// $innerTable .= "<tr><th colspan='2'>" . gettype($array) . "(" . ($indexed_array ? "Indexed" : "Associative") . ")</th></tr>";
// Loop
foreach ($array as $key => $value) {
$innerTable .= "<tr>";
// Add key content
$innerTable .= "<td>" . $key . "</td>";
// If the value of $array[$key] is not an array
if (!is_array($value)) {
// Just add its value
$innerTable .= "<td>" . $value . "</td>";
} else {
// Call this function itself to parse array
$innerTable .= $this->parseTable($value);
}
// End one line
$innerTable .= "</tr>";
}
// End table
$innerTable .= "</table></td>";
// Return the final table
return $innerTable;
} // function parseTable()
} // class Tabletizer