I have to create a tree structure in dropdown list like this:
I'm using a recursive function for creating the tree.
My database structure like :
Tree structure code as below :
function fetchCategoryTree($parent = 0, $spacing = '', $user_tree_array = '') {
if (!is_array($user_tree_array))
$user_tree_array = array();
$module = new Modulemaster();
$module->cquery = "SELECT id,sub_module_id,module_name FROM module_master WHERE 1 AND sub_module_id = $parent AND cmp_id = 0 AND is_delete = 0 ORDER BY id ASC";
$module->action = "get";
$module_res = $module->process();
if($module_res['count'] > 0) {
foreach($module_res['res'] as $module_row_key => $module_row) {
$user_tree_array[] = array("id" => $module_row['id'], "module_name" => $spacing . $module_row['module_name']);
$user_tree_array = fetchCategoryTree($module_row['id'], $spacing . '   ', $user_tree_array);
}
}
return $user_tree_array;
}
<select class="form-control required" id="sub_module_id" name="sub_module_id">
<option value="">--Select--</option>
<?php
$categoryList = fetchCategoryTree($module_id,$spacing = '', $user_tree_array = '');
foreach ($categoryList as $row) { ?>
<option value="<?php echo $row['id']; ?>" <?php echo selected($sub_module_id, $row['id']); ?>><?php echo $row['module_name']; ?>
</option>
<?php
} ?>
</select>
How can I disable the child?
If you want to disable option try this: