递归循环出菜单,如何通过smarty显示?还有什么好的方法?

#1 edricoo

下面的递归可以循环出菜单,见结果,可否用smarty显示?不知道如何分级别发送到smarty。
// **********************本段为菜单信息*******************************************>>
        function menu(){ // 这里是首页
                $this->checksession();
                $conditions = "parent = 0";
                $results = spClass("test_menu")->findAll($conditions);
                if($results){
                        echo "
    ";
                            foreach( $results as $user )        {
                                    echo "
  • ".$user['name']."
  • ";
                                    // 增加子分类
                                            if( !empty($user["name"]) ){
                                            echo "
      ";
                                              foreach( $user["name"] as $fruit ){
                                                      echo "
    • ".$user['name']."
    • ";
                                              }
                                              echo "
    ";
                                    }
                                    $this->printmenu($user["id"]);
                            }
                            echo "
";
                }else{
                        echo "系统还未初始化,暂时不提供使用!";
                }
        }
         
        function printmenu($id){
        $conditions = "parent = ".$id;
        $results = spClass("test_menu")->findAll($conditions);
        if($results){
                echo "
    ";
                    foreach( $results as $user )        {
                            echo "
  • ".$user['name']."
  • ";
                            // 继续展开
                            if( !empty($user["name"]) ){
                                            echo "
      ";
                                              foreach( $user["name"] as $fruit ){
                                                      echo "
    • ".$user['name']."
    • ";
                                              }
                                              echo "
    ";
                                    }
                            $conditions2 = "parent = ".$user["id"];
                            $results2 = spClass("test_menu")->findAll($conditions2);
                            if( $results2 ){
                                    $this->printmenu($user["id"]);
                            }
                           
                    }
                    echo "
";
        }
        }
        // **********************本段为菜单信息*******************************************<<

结果:
  • 001
    • 0011
      • 00110
    • 0010
  • 002
  • 003
    • 0030
  • 004

2011-10-12 14:08:02

#2 jake

用一个变量,把你echo的东西都记下来
echo "
  • ".$user['name']."
  • ";
    变成
    $string .= "
  • ".$user['name']."
  • ";
    然后 在menu函数的最后

    $this->mymenu = $string;
    $this->display('模板');

    模板里面显示:
    <{$mymenu }>

    2011-10-12 14:12:45

    #3 edricoo

    谢jake,这么简单的一个变换,脑子秀逗了:lol

    2011-10-12 14:17:33