Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7579189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:36:54+00:00 2026-05-30T17:36:54+00:00

Here im building a tree menu. A category is accessed by its id and

  • 0

Here im building a tree menu. A category is accessed by its id and its child displayed.
my function works fine for two level menu but fails to generate level third. Third level menu should be displayed only if id of a second level item clicked. I need this function to track the path to node too. That if a third level id is given than tree should be expanded to third level. i.e. parent of the child and if that parent has a parent too.
mySQL Table

+-------+-----------+------------+
|  id   |  title    | parent_id  |
+-------+-----------+------------+
|   1   | Computers |    NULL    |
+-------+-----------+------------+
|   2   | Dell      |     1      |
+-------+-----------+------------+
|   3   | Laptops   |     2      |
+-------+-----------+------------+
|   4   | Desktops  |     2      |
+-------+-----------+------------+
|   5   | HP        |     1      |
+-------+-----------+------------+

PHP code

<?php
 $sql = "SELECT * FROM category";
 $statement= $db->prepare($sql);
 $statement->execute();
 $categories = array();
 $rootCategories = array();
 while ( $row = $statement->fetchObject() ) {
        $row->childs = array();
        $categories[$row->id] = $row;
        if(empty($row->parent_id)) {
                $rootCategories[$row->id] = $categories[$row->id];
        } else {
                $categories[$row->parent_id]->childs[] = $categories[$row->id];
        }
 }

  function rederTreeById($records, $id=false) {
        echo '<ul>';
        foreach($records as $record) {
                if($id == $record->id) {
                        echo '<li>'.$record->title;
                        if(!empty($record->childs)) {
                                rederTreeById($record->childs);
                        }
                        echo '</li>';
                } else {
                        echo '<li>'.$record->title.'</li>';
                }
        }
        echo '</ul>';
 }


 rederTreeById($rootCategories, 1);
?>

For example
if clicked Computers then

  • Computer
    • Dell
    • HP

if Dell s clicked then

  • Computer
    • Dell
      • Laptops
      • Desktops
    • HP
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:36:55+00:00Added an answer on May 30, 2026 at 5:36 pm

    I see one problem with your solution. When you check for ID if($id == $record->id) you will only match the current level in the tree. i.e. selecting Dell with id=2 will not match the first iteration so your function wil not traverse to next level.

    You need to keep track of the path to your selected menu.

    In your case. When you select Dell you will only se “Computer”, am I right?

    How about something like this:

    ...
      function rederTreeById($records, $path) {
            echo '<ul>';
            foreach($records as $record) {
                    if(in_array($record->id, $path)) {
                            echo '<li>'.$record->title;
                            if(!empty($record->childs)) {
                                    rederTreeById($record->childs, $path);
                            }
                            echo '</li>';
                    } else {
                            echo '<li>'.$record->title.'</li>';
                    }
            }
            echo '</ul>';
     }
    
     function getPath($id) {
        $path = array();
        $current=$id;
        $path[] = 1
        while(!is_null($categories[$current]->parent_id)) {
            $current=$categories[$current]->parent_id
            $path[] = $current;
        }
        return $path;
     }
    
    
    $selectedId = 1;
    
    
     rederTreeById($rootCategories, getPath($selectedId));
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So here's the scenario, I'm building a theme that would display sub category of
We see a size difference here when building our application in VC 2008 Express
I'm building an excel template (*.xlt) for a user here, and one of the
I'm building a python application from some source code I've found Here I've managed
fairly new iPhone developer here. Building an app to send RS232 commands to a
I'm building a custom autoloader based on Zend Framework's autoloading (related question here ).
I'm looking for some opinions here, I'm building a web application which has the
I'm building nested tree and I need to get data for the next row
I have written an ANT script and finally am building the jar here is
I seem to have a probably newbie problem but here it is. How do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.