-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
335 lines (205 loc) · 7.01 KB
/
index.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<?php
$pdo = new PDO('mysql:dbname=test_base_task;host=mysql', 'root', 'secret', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
// take initial group of user choise
$inital = $_GET['x'] ?? 0;
// Main func: produce group navs and counts
function display_stuff($parent_group=0) {
global $pdo;
global $inital;
// taking group path in tree
// add 0 and init element to array (kind of lame need to add to get_path function)
$full_path_arr = array_merge(array(0), get_path($inital), array($inital));
// retrieve children that belong to full_path_arr only
if (in_array($parent_group, $full_path_arr)) {
// get group name and id
$chunk = $pdo->query('SELECT name,id FROM groups WHERE id_parent="'.$parent_group.'"');
// display each child with count
foreach ($chunk as $row) {
// geting all products of the group and its children
$str_of_products_ids = all_prods_of_group($row['id']);
// count all products of the group and its children
$prod_count_obj = $pdo->query(' SELECT count(*) FROM products WHERE id_group in ('.$str_of_products_ids.') ');
$prod_count = $prod_count_obj->fetch(PDO::FETCH_COLUMN);
// printing navs
$color = 0;
if ($inital == $row['id']) {
$color = "style='color:limegreen'";
};
echo "<ul>";
echo "<li>"."<a ".$color." href='?x=".$row['id']."'>".$row['name']." "."(".$prod_count.")"."</a>"."</li>";
// call this function again to display this
// child's children
display_stuff($row['id']);
};
};
echo "</ul>";
}
// Produce product list of particular group with children
function show_products($group_id) {
global $pdo;
$group_prods = all_prods_of_group($group_id);
if ($group_id == 0) {
$prod_list = $pdo->query(' SELECT name FROM products ');
} else {
$prod_list = $pdo->query(' SELECT name FROM products WHERE id_group in ('.$group_prods.')');
}
foreach ($prod_list as $row) {
echo "<li>".$row['name']."</li>";
}
}
// Produce path from group to the top of hierarchy
function get_path($node) {
global $pdo;
// if no node supplied or node=0 to not bother db
// cos fethcing of empty pdo.object returns 'false'
// then treating it as array cos error "Trying to access array offset on value of type bool"
if (!$node) {
$path = array();
return $path;
};
// look up the parent of this node
$parent_obj = $pdo->query('SELECT id_parent FROM groups WHERE id="'.$node.'"');
$row = $parent_obj->fetch();
// gonna save the path in this array
$path = array();
// only continue if this $node isn't the root node (that's the node with id_parent=0)
if ($row['id_parent']) {
// the last part of the path to $node, is the name of the parent of $node
$path[] = $row['id_parent'];
// we should add the path to the parent of this node to the path - recursion warning
$path = array_merge(get_path($row['id_parent']), $path);
}
return $path;
}
// Produce string of products of particular group and of all child groups
// in such format eg. 3,4,5,7 (ids of products)
// NOTE it will be much better to add columns with such values to db, products count as well. And then just retrive (TODO)
function all_prods_of_group($group) {
global $pdo;
// getting all children ids of current group (https://stackoverflow.com/questions/20215744/how-to-create-a-mysql-hierarchical-recursive-query)
$chunk2 = $pdo->query('with recursive cte (id) as (
select id
from groups
where id_parent ="'.$group.'"
union all
select p.id
from groups p
inner join cte
on p.id_parent = cte.id
)
select * from cte ');
// add id of current group for proper count
$arr_of_children_ids = array_merge($chunk2->fetchAll(PDO::FETCH_COLUMN),array($group));
$result2 = implode(',',$arr_of_children_ids);
return $result2;
}
?>
<style>
* {
box-sizing: border-box;
}
/* Create two equal columns that floats next to each other */
.column {
float: left;
width: 50%;
padding: 10px;
height: 450px; /* Should be removed. Only for demonstration */
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
a:active {
color: green;
background-color: transparent;
text-decoration: underline;
}
/* Create box for Zadanie 2 */
div.container {
width: 220px;
height: 170px;
margin: 10px 50px 10px 10px;
background: lightgreen;
border: 2px groove;
float: left;
}
</style>
</head>
<body>
<h2>Задание 1</h2>
<div class="row">
<div class="column" style="background-color:#F5F5F5;">
<p><a href="/">Все товары</a></p>
<?php
// 0
display_stuff();
?>
</div>
<div class="column" style="background-color:#E8E8E8;">
<ul style="list-style: none;">
<?php
show_products($inital);
?>
</ul>
</div>
</div>
<br>
<br>
<br>
=============================================================================
<h2>Задание 2</h2>
<p id="coords"></p>
<div class="container"></div>
</body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js">
</script>
<script>
(function ( $ ) {
$.fn.trackCoords = function( options ) {
// This is the easiest way to have default options.
var settings = $.extend({
// These are the defaults.
checkInterval: "30",
sendInterval: "3000"
}, options );
// var for initialization
var mousePosition = 'mouse is not in area, time - ';
var listToStoreData = [];
this.mousemove(function(e) {
const selector = document.getElementById("coords");
// const inPointTime = new Date().getTime();
// mousePosition = {'x': e.pageX, 'y': e.pageY};
mousePosition = "Coords: X: "+e.clientY+" Y: "+e.clientX+" Time since Jan 1, 1970: ";
});
setInterval(function () {
// do something with mousePosition
const selector = document.getElementById("coords");
const inPointTime = new Date().getTime();
dataToRec = mousePosition+' '+inPointTime
selector.textContent=dataToRec;
listToStoreData.push(dataToRec);
// window.console && console.log(inPointTime);
// window.console && console.log(listToStoreData);
}, settings.checkInterval);
setInterval(function () {
// do something with mousePosition
$.post( settings.url, { 'val': listToStoreData },
function( data ) {
window.console && console.log(data);
});
window.console && console.log(listToStoreData);
// empty list of coords
listToStoreData = [];
}, settings.sendInterval);
};
}( jQuery ));
$(document).ready(function(){
$('div.container').trackCoords({
checkInterval: "3000",
sendInterval: "10000",
url: "/save.php"
});
});
</script>