0

I have navigation folders structure stored in mysql:

navigation structure

I have this navigation:

enter image description here

Now I need to make automatic detect parent's folders and create navigation information bar for all levels parent-folders but I dont know how, I have only first level parent.

enter image description here

How I can create loop and stop automatically when parent is 0?

Here is my function:

    public static function Gallery($fid = NULL){

    $folder_id  =   $fid ? $fid : 1;
    echo "<div class=\"gallery-c\">";
    echo "<div class=\"gallery-path--c\">";

    $current_path_obj       =   Database::dbquery("SELECT * FROM calendar_gallery_folders WHERE id = $fid", false, true)['result'][0];
    $current_path_name      =   $current_path_obj->folder;
    $current_path_parent    =   $current_path_obj->parent;
    $parent_path_name       =   Database::dbquery("SELECT folder FROM calendar_gallery_folders WHERE id = $current_path_parent", false, true)['result'][0]->folder;

    // Navigation information bar
    echo $parent_path_name."/".$current_path_name;

    echo "</div>";
    echo "<div class=\"gallery-folder--c\">";
    // Folders
    foreach(Database::dbquery("SELECT * FROM calendar_gallery_folders WHERE parent = $folder_id ORDER BY id", false, true)['result'] as $folder){
        echo "<a class=\"js-gallery-folderlink gallery-folder--box\" data-folder=\"$folder->id\">
        <div class=\"gallery-folder--icon\"><span class=\"icon material-symbols-outlined\">folder</span></div>
        <div class=\"gallery-folder--text\">$folder->folder</div></a>";
    }

    echo "</div>";

    echo "</div>";
}

1 Answer 1

1

You need to get all the folders in an array and do recursive looping.

public static function Gallery($fid = NULL){

    $folder_id  =   $fid ? $fid : 1;
    echo "<div class=\"gallery-c\">";
    echo "<div class=\"gallery-path--c\">";

    $all_folders       =   Database::dbquery("SELECT * FROM calendar_gallery_folders", false, true)['result'];
    //Map result with Key ID
    $folders_map = [];
    foreach($all_folders as $folder){
        $folders_map[$folder->id]=$folder;
    }
    //Recrusive Loop function to get the trail
    $navigation = getTrail($folders_map,$fid);

    //Reverse the result 
    $navigation = array_reverse($navigation);
    //Print the navigation
    echo implode('/',$navigation)
    

    echo "</div>";

    echo "</div>";
}
function getTrail($folder_map,$folder_id,$trail=[]){
    if(isset($folder_map[$folder_id])){
        array_push($trail,$folder_map[$folder_id]->folder);
    }else{
        return [];
    }

    if($folder_map[$folder_id]->parent!=0){
        $trail = $this->getTrail($folder_map,$folder_map[$folder_id]->parent,$trail);
    }
    return $trail;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.