function BuildTree Parent Childs Selected null static Level if empty C

 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
function BuildTree($Parent, $Childs, $Selected = null)
{
static $Level;
if(!empty($Childs[$Parent]))
{
$Level++;
foreach($Childs[$Parent] as $Item)
{
echo "<option value=\"".$Item['id']."\"";
if($Item['id'] == $Selected)
{
echo " selected=\"1\"";
}
echo ">".str_repeat('-', $Level).$Item['title']."</option>\n";
BuildTree($Item['id'], $Childs);
}
$Level--;
}
}
function MakeCategoriesSelect($Module, $Selected = null)
{
global $DatabaseConnection;
$Optional = null;
if(!empty($Module))
{
$Optional = " WHERE module='".$Module."'";
}
if($Query = Database::GetItems('categories', 0, FALSE, null, FALSE, $Optional))
{
$Childs = array();
$Parents = array();
foreach($Query as $Row)
{
if(!empty($Row['parent']))
{
$Childs[$Row['parent']][$Row['id']] = $Row;
} else {
$Parents[$Row['id']] = $Row;
}
}
echo "<option value=\"--\">--</option>\n";
foreach($Parents as $Parent)
{
echo "<option value=\"".$Parent['id']."";
if($Parent['id'] == $Selected)
{
echo " selected=\"1\"";
}
echo "\">".$Parent['title']."</option>\n";
BuildTree($Parent['id'], $Childs, $Selected);
}
}
}