无限循环问题

时间:2013-09-28 15:00:42

标签: php loops yii foreach

这是我的功能。使用Yii fw。

public function generateMenu($type, $class = "", $curl, $parent = 0)
{
    switch ($type) {
        case 1:
            //LEFT MENU
            $html = "<ul $class>";
            if ($parent == 0 && $type == 0)
                $html .= '<li>
                                <div class="sidebar-toggler hidden-phone"></div>
                             </li>';
            $criteria        = new CDbCriteria();
            $criteria->order = "sort ASC";
            $criteria->compare("menuType", $type);
            $criteria->compare("parent", 0);
            $bu      = Yii::app()->baseUrl;
            $results = AdminMenu::model()->findAll($criteria);
            if ($results) {
                $i = 1;
                foreach ($results as $result) {
                    $liClass = "";
                    if ($i == 1 && $parent == 0)
                        $liClass = "start";
                    else if ($i == count($results) && $parent == 0)
                        $liClass = "last";
                    $criteria        = new CDbCriteria();
                    $criteria->order = "sort ASC";
                    $criteria->compare("parent", $result->id);
                    $children = AdminMenu::model()->count($criteria);

                    $active = $this->checkIfActive($result->url, $curl);

                    $liClass .= ($active) ? " active" : "";

                    $html .= "<li class='$liClass'>
                         <a href='";
                    $html .= ($children > 0) ? "javascript::" : $result->url;
                    $html .= "'>";
                    $html .= (is_null($result->icon)) ? "" : "<i class='$result->icon' ></i>";
                    $html .= "< span class='title' >" . $result->title . " </span >";
                    $html .= ($active) ? "<span class='selected'></span>" : "";
                    $html .= "</a>";
                    if ($children > 0) {
                        $html .= $this->generateMenu($type, "", $curl, $result->id);
                    }
                    $html .= "</li>";
                }
                $html .= "</ul>";
            }
            return $html;
            break;
    }


}

这样说:$nav->generateMenu(1, "page-sidebar-menu", Yii::app()->request->url, 0);

在函数内部,PHP进入无限循环。我究竟做错了什么?

数据库:

1 个答案:

答案 0 :(得分:2)

尝试

$criteria->compare("parent", $parent);

而不是

 $criteria->compare("parent", 0);