Q:

Show a hierarchy in PHP

I have a database which stores a hierarchy of foods.
Category(id_cat,name_cat);
his_low_cat(id_cat,id_low_cat);

A category can have 0..n low category. If it had no lower category I do a id_cat,-1 field in his_low_cat.
I do not know if it's possible but I would like to show it in a kind of "pulldown menu"
(if you have any other idea on how to show a full hierarchy please suggest it)
Like this :
echo " <div id=\"menu\"> 
   <ul class=\"niveau1\"> 
      <li class=\"sousmenu\"><a href=\"Food\">Food</a> 
         <ul class=\"niveau2\"> 
            <li class=\"sousmenu\"><a href=\"Sous menu 1.1\">Sous menu 1.1</a> 
               <ul class=\"niveau3\"> 
                  <li><a href=\"Sous sous menu 1.1.1\">Sous sous menu 1.1.1</a></li>  
               </ul> 
            </li> 
            <li><a href=\"Sous menu 1.2\">Sous menu 1.2</a></li> 
         </ul> 
      </li> 
   </ul> 
</div>";

My first cat is "food" and then it derives into 4 lowers categories, which derive themselves in more. 
The problem is that it must be dynamic and load field from my database.
The goal would be to be able to catch the clicked value and use it in another .php
How would I do this?

A:

Recursion is definitely the way to go with this problem, I've coded up this solution:
<?php
function nestElements($elements, $depth=0)
{
    foreach($elements as $elementName=>$element)
    {
         echo str_repeat("\t", $depth).'<ul class="niveau'.($depth+1).'">'."\n";
         if(is_array($element))
         {
               echo str_repeat("\t", $depth+1)."<li class=\"sousmenu\"><a href=\"${elementName}\">${elementName}</a>\n";
               nestElements($element, $depth+2);
               echo str_repeat("\t", $depth+1)."</li>\n";
         }
         else
         {
               echo str_repeat("\t", $depth+1)."<li class=\"sousmenu\"><a href=\"${element}\">${elementName}</a></li>\n";
         }
         echo str_repeat("\t", $depth)."</ul>\n";
    }
}
nestElements(array("Food"=>array("Meat"=>array("Poultry"=>array("Chicken"=>"Meat/Poultry/Chicken"), "Beef"=>array("Hamburgers"=>"Meat/Beef/Hamburgers", "Steak"=>"Meat/Beef/Steak")), "Dairy"=>array("Cow"=>"Dairy/Cow", "Sheep"=>"Dairy/Sheep")), "name"=>"url"));
?>

Testing with this:
<?php
nestElements(array("Food"=>array("Meat"=>array("Poultry"=>array("Chicken"=>"Meat/Poultry/Chicken"), "Beef"=>array("Hamburgers"=>"Meat/Beef/Hamburgers", "Steak"=>"Meat/Beef/Steak")), "Dairy"=>array("Cow"=>"Dairy/Cow", "Sheep"=>"Dairy/Sheep")), "name"=>"url"));
?>

Results in:
<ul class="niveau1">
    <li class="sousmenu"><a href="Food">Food</a></li>
    <ul class="niveau2">
            <li class="sousmenu"><a href="Meat">Meat</a></li>
            <ul class="niveau3">
                    <li class="sousmenu"><a href="Poultry">Poultry</a></li>
                    <ul class="niveau4">
                            <li class="sousmenu"><a href="Meat/Poultry/Chicken">Chicken</a></li>
                    </ul>
            </ul>
            <ul class="niveau3">
                    <li class="sousmenu"><a href="Beef">Beef</a></li>
                    <ul class="niveau4">
                            <li class="sousmenu"><a href="Meat/Beef/Hamburgers">Hamburgers</a></li>
                    </ul>
                    <ul class="niveau4">
                            <li class="sousmenu"><a href="Meat/Beef/Steak">Steak</a></li>
                    </ul>
            </ul>
    </ul>
    <ul class="niveau2">
            <li class="sousmenu"><a href="Dairy">Dairy</a></li>
            <ul class="niveau3">
                    <li class="sousmenu"><a href="Dairy/Cow">Cow</a></li>
            </ul>
            <ul class="niveau3">
                    <li class="sousmenu"><a href="Dairy/Sheep">Sheep</a></li>
            </ul>
    </ul>
</ul>
<ul class="niveau1">
    <li class="sousmenu"><a href="url">name</a></li>
</ul>

To parse it you'd have to make a mod_rewrite which redirects to index.php?r=TheURL and from their, explode the r parameter using "/" as the delimeter, then you have a list of menus and submenus that the clicked link was from.  By adding another parameter the url coul be automatically generated.
Edit: Fixed problem with original code output seen below
<li class="sousmenu"><a href="Sheep">Sheep</a></li>
<li class="sousmenu"><a href="Dairy/Sheep">Sheep</a></li>

To generate the array:
<?php
function genArray(&$targetArray, $parentID=null){
    $res=(is_null($parentID))?mysql_query("SELECT * FROM categorie WHERE id_cat NOT IN (SELECT id_low_cat FROM hislowcat) ORDER BY id_cat DESC;"):mysql_query("SELECT *, (SELECT name_cat FROM categorie WHERE id_cat= '".$parentID ."') AS name_cat FROM hislowcat WHERE id_cat= '" .$parentID ."'");
    if(!is_null($parentID) && !mysql_num_rows($res))
    {
        $res3=mysql_query("SELECT name_cat FROM categorie WHERE id_cat='${parentID}';");
        $row3=mysql_fetch_array($res3);
        $targetArray[$row3['name_cat']]=$row3['name_cat'];
        return;
    }
    while(($row=mysql_fetch_array($res)))
    {
        //echo $row->name_cat;
        if(is_null($parentID))
        {
             if(!isset($targetArray[$row['name_cat']]))
             {
                   $targetArray[$row['name_cat']]=array();
             }
             genArray($targetArray[$row['name_cat']], $row['id_cat']);
        }
        else
        {
             genArray($targetArray[$row['name_cat']], $row['id_low_cat']);
        }
    }
}
$array=array();
genArray($array);
print_r($array);
?>

Notice how $targetArray is set up as a reference, this way we can treat it one-dimensionally.

