class Tree {
private $db = null;
private $categoryArr = array();
public function __construct() {
$this->db = new PDO("mysql:dbname=class;host=localhost;charset=UTF8", "root", "123");
$this->categoryArr = $this->getCategory();
}
private function getCategory() {
$query = $this->db->prepare("SELECT * FROM `categories`");
$query->execute();
$result = $query->fetchAll(PDO::FETCH_OBJ);
$return = array();
foreach ($result as $value) {
$return[$value->id_parent][] = $value;
}
return $return;
}
public function getFullTree() {
// здесь код метода
}
}
class Tree {
public $categories = [];
public $categoriesTree = [];
private $db = null;
public function __construct() {
$this->db = new PDO("mysql:dbname=zadanie;host=localhost;charset=UTF8", "root", "");
$this->categories = $this->getCategories();
$this->categoriesTree = $this->getFullTree();
}
/*
* Список всех категорий
*/
private function getCategories(){
$arr_cat = [];
$result = $this->db->query("SELECT * FROM `categories`")->fetchAll();
foreach($result AS $category){
$arr_cat[$category['id']] = $category;
}
return $arr_cat ;
}
/*
* Список категорий в форме дерева
*/
private function getFullTree() {
$tree = [] ;
$categories = $this->categories;
foreach($categories as $id => &$node) {
if(!$node['id_parent']){
$tree[$id] = &$node ;
}else{
$categories[$node['id_parent']]['children'][$id] = &$node ;
}
}
return $tree ;
}
/*
* хлебные крошки
* $id - ID категории в которой мы находимся
*/
public function breadcrumbs($id){
$breadcrumbs_array = [];
$array = $this->categories;
for($i = 0; $i < count($array); $i++){
if($id){
$breadcrumbs_array[$array[$id]['id']] = $array[$id]['title'] ;
$id = $array[$id]['id_parent'] ;
}
}
return array_reverse($breadcrumbs_array, true) ;
}
/*
* @bonus
* Получаем ID всех дочерных категорий родителя
*/
public function getChildren($id_parent){
static $children = [];
$result = $this->db->query("SELECT `id` FROM `categories` WHERE `id_parent` = '$id_parent'")->fetchAll();
foreach($result AS $post){
$children[] = $post['id'];
$this->getChildren($post['id']);
}
return $children;
}
}
$category = new Tree();
$categories = $category->categoriesTree; // список всех категорий
$tree = $category->categoriesTree; // список категорий в форме дерева
$id_categoty = 7; // ID категории в которой мы сейчас находимся
$breadcrumbs = $category->breadcrumbs($id_categoty);
foreach($breadcrumbs AS $id => $title){
if($id == $id_categoty)
continue; // текущую категорию пропускаем
echo ' - <a href="?id=' . $id . '">' . $title . '</a>';
}
$id_parent = 2; // ID категории родителя
$children = $category->getChildren($id_parent);
//print_r($children);
// ...
$this->categories = $this->getCategory();
// ...
public function getFullTree($parentId) {
if (!isset($this->categories[$parentId])) {
return [];
}
$tree = [];
foreach($this->categories[$parentId] as $category) {
$tree[$category->id] = [
'name' => $category->name,
'children' => $this->getFullTree($category->id),
];
}
return $tree;
}
// ...
$tree = $this->getFullTree(0);