用于编辑页面的PHP中的递归类别树

I have created a recursive category tree in PHP, below is the code

function categoryTree($parent_id= 0, $sub_mark = ''){
        $stmt = $this->db->prepare("SELECT * FROM category WHERE cat_p_id = '".$parent_id."'");
        $stmt->execute();
        $rowCount =  $stmt->rowCount();

        if($rowCount > 0){
            while($row = $stmt->fetch(PDO::FETCH_ASSOC)){

                echo  '<option value="'.$row['cat_id'].'">'.$sub_mark.$row['cat_name'].'</option>';
                $this->categoryTree($row['cat_id'], $sub_mark.'--- ', '');
            }
        }
    }

The above code is working properly when we add Category, the problem is that when we edit the category. How to make the category selected while editing?

Below is the screenshot of the requirement:

enter image description here

When you edit page you will have the current object to edit.

current_object = get_current_object_somewhere();
selected_category_id = current_object->category_id;

So you have the selected_category_id you need to highlight when editing, now just need to mark it as selected in your select options:

if($rowCount > 0){
            while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
                selected = selected_category_id === $row['cat_id'] ? ' selected' : '';

                echo "<option{selected} value='{$row['cat_id']}'>{$sub_mark}{$row['cat_name']}</option>";
                $this->categoryTree($row['cat_id'], $sub_mark.'--- ', '');
            }
        }