How to disable child in tree structure using php

206 views Asked by At

I have to create a tree structure in dropdown list like this:

enter image description here

I'm using a recursive function for creating the tree.

My database structure like :

enter image description here

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 . ' &nbsp     ', $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?

1

There are 1 answers

3
Militaru On

If you want to disable option try this:

foreach ($categoryList as $row)
{ 
    if(in_array($row['id'],array(4,5,6)) // array(4,5,6) id's you want to disable
    {
        $disabled = ' disabled="1" ';
    }
    else
    {
        $disabled = '';
    }
?>
    <option value="<?php echo $row['id']; ?>" <?php echo selected($sub_module_id, $row['id']); ?> <?php echo @$disabled; ?>>
        <?php echo $row['module_name']; ?>
    </option>
<?php } ?>