<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();
?>