PHP - OO / Array prob?

Associate
Joined
28 Nov 2004
Posts
1,237
Location
Birmingham
Hi all,
I have a class for my site menu. it works ok in that I can declare a new menu, call my 'get menu' function and it outputs on screen.

I've just introduced a 2nd level to my menu so as i'm looping through my menu items, i declare a new sub-menu and call the function. I know there are items in the database but nothing is coming back. Any help would be appreciated.

PHP:
<ul class="menu"><?php
    $menuLinks=array();
    $myMenu=new menu();
    $menuLinks=$myMenu->get_menu(1,0);
 
    foreach($menuLinks as $i => $value) {
     echo "<li><a href='".$menuLinks[$i]->get_url()."'>".$menuLinks[$i]->get_name()."</a></li>";
 
     }
    ?></ul>

That works fine. Now, when I add the following in, nothing comes back for my sub menu:

PHP:
<ul class="menu"><?php
    $menuLinks=array();
    $myMenu=new menu();
    $menuLinks=$myMenu->get_menu(1,0);
    foreach($menuLinks as $i => $value) {
     echo "<li><a href='".$menuLinks[$i]->get_url()."'>".$menuLinks[$i]->get_name()."</a></li>";
 
     $sublinks=array();    
     $mySubMenu=new menu();
     $sublinks=$mySubMenu->get_menu(1,$menuLinks[$i]->get_id());
     foreach($sublinks as $y => $value) {
      echo "<li><a href='".$sublinks[$y]->get_url()."'>".$sublinks[$y]->get_name()."</a></li>";
     }
 
    }
    ?></ul>
 
i've figured the problem on this. both of the "foreach" loops miss out the 1st item in the array. is this a common problem?
 
Hmm Normally if you loop through an array indices start at 0 so if you start looping through starting at 1 you will miss the first entry.I though that forech would loop through each entry hence the name, strange.
 
i wonder what it could be. perhaps my problem lies in the class side function which is building my array of menu objects....i'll take another look
 
This is where a debugger really pays off, takes a bit of manual effort with PHP though. Try doing print_r($sublinks); before entering the second foreach loop;
 
Last edited:
i've just read about the reset function actually (i'm new to php) so will try that. it's actually both foreach loops that are missing the 1st element. very strange.
i've ran the sql im using and they're not missing from those results so it must be something to do with my array loading if the reset doesn't work.
 
it's just a couple of db fields.
1 = menu type
0 = parent id

so, for each menu item id, i can call the same function passing that id as a parent id to get the sub menu items.

get_menu(1,1),
get_menu(1,2),
get_menu(1,3)
etc...
 
Ah okay I see,

try this, have a look at the source code once run what do you get?

foreach($sublinks as $key => $value) {
echo "<li><a href='".$key->get_url()."'>".$value->get_name()."</a></li>";
 
nah, that doesn't work. i have to reference it like:
foreach($sublinks as $key => $value) {
echo "<li><a href='".$sublinks[$key]->get_url()."'>".$sublinks[$key]->get_name()."</a></li>";
}
 
found it. im an idiot!

in my class, i had:
$row = mysql_fetch_array($retid);
while ($row = mysql_fetch_array($retid)) {

}

when it should be:
while ($row = mysql_fetch_array($retid)) {

}

basically skipping the 1st row of my result set!
 
yea, was prob something left over from my early development playing around. cheers for the input anyway!
lesson learned
 
Back
Top Bottom