<?php
/**
Copyright Rolf Fredriksson ROSiRO Dec 2018. https://www.webman.se
General class that collects a recursive tree of tree objects from database.
Uses 4 fields: id, parent_id, name and place from a given table stored in
the global variable $db_table. The top parent id is 0 and all other objects
must track up to this.
The class can be changed by altering the sql question, for instance
to display and sort on other fields or by adding a global variable
to keep track on which parts of the Tree that is in use.
Use like this:
// 1. Create top object: id, path to parent, name
$oTree = new Tree(0, array(0), 'top');
// 2. Build a tree of Tree objects: Tree object, path
$oTree->buildTree($oTree, array(0));
// 3. Use the object (as many times you want)
function showTree($oTree) {
foreach($oTree->getSubtrees() as $o) {
echo $o->getName(), '<br />';
// Do something more...
showTree($o); // Recursive call
}
}
showTree($oTree);
*/
class Tree {
protected $id; // int
protected $aPath; // array of parent ids, from top level down
protected $name; // str
protected $subtrees; // array of objects
function __construct($id, $aPath, $name) {
$this->id = intval($id);
$this->aPath = $aPath;
$this->name = $name;
$this->subtrees = array();
}
function buildTree($oTree, $aPath) {
global $conn, $db_table;
// Get last id from path and use it as parent to get the sub-items...
$sql = "select * from " . $conn->escape($db_table) .
" where parent_id = " . $aPath[count($aPath) - 1] . " order by place";
$rs = $conn->getResult($sql);
foreach (new LimitIterator($rs, 0) as $row) {
// Copy the path array and add the new id...
$path = $aPath;
$path[] = $row['id'];
// Create and add a new sub-tree with that id and path...
$tree = new Tree($row['id'], $path, $row['name']);
$oTree->subtrees[] = $tree;
// Build the new tree...
$this->buildTree($tree, $path); // Recursive call
}
}
function getId() { return $this->id; }
function getParentId() { return $this->aPath[count($this->aPath) - 2]; }
function getPath() { return $this->aPath; }
function getLevel() { return count($this->aPath) - 2; }
function getName() { return $this->name; }
function getSubtrees() { return $this->subtrees; }
} ?>
Filter
2018-12-03
General Tree Class
Php
A general class for constructing recursive trees (that can include a list of other trees of the same class as the tree itself)
2018-09-28
Avstånd i css3
Css
#theCalcDiv {
background:green;
height: -moz-calc(100% - (20px + 30px));
height: -webkit-calc(100% - (20px + 30px));
height: calc(100% - (20px + 30px));
display:block
}
2017-04-25
sql
Sql
select sku, count(*)
from trading_units
where sku is not null
and sku != ''
group by sku
having count(*) > 1;
2017-04-10
Kalenderinfo
Tips & tricks
Skottår:
Sker vart fjärde år, men en extra skottdag läggs till vart 100:e år.
"If the year is the last year of a century, eg. 1700, 1800, 1900, 2000,
then it is only a leap year if it is exactly divisible by 400.
Therefore, 1900 wasn't a leap year but 2000 was."
Veckonummer:
Vecka 1 är alltid den första veckan som innehåller minst 4 dagar,
beroende på vilken veckodag som anses som den första dagen i veckan.