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 504019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:27:13+00:00 2026-05-13T06:27:13+00:00

I want to pull out menu items from MySQL. Main menu id=1, parentid=0 -Contact

  • 0

I want to pull out menu items from MySQL.

Main menu           id=1, parentid=0
-Contact us         id=2, parentid=1
-Music              id=3, parentid=1
 --Rock             id=8, parentid=3
 --Classic          id=9, parentid=3
-Car                id=4, parentid=1
  --Toyota          id=5, parentid=4,
  --Ford            id=6, parentid=4,
  --Honda           id=7, parentid=4

Other menu          id=10, parentid=0
-Othermain          id=11, parentid=10
  --submenu         id=12, parentid=11

etc.

I can pullout data from id=1 to 4 and display by “…where parentid=1” etc.
However this pulls out only the top level.

But I want to pullout all the data including submenu for each menu(main menu) as well.

Could anyone tell me how to write a query in MySQL for this please?

Thanks in advance.

  • 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-13T06:27:13+00:00Added an answer on May 13, 2026 at 6:27 am

    You need to implement recursion to make repeated calls to the database to retrieve all children. You will have to replace my database abstraction layer implementation with your own but the concept is the same.

    memcache solution

    function generateTree($parentid = 0, &$tree) {
        $sql = sprintf('SELECT * FROM navigation WHERE parentid = %d', $parentid);
        $res = $this->db->results($sql);
        if ($res) {
            foreach ($res as $r) {
                // push found result onto existing tree
                $tree[$r->id] = $r;
                // create placeholder for children
                $tree[$r->id]['children'] = array();
                // find any children of currently found child
                $tree = generateTree($r->id, $tree[$r->id]['children']);
            }
        }
    }
    
    function getTree($parentid) {
        // memcache implementation
        $memcache = new Memcache();
        $memcache->connect('localhost', 11211) or die ("Could not connect"); 
        $tree = $memcache->get('navigation' . $parentid);
        if ($tree == null) {
            // need to query for tree
            $tree = array();
            generateTree($parentid, $tree);
    
            // store in memcache for an hour
            $memcache->set('navigation' . $parentid, $result, 0, 3600);
        }
        return $tree;
    }
    
    // get tree with parentid = 0
    getTree(0);
    

    non memcache solution

    function generateTree($parentid = 0, &$tree) {
        $sql = sprintf('SELECT * FROM navigation WHERE parentid = %d', $parentid);
        $res = $this->db->results($sql);
        if ($res) {
            foreach ($res as $r) {
                // push found result onto existing tree
                $tree[$r->id] = $r;
                // create placeholder for children
                $tree[$r->id]['children'] = array();
                // find any children of currently found child
                $tree = generateTree($r->id, $tree[$r->id]['children']);
            }
        }
    }
    
    // get tree with parentid = 0
    $tree = array();
    $parentid = 0;
    generateTree($parentid, $tree);
    
    // output the results of your tree
    var_dump($tree); die;
    

    The above is untested so if anybody catches an error please let me know or feel free to update.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to pull out a string from an array by index. e.g array
I want to pull out duplicate records in a MySQL Database. This can be
I want to pull a series of relationships out of xml files and transform
I am looking for an efficient way to pull the data I want out
I want to pull a number out the middle of a string in JavaScript
I want to pull out the records that have a date that is LIKE
I'm trying to create a filter in Jira. I want to pull out all
I want to pull out the user block. The JSON result will always change,
I want to pull out capitalized words that don't start a sentence along with
Hi I have the following html and want to pull out all the other

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.