开发者

Need some help to determine the amount of recursive calls in PHP

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 recursively.

The thing I want is to determine at which recursion level 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");   
    }        开发者_如何学C   

    $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;
}


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;
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜