1

i want to have unlimited menu and submenu and show them like tree as shown at the bottom

My mysql table structure

id           //menu ID
title        //menu title
parentID     //menu parent ID

My PHP code

<select id="sub" name="sub" class="select" style="width:200px;">
<option value="">none</option>
<?
//----------------Get Menu Function---------------
function getMenu($parentID=0)
{
    $selectMenus = mysql_query("SELECT * FROM menus WHERE parentID='$parentID'") OR DIE(mysql_error());
    if(mysql_num_rows($selectMenus) != 0)
    {
        while ($rowMenus = mysql_fetch_array($selectMenus))
        {
            if ($rowMenus['parentID'] != 0)
            {
                $tab .= "--";
            }
            else
            {
                $tab = "";
            }
            echo "<option value='" . $rowMenus['id'] . "|" . $rowMenus['title'] . "'>" . $tab . $rowMenus['title'] . "</option>";
            $menus .= getMenu($rowMenus['id']);
        }
    }
    return $menus;
}
//-----------------------------------------------
echo getMenu();
?>
</select>

I want to add 2 dash in first of submenus of each menu and show them in option of a select box like this

Mainpage
Products
--Granite
----GreenGranite
----BrownGranite
--travertine
AboutUs

Whats wrong with my code?

  • what output you getting? – suresh gopal Dec 22 '12 at 15:04
  • Answer your question: [http://stackoverflow.com/questions/10646833/using-mysql-query-to-traverse-rows-to-make-a-recursive-tree][1] [1]: http://stackoverflow.com/questions/10646833/using-mysql-query-to-traverse-rows-to-make-a-recursive-tree – sub Dec 22 '12 at 16:00

0 Answers0