Repository navigation
Expand file tree
/
Copy pathclass.Table.php
More file actions
113 lines (91 loc) · 3.03 KB
/
Copy pathclass.Table.php
File metadata and controls
113 lines (91 loc) · 3.03 KB
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
<?php
/*
* PHP class for working with table data
* It's need for filtering data with special conditions, like WHERE from SQL
*/
class Table {
private $table = array();
private $tempArray = array();
public function createHeader($arrayHeaderName) {
foreach ($arrayHeaderName as $item) {
$this->table[$item] = array();
}
}
public function addCell($headerName, $value) {
if (isset($this->table[$headerName]))
$this->table[$headerName][] = $value;
}
public function select($keysStr='') {
if($keysStr=='')
$arrayHeaderName = array_keys($this->table);
else {
$arrayHeaderName = explode(",",$keysStr);
}
foreach ($arrayHeaderName as $item) {
$this->tempArray[$item] = $this->table[$item];
}
return $this;
}
public function where($headerName, $condition, $value) {
$arrayHeaderName = array_keys($this->tempArray);
foreach ($this->tempArray[$headerName] as $index => $item) {
switch ($condition) {
case '>':
if($item <= $value ) {
foreach($arrayHeaderName as $header) {
unset($this->tempArray[$header][$index]);
}
}
break;
case '<':
if($item >= $value ) {
foreach($arrayHeaderName as $header) {
unset($this->tempArray[$header][$index]);
}
}
break;
case '=':
if($item != $value ) {
foreach($arrayHeaderName as $header) {
unset($this->tempArray[$header][$index]);
}
}
break;
case '!=':
if($item == $value ) {
foreach($arrayHeaderName as $header) {
unset($this->tempArray[$header][$index]);
}
}
break;
}
}
return $this;
}
public function result() {
$temp = array();
$arrayHeaderName = array_keys($this->tempArray);
foreach($arrayHeaderName as $headerName) {
foreach ($this->tempArray[$headerName] as $index => $item) {
$temp[$index][$headerName] = $item;
}
}
return $temp;
}
}
// Test
/*
$table = new Table();
$table->createHeader(array('id', 'user_id', 'price', 'date'));
$table->addCell('id', 1);
$table->addCell('user_id', 99);
$table->addCell('price', 199);
$table->addCell('date', 12345678);
$table->addCell('id', 2);
$table->addCell('user_id', 87);
$table->addCell('price', 368);
$table->addCell('date', 52325678);
$res = $table->select()->where('id', '>', 1)->result();
print_r($res);
*
*/