/
category.php
69 lines (58 loc) · 2.34 KB
/
category.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
<?php
require('lib/common.php');
/*
* Проверяет, что была выполнена отправка формы входа
*/
function is_postback()
{
return isset($_GET['catgory_id']);
}
function is_postbuy()
{
return isset($_POST['buy_product_id']);
}
/*
* Точка входа скрипта
*/
function main()
{
session_start();
// обрабатываем отправленную форму
//Если передан гет запрос, то открыть запрашиваемую страницу,
//иначе открыть категорию catgory_id=1
$dbh = db_connect();
if (is_postbuy()) {
if (is_current_user()) {
$product = array('count' => 1, 'user_id' => $_SESSION['user_id'], 'product_id' => $_POST['buy_product_id']);
db_product_incar_insert($dbh, $product);
} else redirect('login.php');
}
if (is_current_user()) {
$count_in_car = product_count_in_car($dbh);
$car_items = db_get_product_in_car_by_user($dbh);
/*Добавлен ли продукт в корзин пользователя? */
/* Если корзина пустая, то в массиве хранится значение
Array ( [0] => Array ( [total] => 0 ) ), отсюда получается следующий оператор) исправлю потом */
if($car_items[0]['total'] !== 0)
foreach ($car_items as $car_item) {
$car_productid[] = $car_item[0]['id'];
} else $car_productid[] = null;
} else {
$count_in_car = array();
$car_productid[] = null;
}
/* показать выбранную категорию*/
if(is_postback() || is_postbuy()) {
if(is_postback()) $items_result = db_product_find_by_category_id($dbh, $_GET['catgory_id']);
else {
$items_result = db_product_find_by_category_id($dbh, $_POST['get_category_id']);
$_GET['catgory_id'] = $_POST['get_category_id'];
}
} else
$items_result = db_product_find_by_category_id($dbh, 1);
$category_items = db_product_find_category_all($dbh);
db_close($dbh);
render('Category_Page_Template', array(
'items' => $items_result, 'category' => $category_items, 'count_in_car' => $count_in_car, 'car_productid' => $car_productid));
}
main();