Greetings PHP guru's!
It's been a while since I bothered you, but I have a problem that bothered me for a week and since I couldn't solve it on my own I need your wisdom and guidance with it...
It was a test I took when I applied for a web developer job in a firm in my home town and I since I never did anything like it I spent the whole week trying to solve it and came close but never quite finished it...
Here goes:
I got an array that I am to convert it in a navigation menu using whatever means I have at my disposal (PHP, then HTML, and CSS)...
here's the array:
$navigation = array(
array('id'=>'1', 'name'=>'Tel', 'parent'=>'0'),
array('id'=>'2', 'name'=>'Cam', 'parent'=>'0'),
array('id'=>'3', 'name'=>'DVD', 'parent'=>'0'),
array('id'=>'101', 'name'=>'LCD', 'parent'=>'1'),
array('id'=>'102', 'name'=>'Plasma', 'parent'=>'1'),
array('id'=>'103', 'name'=>'MiniDV', 'parent'=>'2'),
array('id'=>'104', 'name'=>'DVD Camcorders', 'parent'=>'2'),
array('id'=>'105', 'name'=>'Hoome theaters', 'parent'=>'3'),
array('id'=>'1001', 'name'=>'20LC1RB-ZG', 'parent'=>'101'),
array('id'=>'1002', 'name'=>'RZ-20LS3R-ZA', 'parent'=>'101'),
array('id'=>'1003', 'name'=>'TX-20LA70P', 'parent'=>'101'),
array('id'=>'1100', 'name'=>'TH-42PV80P', 'parent'=>'102'),
array('id'=>'1301', 'name'=>'NV-GS37EP-S', 'parent'=>'103'),
array('id'=>'1302', 'name'=>'NV-GS80EP-S', 'parent'=>'103'),
array('id'=>'1401', 'name'=>'DCR-DVD106E', 'parent'=>'104'),
array('id'=>'1402', 'name'=>'VDR-D310EP-S', 'parent'=>'104'),
array('id'=>'1501', 'name'=>'SC-PT250E-S', 'parent'=>'105')
);
Sound's simple doesn't it? Well, the trouble is - I have to make it so that ti would first list only top level nodes that I like to call categories (those that have parent = 0) and when a user is to click on any of them only it's subcategories are to be shown on a page and then each subcategory can also be clicked on to show it's children.
Basically, from an array that has something like this
id name parent
------------------------------------------------------
1 Sports 0
2 TV program 0
3 Football 1
4 Basketball 1
5 EURO 2008 3
6 WorldCup 2010 3
I am to create a menu that would look like this:
- Sports
- TV Program
And then if I am to click Sports it should show me this:
-Sports
-Football
-Basketball
-TV Program
And if I am to click Football I would get this:
-Sports
-Football
-Euro 2008
-WorldCup 2010
-Basketball
-TV Program
Now, what I managed to do is to get subcategories to show when I click on a category - which is first step, cool. But when I click on a subcategory that is upper in list i.e. Football in my example I get Euro and WorldCup sub-subcategories, but Basketball won't show... Again, if I am to click on a Basketball, it's subcategories will show and the Football would show as well, but only in this case it would work... 🙁
Now, my code is this:
$crumb = array();
$nav = array();
function neez($array,$eed){
global $crumb;
global $nav;
foreach($array as $value){
if($value['parent']==$eed){
$nav[$value['parent']][$value['id']] = "$value[name]";
$crumb[$value['id']][$eed] = $value['name'];
neez($array,$value['id']);
}
}
return $nav;
}
$elem = array();
function find_children($eid){//
global $navigation;
global $iid;
global $elem;
global $id;
while(list($k,$v) = each($navigation)){
if($v['parent']==$eid){
if(in_array($v['id'],$iid)){
$elem[$k] = $v;
if($v['id']==$id){
find_children($v['id']);
}
}
else{
$elem[$k] = $v;
}
}
}
}
$arr = array();
$iid = array();
function find_crumb($array,$eed){
global $arr;
global $iid;
while(list($k,$v) = each($array)){
if(is_array($v)){
$nw = (list($key,$value) = each($v));
if($eed==$k){
$arr[$k] = $value;
$iid[$k] = $k;
find_crumb($array,$key);
}
}
}
}
And then, on a display end I have this:
neez($navigation,0);
krsort($crumb);
find_crumb($crumb,$id);
ksort($arr);
$path = "<a href=\"$page\" class=\"admin\">Home</a>";
while(list($k,$v) = each($arr)){
$path .= "/";
if($k == $id){
$path .= "$v";
}
else{
$path .= "<a href=\"$page?id=$k\" class=\"admin\">$v</a>";
}
}
echo $path."<br />\n<br />\n";
reset($navigation);
foreach($navigation as $value){
if($value['parent']==0){
if(in_array($value['id'],$iid)){
echo "<span class=\"big\">";
}
else{
echo "<a href=\"$page?id=$value[id]\" class=\"adminC\">";
}
echo "$value[name]";
if(in_array($value['id'],$iid)){
echo "</span>";
}
else{
echo "</a>";
}
echo "<br />\n";
if(in_array($value['id'],$iid)){
find_children($value['id']);
while(list($ket,$vet)= each($elem)){
echo "<a href=\"$page?id=$vet[id]\" class=\"adminM\">";
echo "$vet[name]";
echo "</a><br />\n";
}
}
}
}
Of course, $page is page name I am using...
Also, here is a function to show path from where user is coming to each page and it is working like a charm...
Can anybody lend a hand for me here? I clearly need a fresh set of ideas as to what am I doing wrong and how to make it happen...
I delivered my failed solution to the employer, so the answer here will only have a academic purpose, for I am intrigued by this problem...
And if possible I would like to understand the solution and not just to have a new code that would work since I REALLY need to understand this and to change my sig somewhat... 🙂
Thanx in advance for bothering!