-
Notifications
You must be signed in to change notification settings - Fork 2
/
index (2).php
576 lines (388 loc) · 18.2 KB
/
index (2).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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
<?php
require_once 'db_config.php';
include('simplehtmldom_1_5/simple_html_dom.php');
$link = mysqli_connect($host, $user, $password, $database)
or die("Ошибка " . mysqli_error($link));
// try {
// $link = mysqli_connect($host, $user, $password, $database);
// } catch (mysqli_sql_exception $e) {
// print "Error!: " . $e->getMessage() . "<br/>";
// die();
// }
$host = 'localhost'; // адрес сервера
$db = 'id7219699_appculinary'; // имя базы данных
$user = 'id7219699_qwerty'; // имя пользователя
$pass = 'qwerty777'; // пароль
$charset = 'utf8';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
//$pdo = new PDO($dsn, $user, $pass, $opt);
try {
$pdo = new PDO($dsn, $user, $pass, $opt);
$res['status2'] = "Connection success";
} catch (PDOException $e) {
$res['status2'] = "Connection fail";
die('Подключение не удалось: ' . $e->getMessage());
}
if ($_REQUEST['proc'] == "logIn") {
$sql = "SELECT EXISTS (SELECT * FROM clients WHERE login = :login and password = :password)";
$stmt = $pdo->prepare($sql);
$stmt->bindValue(':login', $_REQUEST['login']);
$stmt->bindValue(':password', $_REQUEST['password']);
$stmt->execute();
if ($stmt->fetchColumn()){
$res['status'] = "yes";
} else $res['status'] = "Неверный логин или пароль!";
$pdo = null;
$dsn = null;
} else if ($_REQUEST['proc'] == "createAccount") {
try{
$sql = "INSERT INTO `clients` (`login`,`password`,`email`) VALUES (?, ?, ?)";
$stmt = $pdo->prepare($sql);
// $stmt->bindValue(':login', $_REQUEST['newLogin']);
/// $stmt->bindValue(':password', $_REQUEST['newPassword']);
// $stmt->bindValue(':email', $_REQUEST['newEmail']);
$stmt->execute([$_REQUEST['newLogin'], $_REQUEST['newPassword'], $_REQUEST['newEmail']]);
//$stmt->execute();
$res['status'] = "yes";
}
catch(PDOException $e)
{
$res['status'] = "no";
}
$pdo = null;
$dsn = null;
} else if ($_REQUEST['proc'] == "selection") {
$products = $_REQUEST['sql'];
$productsAll = $_REQUEST['sqlAll'];
$countParam = $_REQUEST['countParam'];
//echo $countParam."<br>";
$productsAll = str_replace("P", "%", $productsAll);
$products = str_replace("P", "%", $products);
//echo $products."<br>";
if ($countParam > 0) {
$param = "";
$ar1 = [];
$ar2 = [];
for ($i = 0; $i < $countParam; $i++) {
$_REQUEST['param' . $i] = str_replace("P", "%", $_REQUEST['param' . $i]);
//echo $_REQUEST['param' . $i]."</br>";
if($i == 0)
{
$param = "SELECT DISTINCT table2.id from table2 WHERE " . $_REQUEST['param' . $i];
$result1 = mysqli_query($link, $param) or die("Ошибка " . mysqli_error($link));
while ($resId = mysqli_fetch_array($result1)) {
$ar1[] = $resId['id'];
}
//echo print_r($ar1)."<br>";
}
else
{
$param = "SELECT DISTINCT table2.id from table2 WHERE " . $_REQUEST['param' . $i];
$result1 = mysqli_query($link, $param) or die("Ошибка " . mysqli_error($link));
$ar2 = [];
while ($resId = mysqli_fetch_array($result1)) {
$ar2[] = $resId['id'];
}
$ar1 = array_intersect($ar1, $ar2);
//echo print_r($ar1)."<br>";
}
// if ($countParam - 1 != $i)
// $param .= "SELECT DISTINCT table2.id from table2 WHERE " . $_REQUEST['param' . $i] . " in ";
// else $param .= "SELECT DISTINCT table2.id from table2 WHERE " . $_REQUEST['param' . $i];
}
//echo print_r($ar1)."<br>";
// $param .= " GROUP by table2.id";
// echo $param."<br>";
$productsAll = str_replace("+", " ", $productsAll);
$products = str_replace("+", " ", $products);
$sqlAll = "SELECT DISTINCT table2.id from table2 WHERE table2.id NOT IN (SELECT table2.id from table2 WHERE table2.name NOT IN (" . $productsAll . "))";
$sql = "SELECT DISTINCT table2.id from table2 WHERE table2.id NOT IN (SELECT table2.id from table2 WHERE table2.name NOT IN (" . $products . "))";
//$param .= $sql;
//$result1 = mysqli_query($link, $param) or die("Ошибка " . mysqli_error($link));
$result2 = mysqli_query($link, $sqlAll) or die("Ошибка " . mysqli_error($link));
$result3 = mysqli_query($link, $sql) or die("Ошибка " . mysqli_error($link));
$arr1 = $ar1;
$arr2 = [];
$arr3 = [];
$arrAll = [];
// while ($resId = mysqli_fetch_array($result1)) {
// $arr1[] = $resId['id'];
// }
while ($resId = mysqli_fetch_array($result2)) {
$arr2[] = $resId['id'];
$arrAll[] = $resId['id'];
}
while ($resId = mysqli_fetch_array($result3)) {
$arr3[] = $resId['id'];
}
$intersect = array_intersect($arr1, $arr2); //пересечение ограничений и всех продуктов
$intersect = array_merge($intersect,$arr3); //объединение верхнего и продуктов без ограничений
$clearZapros = array_diff($arr2, $intersect); // продукты с недостающим количеством
$plusProdZapros = "SELECT id,name from table2 WHERE name NOT IN (" . $products . ") GROUP BY id HAVING COUNT(id) <= 1";
$resultPlusProd = mysqli_query($link, $plusProdZapros) or die("Ошибка " . mysqli_error($link));
$res = [];
$valueArr = [];
$valueArrName = [];
while ($val = mysqli_fetch_array($resultPlusProd)) {
$valueArr[] = $val['id'];
$valueArrName[] = $val['name'];
}
$valueArr = array_diff($valueArr, $clearZapros);
$num = 0;
foreach ($valueArr as &$value){
$query = "SELECT * from table1 where table1.id=" . $value;
$result = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link));
if ($result) {
$row = mysqli_fetch_row($result); // количество полученных строк
$tmp1['id'] = $row[0];
$tmp2['name'] = $row[1];
$tmp3['serv'] = $row[2];
$tmp4['time'] = $row[3];
$tmp4['descr'] = $row[4];
$tmp5['img'] = $row[5];
$tmp6['flag'] = "Не хватает продукта";
$num = $num+1;
$tmp = [$tmp1['id'], $tmp2['name'], $tmp3['serv'], $tmp4['time'], $tmp4['descr'], $tmp5['img'], $tmp6['flag']];
$res[] = $tmp;
}
}
foreach ($clearZapros as &$value) {
$query = "SELECT * from table1 where table1.id=" . $value;
$result = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link));
if ($result) {
$row = mysqli_fetch_row($result); // количество полученных строк
$tmp1['id'] = $row[0];
$tmp2['name'] = $row[1];
$tmp3['serv'] = $row[2];
$tmp4['time'] = $row[3];
$tmp4['descr'] = $row[4];
$tmp5['img'] = $row[5];
$tmp6['flag'] = "Не достает количества у продукта";
$tmp = [$tmp1['id'], $tmp2['name'], $tmp3['serv'], $tmp4['time'], $tmp4['descr'], $tmp5['img'], $tmp6['flag']];
$res[] = $tmp;
}
}
foreach ($intersect as &$value) {
$query = "SELECT * from table1 where table1.id=" . $value;
$result = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link));
if ($result) {
$row = mysqli_fetch_row($result); // количество полученных строк
$tmp1['id'] = $row[0];
$tmp2['name'] = $row[1];
$tmp3['serv'] = $row[2];
$tmp4['time'] = $row[3];
$tmp4['descr'] = $row[4];
$tmp5['img'] = $row[5];
$tmp6['flag'] = "";
$tmp = [$tmp1['id'], $tmp2['name'], $tmp3['serv'], $tmp4['time'], $tmp4['descr'], $tmp5['img'], $tmp6['flag']];
$res[] = $tmp;
}
}
} else {
$products = str_replace("+", " ", $products);
$param = "SELECT DISTINCT table2.id from table2 WHERE table2.id NOT IN (SELECT table2.id from table2 WHERE table2.name NOT IN (" . $products . "))";
//echo $param."<br>";
$query = $param;
$resultAll = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link));
$count = 3;
//ы$clearZapros = array_diff($arr2, $intersect); // продукты с недостающим количеством
$res = [];
while ($resId = mysqli_fetch_array($resultAll)) {
$query = "SELECT * from table1 where table1.id=" . $resId['id'];
$result = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link));
if ($result) {
$row = mysqli_fetch_row($result); // количество полученных строк
$tmp1['id'] = $row[0];
$tmp2['name'] = $row[1];
$tmp3['serv'] = $row[2];
$tmp4['time'] = $row[3];
$tmp4['descr'] = $row[4];
$tmp5['img'] = $row[5];
$tmp6['flag'] = "";
$tmp = [$tmp1['id'], $tmp2['name'], $tmp3['serv'], $tmp4['time'], $tmp4['descr'], $tmp5['img'], $tmp6['flag']];
$res[] = $tmp;
}
}
$valueArr = [];
$valueArrName = [];
$plusProdZapros = "SELECT id, name from table2 WHERE name NOT IN (" . $products . ") GROUP BY id HAVING COUNT(id) <= 1";
$resultPlusProd = mysqli_query($link, $plusProdZapros) or die("Ошибка " . mysqli_error($link));
while ($val = mysqli_fetch_array($resultPlusProd)) {
$valueArr[] = $val['id'];
$valueArrName[] = $val['name'];
}
$num = 0;
foreach ($valueArr as &$value){
$query = "SELECT * from table1 where table1.id=" . $value;
$result = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link));
if ($result) {
$row = mysqli_fetch_row($result); // количество полученных строк
$tmp1['id'] = $row[0];
$tmp2['name'] = $row[1];
$tmp3['serv'] = $row[2];
$tmp4['time'] = $row[3];
$tmp4['descr'] = $row[4];
$tmp5['img'] = $row[5];
$tmp6['flag'] = $valueArrName[$num];
$num = $num+1;
$tmp = [$tmp1['id'], $tmp2['name'], $tmp3['serv'], $tmp4['time'], $tmp4['descr'], $tmp5['img'], $tmp6['flag']];
$res[] = $tmp;
}
}
}
} else if ($_REQUEST['proc'] == "added") {
$query = "SELECT name, type FROM table2 GROUP BY name";
//SELECT name,type FROM table2 GROUP BY name
$result = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link));
// $count = 0;
$res = [];
while ($result2 = mysqli_fetch_array($result)) {
// $srt = $count."q";
$tmp1['name'] = $result2[0];
$tmp2['type'] = $result2[1];
$tmp = [$tmp1['name'], $tmp2['type']];
$res[] = $tmp;
//$res[$srt] = $result2['name'];
//if($count == 15) break;
// $count++;
}
//$res["size"] = $count;
}else if ($_REQUEST['proc'] == "idRecipe") {
$query = "SELECT * from table2 where id=".$_REQUEST['id'];
//SELECT name,type FROM table2 GROUP BY name
$result = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link));
// $count = 0;
$res = [];
while ($result2 = mysqli_fetch_array($result)) {
// $srt = $count."q";
//echo $result2[0]." ".$result2[1]." ".$result2[2]." ".$result2[3]."<br>";
$tmp1['name'] = $result2[1];
$tmp1['count'] = $result2[2];
$tmp2['type'] = $result2[3];
$tmp = [$tmp1['name'],$tmp1['count'], $tmp2['type']];
$res[] = $tmp;
}
}
else if ($_REQUEST['proc'] == "sales") {
$query = "SELECT * from sales where caterogy='".$_REQUEST['nameProduct']."'";
$result = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link));
$res = [];
while ($result2 = mysqli_fetch_array($result)) {
$tmp1['name'] = $result2[1];
$tmp1['caterogy'] = $result2[2];
$tmp1['shop'] = $result2[3];
$tmp1['price'] = $result2[4];
$tmp = [$tmp1['name'],$tmp1['caterogy'], $tmp1['shop'],$tmp1['price']];
$res[] = $tmp;
}
}
else if ($_REQUEST['proc'] == "userRecipe") {
$query = "SELECT * from table1 where id in ".$_REQUEST['ids'];
$result = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link));
$res = [];
while ($row = mysqli_fetch_array($result)) {
$tmp1['id'] = $row[0];
$tmp1['name'] = $row[1];
$tmp1['serv'] = $row[2];
$tmp1['time'] = $row[3];
$tmp1['descr'] = $row[4];
$tmp1['img'] = $row[5];
$tmp = [$tmp1['id'], $tmp1['name'], $tmp1['serv'], $tmp1['time'], $tmp1['descr'], $tmp1['img']];
$res[] = $tmp;
}
}
else if ($_REQUEST['proc'] == "addRecipe") {
$query = "SELECT COUNT(*) FROM table1";
$result = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link));
$row = mysqli_fetch_row($result);
//echo $row[0]."<br>";
$id = $row[0]+55;
//echo $id."<br>";
$query = "INSERT INTO `table1` (`id`, `name`, `time`, `servings`, `description`, `image`) VALUES ($id, '".$_REQUEST['nameRecipe']."', '5', '30', '".$_REQUEST['decrRecipe']."', 'rr450')";
//echo $query."<br>";
$result = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link));
//$row = mysqli_fetch_row($result);
if ($result) {
$res['statusRecipeAdded'] = "yes";
$res['id'] = $id;
} else $res['statusRecipeAdded'] = "error added";
//$query = "INSERT INTO `table2` (`id`, `name`, `count`, `type`) VALUES ('1', 'n1', 'c1', 't1'), ('1', 'n2', 'c2', 't2')";
$query ="INSERT INTO `table2` (`id`, `name`, `count`, `type`) VALUES ";
for($i = 0; $i < $_REQUEST['countP']; $i++)
{
$str = "('$id', '".$_REQUEST['nameIngr'.$i]."', '".$_REQUEST['countIngr'.$i]."', '".$_REQUEST['typeIngr'.$i]."'),";
$query .= $str;
}
$query = mb_substr($query, 0, -1);
$result = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link));
if ($result) {
$res['statusProductsAdded'] = "yes";
$res['id'] = $id;
} else $res['statusProductsAdded'] = "error added";
}
/*
* else if($_REQUEST['proc'] == "parse") {
$query="SELECT DISTINCT table2.name FROM table2";
//SELECT name,type FROM table2 GROUP BY name
$result = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link));
$count = 0;
while ($result2 = mysqli_fetch_array($result)) {
$srt = $count."q";
$res[$srt] = $result2['name'];
//if($count == 15) break;
$count++;
}
$res["size"] = $count;
}
// setcookie("_ga","GA1.2.1412451640.1541493703");
// setcookie("_gid","GA1.2.24325606.1541493703");
// //setcookie("_git",".edadeal.ru","Tue, 30 Oct 2018 19:10:17 GMT", ,"false","Unset");
// setcookie("_ym_uid","1541493703961634153");
// setcookie("_ym_d","1541493703");
// setcookie("_ym_visorc_34675050","w");
// setcookie("_ym_isad","2");
// setcookie("_ym_uid","1541493703961634153");
// setcookie("_ym_visorc_34675050","w");
//require_once 'phpQuery2/phpQuery/phpQuery.php';
require_once 'db_config.php';
include('simplehtmldom_1_5/simple_html_dom.php');
// $html = new simple_html_dom();
// $html = file_get_html('https://edadeal.ru/', false, null, 0);
// foreach($html->find('div') as $element)
// echo $element . '<br>';
// $html = file_get_contents('https://www.google.com/');
// $b = str_get_html($object);
// +++++
// $html = file_get_html( "https://edadeal.ru/perm/offers", false, null, 0 );
// echo $html;
// $html = file_get_html('https://www.edadeal.ru/', false, null, 0);
// $dom = str_get_html(file_get_contents('https://www.edadeal.ru/'));
// echo $dom;
// $html = file_get_html('https://edadeal.ru/', false, null, 0);
// foreach($html->find('div') as $element)
// echo $element . '<br>';
// $url = "https://edadeal.ru/";
// $curl = curl_init($url);
// curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// $page = curl_exec($curl);
// $html = file_get_html('https://kedem.ru/recipe/salads/', false, null, 0);
// $site = 'https://kedem.ru/recipe/salads/';
// $page = 1;
// $data = file_get_html('https://kedem.ru/recipe/salads/'.$page.'/', false, null, 0);
if ($_REQUEST['proc'] == "logIn") {
$login = $_REQUEST['login'];
$password = $_REQUEST['password'];
$query = "SELECT EXISTS (SELECT * FROM clients WHERE login = '" . $login . "' and password = '" . $password . "')";
$result = mysqli_query($link, $query) or die("Ошибка " . mysqli_error($link));
$row = mysqli_fetch_row($result);
if ($row[0] > 0) {
$res['status'] = "yes";
} else $res['status'] = "Неверный логин или пароль!";
*/
echo json_encode($res, JSON_UNESCAPED_UNICODE);
?>