在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?

我有一个存储食物层次结构的数据库。 p>

 类别(id_cat,name_cat); 
his_low_cat(id_cat,id_low_cat); 
  code>  pre> 
 
 

类别可以具有0..n低类别。 如果没有较低的类别,我在 his_low_cat code>中执行 id_cat,-1 code>字段。 p>

我不知道是否可能但是 我想在一种“下拉菜单”中显示它 (如果你对如何显示完整的层次结构有任何其他想法请建议) p>

像这样: p>

  echo“&lt; div id = \”menu \“&gt; 
&lt; ul class = \”niveau1 \“&gt; 
&lt; li class = \”sousmenu  \“&gt;&lt; a href = \”Food \“&gt;食物&lt; / a&gt; 
&lt; ul class = \”niveau2 \“&gt; 
&lt; li class = \”sousmenu \“&gt;&lt;  ; a href = \“Sous menu 1.1 \”&gt; Sous menu 1.1&lt; / a&gt; 
&lt; ul class = \“niveau3 \”&gt; 
&lt; li&gt;&lt; a href = \“Sous sous 菜单1.1.1 \“&gt; Sous sous菜单1.1.1&lt; / a&gt;&lt; / li&gt; 
&lt; / ul&gt; 
&lt; / li&gt; 
&lt; li&gt;&lt; a href = \”  Sous menu 1.2 \“&gt; Sous menu 1.2&lt; / a&gt;&lt; / li&gt; 
&lt; / ul&gt; 
&lt; / li&gt; 
&lt; / ul&gt; 
&lt; / div&gt;”; \  n  code>  pre> 
 
 

我的第一个 猫是“食物”,然后它衍生出4个较低的类别,它们来自更多。 p>

问题是它必须是动态的并且从我的数据库加载字段。 目标是能够捕获点击的值并在另一个中使用它.php p>

我该怎么做? p> div>

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).'">'."
";
         if(is_array($element))
         {
               echo str_repeat("\t", $depth+1)."<li class=\"sousmenu\"><a href=\"${elementName}\">${elementName}</a>
";
               nestElements($element, $depth+2);
               echo str_repeat("\t", $depth+1)."</li>
";
         }
         else
         {
               echo str_repeat("\t", $depth+1)."<li class=\"sousmenu\"><a href=\"${element}\">${elementName}</a></li>
";
         }
         echo str_repeat("\t", $depth)."</ul>
";
    }
}
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.