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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:02:01+00:00 2026-06-13T10:02:01+00:00

I am currently making an product page. I need a selectbox displaying multilevels of

  • 0

I am currently making an product page. I need a selectbox displaying multilevels
of categories where to add the product in. Need something like (php/mysql dynamic):

<option>Workstations</option>
<option>--Macs</option>
<option>--Windows</option>
<option>Software</option>
<option>--Image editing</option>
<option>----Pro</option>
<option>----Semi pro</option>

My mysql fields are, cat_id, cat_name, cat_parent.
I have the following code, so is there any way to customize it to work with a selectbox insted of ul/li?

function display_children($parent, $level) {
    $result = mysql_query("SELECT a.id, a.label, a.link, Deriv1.Count FROM `menu` a  LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent=" . $parent);
    echo "<ul>";
    while ($row = mysql_fetch_assoc($result)) {
        if ($row['Count'] > 0) {
            echo "<li><a href='" . $row['link'] . "'>" . $row['label'] . "</a>";
            display_children($row['id'], $level + 1);
            echo "</li>";
        } elseif ($row['Count']==0) {
            echo "<li><a href='" . $row['link'] . "'>" . $row['label'] . "</a></li>";
        } else;
    }
    echo "</ul>";
}

display_children(0, 1);

Regards, Simon

  • 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-13T10:02:02+00:00Added an answer on June 13, 2026 at 10:02 am

    Something like this would do the trick:

    function display_children($parent, $level) {
        $result = mysql_query("SELECT a.id, a.label, a.link, Deriv1.Count FROM `menu` a  LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `menu` GROUP BY parent) Deriv1 ON a.id = Deriv1.parent WHERE a.parent=" . $parent);
    
        $indent = str_repeat('-', $level);
        $tpl = '<option value="%s">%s</option>';
    
        while ($row = mysql_fetch_assoc($result)) {
    
            echo sprintf($tpl, $row['link'], $indent . $row['label']);
    
            if ($row['Count'] > 0) {
                display_children($row['id'], $level + 1);
            }
        }
    }
    

    I think you could also simplify your query slightly

        $result = mysql_query("SELECT a.id, a.label, a.link, (SELECT COUNT(id) FROM `menu` WHERE parent = a.id) AS Count FROM `menu` a  WHERE a.parent=" . $parent);
    

    Reworked example

    The following would allow you to complete the whole process with 1 query. It uses PHP 5.3 due to closures.

    // I assume this is how your data comes out of MySQL
    $items = array(
        array('id' => 1, 'label' => 'Item One',  'link' => 'Link One',    'parent' => 0),
        array('id' => 2, 'label' => 'Child One', 'link' => 'Link Two',    'parent' => 1),
        array('id' => 3, 'label' => 'Child Two', 'link' => 'Link Three',  'parent' => 1),
        array('id' => 4, 'label' => 'Item Two',  'link' => 'Link Four',   'parent' => 0),
        array('id' => 5, 'label' => 'Child One',  'link' => 'Link Five',  'parent' => 4),
        array('id' => 6, 'label' => 'Child Two',  'link' => 'Link Six',   'parent' => 4),
        array('id' => 7, 'label' => 'Child Three',  'link' => 'Link Six',   'parent' => 6),
        array('id' => 8, 'label' => 'Child Four',  'link' => 'Link Six',   'parent' => 6),
    );
    
    // Loop using references to build a tree
    $childs = array();
    foreach($items as &$item)
    {
        $childs[$item['parent']][] = &$item;
    }
    
    unset($item);
    
    foreach($items as &$item)
    {
        if(isset($childs[$item['id']]))
        {
            $item['children'] = $childs[$item['id']];
        }
    }
    // We now have a tree with 'children' key set on each node that has children
    $tree = $childs[0];
    
    // Prepare a template and recursive closure (note reference on &$print)
    $tpl = '<option value="%s">%s %s</option>';
    $print = function($item, $indent = '') use (&$print, $tpl)
    {
        echo sprintf($tpl, $item['link'], $indent, $item['label']) . "\n";
    
        if(isset($item['children']))
        {
            foreach($item['children'] as $child)
            {
                $print($child, $indent . '-');
            }
        }
    };
    
    echo '<select onchange="showProduct(this.value);">';
    // Call the function for each top-level node
    foreach($tree as $row)
    {
        $print($row);
    }
    echo '</select>';
    
    /*
    <select onchange="showProduct(this.value);">
    <option value="Link One"> Item One</option>
    <option value="Link Two">- Child One</option>
    <option value="Link Three">- Child Two</option>
    <option value="Link Four"> Item Two</option>
    <option value="Link Five">- Child One</option>
    <option value="Link Six">- Child Two</option>
    <option value="Link Six">-- Child Three</option>
    <option value="Link Six">-- Child Four</option>
    </select>
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently making a page with loads of products on, which need to be
I'm making an administration page for a website. Currently, it allows a user to
I am currently making a lot of use of code like this in order
I'm currently designing an application using PHP and MySQL, built on the Kohana framework.
Currently making a global volunteer site, and the webpages will more or less have
I'm currently making changes to an older version of my app for customers still
I am currently making a program which is supposed to prompt the user to
I'm currently making an application with Backbone.js and some JQuery libraries. Today I work
I am currently making a program that will send an email if the date
so i am currently making a game (a huge one over 3500 lines of

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.