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
54
55
56
<script>
	function toggleSubCategories(button)
	{
		button = $(button);
		button.parent().next().each(function() {
			$(this).css('display') == 'none' ? $(this).css('display', 'inline') : $(this).css('display', 'none');
		});
	}
</script>

<?php
	require_once 'dbdetails.php';

	mysql_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASS) or die(mysql_error());
	mysql_select_db(DATABASE_NAME) or die(mysql_error()); 
	$results = mysql_query("SELECT * FROM " . TABLE_CATEGORIES);
	
	function levelToButtonType($level)
	{
		switch ($level)
		{
			case 0: return "primary";
			case 1: return "info";
			case 2: return "success";
			case 3: return "warning";
			case 4: return "danger";
		}
	}
	
	$lastLevel = 0;
	function display_children($parent, $level)
	{
		global $lastLevel;
		$result = mysql_query("SELECT name FROM " . TABLE_CATEGORIES . " WHERE parent = '" . $parent . "'") or die(mysql_error()); 
		while ($row = mysql_fetch_array($result))
		{
			$name =  $row['name'];
			if ($level > $lastLevel) 
			{
				echo "<ul>";
			} else if ($level < $lastLevel)
			{
				echo str_repeat("</ul>", $lastLevel - $level);
			}
			echo '<li><button onclick="toggleSubCategories(this)" class="btn btn-' . levelToButtonType($level) . '">' . $name . '</button></li>';
			
			$lastLevel = $level;
			display_children($name, $level + 1); 
		}
	} 

	echo '<div id="catlist">';
	display_children('root', 0);
	echo "</div>";
	mysql_close();
?>