<?php
include 'mysql.php';
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'png');
define('DB_TYPE', 'mysql');
define('DB_PREFIX', 'png_');
// $System_Base = new System_Base();
// $System_Base -> buildTree();
// $System_Base -> printTree();
// $System_HTML = new System_HTML();
// $System_HTML -> buildTree();
// $System_HTML -> printTree();
$System_XSLT = new System_XSLT();
$System_XSLT ->buildTree();
$System_XSLT -> printTree();
class System_Base {
protected $db; // Объект работы с СУБД MySQL
protected $data; // Данные из СУБД
protected $tree; // Построенное дерево элементов
final public function __construct() {
if(ini_get('display_errors') != 1) {
ini_set('display_errors', 1);
}
error_reporting(E_ALL | E_STRICT); // установим нужный уровень репортинга
$this->db = MySQL::getInstance(); // Получаем ссылку на объект MySQL
$this->data = array();
$this->tree = array();
$this->getData();
}
final protected function getData() {
// Отправим запрос с нехитрой группировкой из php
// см MySQL::grouppedRows
$this->data = $this->db->grouppedRows('SELECT * FROM `test_table` ORDER BY id_parent ASC, order_num ASC', 'title');
}
final public function buildTree() {
$this->tree = $this->getChildren();
}
final public function getChildren($id = 0) {
$children = array();
foreach ($this->data as $item) {
if($item['id_parent'] == $id) { // Если мы ищем этот элемент
$children[$item['order_num']] = $item; // сохраняем инфу о нем
if($id != -1) { // Если это - не null-элемент (введен, чтобы избежать зацикливания)
$children[$item['order_num']]['children'] = $this->getChildren($item['id_item'], 1); // рекурсивный вызов
if(!sizeof($children[$item['order_num']]['children'])) { // Если детей у элемента нет - убьем соотв.индекс
unset($children[$item['order_num']]['children']);
}
}
}
}
return $children;
}
public function printTree() {
echo '<pre>';
print_r($this->tree);
echo '</pre>';
}
}
class System_HTML extends System_Base {
public function printTree() {
echo $this->showTree();
}
protected function showTree($data = false) {
$_template = '<div style="padding-left: 25px;">'."\n".'%s'."\n".'</div>'."\n";
if(!$data) {
$data = $this->tree;
}
$html = '';
foreach ($data as $item) {
$html .= "{$item['id_item']} - {$item['title']}<br />";
if(isset($item['children'])) {
$html .= $this->showTree($item['children']);
}
}
return sprintf($_template,$html);
}
}
class System_XSLT extends System_Base {
public function printTree() {
$doc = new DOMDocument();
$xsl = new XSLTProcessor();
$doc->load('styles.xsl');
$xsl->importStyleSheet($doc);
$doc->load('data.xml');
echo $xsl->transformToXML($doc);
}
}