Вот код:
/Catalog/index.php
<?php
include 'config.php';
include 'cart_fns.php';
session_start();
if(!isset($_SESSION['cart']))
{
$_SESSION['cart'] = array();
$_SESSION['total_items'] = 0;
$_SESSION['total_price'] = '0.00';
}
$view = empty($_GET['view']) ? 'index' : $_GET['view'];
include($_SERVER['DOCUMENT_ROOT'].'/catalog/views/layouts/shop.php');
?>
/Catalog/Functions.php
<?php
/**
* Распечатка массива
**/
function print_arr($array){
echo "<pre>" . print_r($array, true) . "</pre>";
}
/**
* Получение массива категорий
**/
function get_cat(){
global $connection;
$query = "SELECT * FROM categories";
$res = mysqli_query($connection, $query);
$arr_cat = array();
while($row = mysqli_fetch_assoc($res)){
$arr_cat[$row['id']] = $row;
}
return $arr_cat;
}
/**
* Построение дерева
**/
function map_tree($dataset) {
$tree = array();
foreach ($dataset as $id=>&$node) {
if (!$node['parent']){
$tree[$id] = &$node;
}else{
$dataset[$node['parent']]['childs'][$id] = &$node;
}
}
return $tree;
}
/**
* Дерево в строку HTML
**/
function categories_to_string($data){
foreach($data as $item){
$string .= categories_to_template($item);
}
return $string;
}
/**
* Шаблон вывода категорий
**/
function categories_to_template($category){
ob_start();
include 'category_template.php';
return ob_get_clean();
}
/**
* Хлебные крошки
**/
function breadcrumbs($array, $id){
if(!$id) return false;
$count = count($array);
$breadcrumbs_array = array();
for($i = 0; $i < $count; $i++){
if($array[$id]){
$breadcrumbs_array[$array[$id]['id']] = $array[$id]['title'];
$id = $array[$id]['parent'];
}else break;
}
return array_reverse($breadcrumbs_array, true);
}
/**
* Получение ID дочерних категорий
**/
function cats_id($array, $id){
if(!$id) return false;
foreach($array as $item){
if($item['parent'] == $id){
$data .= $item['id'] . ",";
$data .= cats_id($array, $item['id']);
}
}
return $data;
}
/**
* Получение товаров
**/
function get_products($ids = false){
global $connection;
if($ids){
$query = "SELECT * FROM products WHERE parent IN($ids) ORDER BY title";
}else{
$query = "SELECT * FROM products ORDER BY title";
}
$res = mysqli_query($connection, $query);
$products = array();
while($row = mysqli_fetch_assoc($res)){
$products[] = $row;
}
return $products;
}
function get_product($id)
{
global $connection;
$query = ("SELECT * FROM products WHERE id='$id' ");
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_array($result);
return $row;
}
/Catalog/config.php
<?php
define("DBHOST", "localhost");
define("DBUSER", "root");
define("DBPASS", "123");
define("DB", "apple");
$connection = @mysqli_connect(DBHOST, DBUSER, DBPASS, DB) or die("Нет соединения с БД");
mysqli_set_charset($connection, "utf8") or die("Не установлена кодировка соединения");
?>[/php]
/Catalog/catalog.php
[code=htmlphp]<?php
include 'config.php';
include 'functions.php';
$categories = get_cat();
$categories_tree = map_tree($categories);
$categories_menu = categories_to_string($categories_tree);
if(isset($_GET['category'])){
$id = (int)$_GET['category'];
// хлебные крошки
// return true (array not empty) || return false
$breadcrumbs_array = breadcrumbs($categories, $id);
if($breadcrumbs_array){
$breadcrumbs = "<a href='/catalog/'>Главная</a> / ";
foreach($breadcrumbs_array as $id => $title){
$breadcrumbs .= "<a href='?category={$id}'>{$title}</a> / ";
}
$breadcrumbs = rtrim($breadcrumbs, " / ");
$breadcrumbs = preg_replace("#(.+)?<a.+>(.+)</a>$#", "$1$2", $breadcrumbs);
}else{
$breadcrumbs = "<a href='/catalog/'>Главная</a> / Каталог";
}
// ID дочерних категорий
$ids = cats_id($categories, $id);
$ids = !$ids ? $id : rtrim($ids, ",");
if($ids) $products = get_products($ids);
else $products = null;
}else{
$products = get_products();
}
/Catalog/cart_fns.php
<?php
function add_to_cart($id)
{
if(isset($_SESSION['cart'][$id]))
{
$_SESSION['cart'][$id]++;
return true;
}
else
{
$_SESSION['cart'][$id] = 1;
return true;
}
return false;
}
?>
/Catalog/pages/add_to_cart
<?php
$id = $_GET['id'];
$add_item = add_to_cart($id);
header('Location: index.php?view=product&id='.$id);
?>
/Catalog/pages/cart.php
<h2 align="center">Ваша корзина товаров</h2>
<form action="" method="post" id="cart-form">
<table id="mycart" align="center" cellspacing="0" cellpadding="0" border="0" width="600" height="100">
<tr>
<th>Товар</th>
<th>Цена</th>
<th>Кол-во</th>
<th>Всего</th>
</tr>
<?php foreach($_SESSION['cart'] as $id => $quantity):
$product = get_product($id);
?>
<tr>
<td align="center"><?php echo $product['title'];?></td>
<td align="center"><?php echo $product['price'];?></td>
<td align="center"><input type="text" size="2" name="" <?php echo $id;?> maxlength="2" value="<?php echo $quantity;?>"/></td>
<td align="center"><?php echo $product['price'] * $quantity;?></td>
</tr>
<?endforeach;?>
</table>
<p class="total" align="center">Общая сумма заказа: <span class="product-price">Руб.</span></p>
<p align="center"><input type="submit" name="update" value="Обновить"/></p>
</form>
/Catalog/pages/product.php
<?php
$id = $_GET['id'];
$product = get_product($id);
?>
<a href="#"><?php echo $product['title']?></a>
<a href="index.php?view=add_to_cart&id=<?php echo $product['id']?>">Добавить в корзину</a>
/Catalog/pages/index.php
<p><?=$breadcrumbs;?></p>
<br>
<hr>
<?php if($products): ?>
<?php foreach($products as $product): ?>
<a href="index.php?view=product&id=<?=$product['id']?>"><?=$product['title']?></a><br>
<?php endforeach; ?>
<?php else: ?>
<p>Здесь товаров нет!</p>
<?php endif; ?>