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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:24:12+00:00 2026-06-04T15:24:12+00:00

Problem: I can’t think of way make a recursion function to my specific situation.

  • 0

Problem: I can’t think of way make a recursion function to my specific situation.

Situation:

Mysql DB
id | root | name |
Where root shows to witch category this is subcategory.

How should HTML look:

    <li><a href="#"><p class="Tier0">Datori</p></a>
                    <ul style="display: block">
                         <li><a href="#"><p class="Tier1">Cookies</p></a></li>
                         <li><a href="#"><p class="Tier1">Events</p></a></li>
                         <li><a href="#"><p class="Tier1">Forms</p></a></li>
                         <li><a href="#"><p class="Tier1">Games</p></a></li>
                         <li><a href="#"><p class="Tier1">Images</p></a>
                            <ul>
                                <li><a href="#"><p class="Tier2">CSS</p></a></li>
                                <li><a href="#"><p class="Tier2">JavaScript</p></a></li>
                                <li><a href="#"><p class="Tier2">JQuery</p></a></li>
                            </ul>
                         </li>
                         <li><a href="#"><p class="Tier1">Navigations</p></a>
                            <ul>
                                <li><a href="#"><p class="Tier2">CSS</p></a></li>
                                <li><a href="#"><p class="Tier2">JavaScript</p></a></li>
                                <li><a href="#"><p class="Tier2">JQuery</p></a></li>
                            </ul>
                        </li>
                         <li><a href="#"><p class="Tier1">Tabs</p></a></li>
                    </ul>
                </li>
                <li><a href="#"><p class="Tier0">Washing Machines</p></a>

What kind of PHP function I would need to print it all out?

  • 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-06-04T15:24:14+00:00Added an answer on June 4, 2026 at 3:24 pm

    How about:

    function recurse($categories, $parent = null, $level = 0)
    {
        $ret = '<ul>';
        foreach($categories as $index => $category)
        {
            if($category['root'] == $parent)
            {
                $ret .= '<li><a href="#"><p class="Tier' . $level . '">' . $category['name'] . '</p></a>';
                $ret .= $this->recurse($categories, $category['id'], $level+1);
                $ret .= '</li>';
            }
        }
        return $ret . '</ul>';
    }
    

    This function requires that you first query your database for the entire list of available categories and assumes that your root categories have a value of null, but the function can be changed to accept -1 or 0 depending on how your current schema works.

    $categories = { get from database into an multi-dimensional array };
    $Tree = $this->recurse($categories);
    echo $Tree;
    

    You may consider doing the following to prevent any empty UL’s appearing when no children exist for the parent:

    function recurse($categories, $parent = null, $level = 0)
    {
        $ret = '<ul>';
        foreach($categories as $index => $category)
        {
            if($category['root'] == $parent)
            {
                $ret .= '<li><a href="#"><p class="Tier' . $level . '">' . $category['name'] . '</p></a>';
                $sub = $this->recurse($categories, $category['id'], $level+1);
                if($sub != '<ul></ul>')
                    $ret .= $sub;
                $ret .= '</li>';
            }
        }
        return $ret . '</ul>';
    }
    

    However, the best solution, would be to select your data to include a column containing how many child categories each category has.

    select Category.*, (select count(distinct c1.id) from Category as c1 where c1.root = Category.id) as ChildCount from Category
    

    In which your function would be:

    function recurse($categories, $parent = null, $level = 0)
    {
        $ret = '<ul>';
        foreach($categories as $index => $category)
        {
            if($category['root'] == $parent)
            {
                $ret .= '<li><a href="#"><p class="Tier' . $level . '">' . $category['name'] . '</p></a>';
                if($category['ChildCount'] > 0)
                    $ret .= $this->recurse($categories, $category['id'], $level+1);
                $ret .= '</li>';
            }
        }
        return $ret . '</ul>';
    }
    

    Hope that helps?

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

Sidebar

Related Questions

First, I'll show the specific problem I'm having, but I think the problem can
I think that this problem can be sorted using reflection (a technology which I'm
Hello! My problem can be described the following way: I have some data which
Got this really stupid problem: Can find the proper name for the android folder
I am just facing a problem,can not able to overcome(make be lack of proper
problem can be seen here: http://www.studioimbrue.com/beta The code: $(document).ready(function(){ $('div.caption').hide(); $('.captions ul li img').hover(function(){
I wonder if the objective function of a general dynamic programming problem can always
Though I use CouchDB-specific JQuery verison, the problem can appear to be not related
My problem can be summed up by making this simple command works : nice
Seems like the slow Tomcat 7 startup problem can be resolved with metadata-complete set

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.