tags:

views:

48

answers:

2

Hi all,

I've got a, I think fairly easy question, but this is bugging me for a while now. So I figured, maybe I can get some help here.

Since recursive functions are always a bit tricky, and sometimes a bit unclear to me, I keep struggling to create a nice working solution to get my menudata.

In one of my classes I have this function, which gives me all menu-items recursivly. The thing I want is to determine at which recursionlevel a certain object was retrieved so I can create a nicely looking HTML output with indents for the levels of nesting.

public function GetObjectList($parentID = 0, $objectlist = null)
{
    if(is_null($objectlist))
    {
        $objectlist = new ObjectList("Model_Navigation");   
    }           

    $query  = MySQL::Query("SELECT * FROM `Navigation` WHERE `WebsiteID` = ".SITE_ID. " AND `LanguageID` = ".LANG_ID." AND `ParentID` = ".$parentID);

    while($result = MySQL::FetchAssoc($query))
    {           
        $object = new Model_Navigation();

        $object->ID             = $result["ID"];
        $object->WebsiteID      = $result["WebsiteID"];
        $object->LanguageID     = $result["LanguageID"];
        $object->ParentID       = $result["ParentID"];
        $object->Name           = $result["Name"];
        $object->Page           = Model_Page::GetObjectByID($result["PageID"]);
        $object->ExternalURL    = $result["ExternalURL"];
        $object->Index          = $result["Index"];
        $object->Level          = [here lies my problem];
        $objectlist->Add($object);

        self::GetObjectList($object->ID, $objectlist);
    }

    return $objectlist;
}

Hope to hear from you!

Greetings from Holland,

Ben Fransen

A: 

Why don't you just add a parameter to the function call that stores the number of calls. At the first call just make that 0, increment the value inside the function and use it in the recursive call.

Alien426
I've tried that, but the levelcount doesn't match when I do that.
Ben Fransen
+3  A: 
public function GetObjectList($parentID = 0, $objectlist = null, $level = 1)
{
    if(is_null($objectlist))
    {
        $objectlist = new ObjectList("Model_Navigation");   
    }           

    $query  = MySQL::Query("SELECT * FROM `Navigation` WHERE `WebsiteID` = ".SITE_ID. " AND `LanguageID` = ".LANG_ID." AND `ParentID` = ".$parentID);

    while($result = MySQL::FetchAssoc($query))
    {           
        $object = new Model_Navigation();

        $object->ID             = $result["ID"];
        $object->WebsiteID      = $result["WebsiteID"];
        $object->LanguageID     = $result["LanguageID"];
        $object->ParentID       = $result["ParentID"];
        $object->Name           = $result["Name"];
        $object->Page           = Model_Page::GetObjectByID($result["PageID"]);
        $object->ExternalURL    = $result["ExternalURL"];
        $object->Index          = $result["Index"];
        $object->Level          = $level;
        $objectlist->Add($object);

        self::GetObjectList($object->ID, $objectlist, $level+1);
    }

    return $objectlist;
}
Anatoly Orlov
Thanks, that worked! By I'm a bit confused now... Why does $level+1 work and $level++ doesn't? While it does, in my opinion, the same...?
Ben Fransen
@Ben Fransen: `$level++` is 'post-increment', so the value returned by the expression is before the increment. `++$level` would work. see http://www.php.net/manual/en/language.operators.increment.php
Tom Haigh
Thanks for explaining Tom! Now you mention it becomes clear, +1
Ben Fransen