Friday, 15 April 2011

Show a hierarchy in PHP -



Show a hierarchy in PHP -

i have database stores 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 had no lower category id_cat,-1 field in his_low_cat.

i not know if it's possible show in kind of "pulldown menu" (if have other thought on how show total hierarchy please suggest it)

like :

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 "food" , derives 4 lowers categories, derive in more.

the problem must dynamic , load field database. goal able grab clicked value , utilize in .php

how this?

recursion way go problem, i've coded solution:

<?php function nestelements($elements, $depth=0) { foreach($elements $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 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 you'd have create mod_rewrite redirects index.php?r=theurl , their, explode r parameter using "/" delimeter, have list of menus , submenus clicked link from. adding parameter url coul automatically generated.

edit: fixed problem 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 array:

<?php function genarray(&$targetarray, $parentid=null){ $res=(is_null($parentid))?mysql_query("select * categorie id_cat not in (select id_low_cat hislowcat) order id_cat desc;"):mysql_query("select *, (select name_cat categorie id_cat= '".$parentid ."') name_cat hislowcat id_cat= '" .$parentid ."'"); if(!is_null($parentid) && !mysql_num_rows($res)) { $res3=mysql_query("select name_cat categorie 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 set reference, way can treat one-dimensionally.

php hierarchy

No comments:

Post a Comment