如何在下拉菜单中仅显示具有相同subpage_id的元素?

I have sub-pages and sub-sub-pages in my dropdown menu and I want to show sub-sub-pages under the title of the sub-page that has the same subpage_id as the sub-sub-page.

Right now, all sub-sub-pages are shown at the bottom of the dropdown, beneath all the sub-pages. I need each sub-sub-page to be exactly under the parent sub-page..

My SQL tables:

'pages' => page_id, subpage_id, page_name; 'subpages' => subpage_id, page_id, subpage_name; 'subsubpages' => subsubpage_id, subpage_id, page_id, subsubpage_name;

Sub-sub-pages get same subpage_id as the parent sub-page, when I insert them via my form.

My form:

<?php
    $page = new Page;
    $childpage = new Childpage;
    $subchildpage = new Subchildpage;

    $pages = $page->fetch_all();
    $childpages = $childpage->fetch_all();
    $subchildpages = $subchildpage->fetch_all();
?>


<div class="sidenav">

<?php foreach ($pages as $page) { 
    $childpages = Childpage::fetch_all_page($page['page_id']);
    $subchildpages = Subchildpage::fetch_all_page($page['page_id']);           
        if (count($childpages)): ?>

    <button class="dropdown-btn">

      <?php foreach ($subchildpages as $subchildpage) {
        if($currentSubpageId == $subchildpage['subsubpage_id'] && $subchildpage['page_id'] == $page['page_id']) {
            echo('id="active" '); } } ?> >
        <?php echo $page['page_name']; ?>

    </button>

    <?php else : ?>
        <a href="#"> <?php echo $page['page_name']; ?> </a>
    <?php  endif; ?>     

    <div class="dropdown-container">
      <?php foreach ($childpages as $childpage) { ?>
        <a href="#"> <?php echo $childpage['subpage_name']; ?> </a>
      <?php } ?>

// PROBLEM DOWN HERE

  <?php foreach ($subchildpages as $subchildpage) { ?>
      <div class="dropdown2">        
        <a href="#">- <?php echo $subchildpage['subsubpage_name']; ?> </a>
      </div>
<?php } ?>

  </div>
<?php } ?>
</div>

My classes (Page, Childpage, Subchildpage):

class Page {
    public function fetch_all(){
        global $pdo;

        $query = $pdo->prepare("SELECT * FROM pages");
        $query->execute();

        return $query->fetchAll();
    }    
    public function fetch_data($page_id) {
        global $pdo;

        $query = $pdo->prepare("SELECT * FROM pages WHERE page_id = ?");
        $query->bindValue(1, $page_id);
        $query->execute();

        return $query->fetch();
    }
}

class Childpage {
    public function fetch_all(){
        global $pdo;

        $query = $pdo->prepare("SELECT * FROM subpages");
        $query->execute();

        return $query->fetchAll();
        }
    public static function fetch_all_page($page_id){
        global $pdo;

        $query = $pdo->prepare("SELECT * FROM subpages WHERE page_id = ?");
        $query->bindValue(1, $page_id);
        $query->execute();

        return $query->fetchAll();
    }
}

class Subchildpage {
    public function fetch_all(){
        global $pdo;

        $query = $pdo->prepare("SELECT * FROM subsubpages");
        $query->execute();

        return $query->fetchAll();
    }
    public static function fetch_all_page($page_id){
        global $pdo;

        $query = $pdo->prepare("SELECT * FROM subsubpages WHERE page_id = ?");
        $query->bindValue(1, $page_id);
        $query->execute();

        return $query->fetchAll();
    }
}

Summary: Right now, all sub-sub-pages are shown at the bottom of the dropdown, beneath all the sub-pages. I need each sub-sub-page to be exactly under the parent sub-page..

I know the code might be a little long, but if anyone has time to read it, any help would be appreciated!