首页 > 后端开发 > php教程 > 子栏目递归问题

子栏目递归问题

WBOY
发布: 2016-06-06 20:40:40
原创
1026 人浏览过

<code>static public function get($parentid = 0, $array = array(), $level = 0, $add = 2, $repeat = '  ') {

    $str_repeat = '';
    if ($level) {
        for($j = 0; $j  $v ['id'], 'catalog_name' => $v ['catalog_name'], 'catalog_name_alias' => $v ['catalog_name_alias'], 'parent_id' => $v ['parent_id'], 'level' => $level, 'sort_order' => $v ['sort_order'], 'seo_keywords' => $v ['seo_keywords'], 'seo_description' => $v ['seo_description'], 'attach_file' => $v ['attach_file'], 'attach_thumb' => $v ['attach_thumb'], 'status_is' => $v ['status_is'], 'data_count' => $v ['data_count'] , 'display_type' => $v ['display_type'], 'menu_is' => $v ['menu_is'],'template_list' => $v ['template_list'],'acl_browser' => $v ['acl_browser'],'acl_operate' => $v ['acl_operate'],'template_page' => $v ['template_page'],'template_show' => $v ['template_show'],'create_time' => $v ['create_time'], 'str_repeat' => $str_repeat, 'page_size'=>$v['page_size'] );

            $temparray = self::get ( $v ['id'], $array, ($level + $add) );
            if ($temparray) {
                $newarray = array_merge ( $newarray, $temparray );
            }
        }
    }
    return $newarray;
}
</code>
登录后复制
登录后复制

以上代码在Catalog model里,没理解错应该是递归获取子栏目这类的
现在我调用这货

<code>Catalog::get($catalogArr['id'],$catalogArr,2,2,'>>')
</code>
登录后复制
登录后复制

返回array(0) { }

<code>$catalogArr`打印出来的内容`array(22) { ["id"]=> string(2) "11" ["catalog_name"]=> string(12) "工作动态" ["catalog_name_alias"]=> string(4) "gzdt" ["parent_id"]=> string(1) "0" ["level"]=> int(0) ["sort_order"]=> string(1) "1" ["seo_keywords"]=> string(12) "工作动态" ["seo_description"]=> string(12) "工作动态" ["attach_file"]=> string(0) "" ["attach_thumb"]=> string(0) "" ["status_is"]=> string(1) "Y" ["data_count"]=> string(1) "0" ["display_type"]=> string(4) "list" ["menu_is"]=> string(1) "N" ["template_list"]=> string(9) "list_text" ["acl_browser"]=> string(0) "" ["acl_operate"]=> string(0) "" ["template_page"]=> string(9) "list_page" ["template_show"]=> string(9) "show_post" ["create_time"]=> string(10) "1409390084" ["str_repeat"]=> string(0) "" ["page_size"]=> string(2) "10" }
</code>
登录后复制
登录后复制

问我这样调用对吗》?为什么获取不了子栏目

回复内容:

<code>static public function get($parentid = 0, $array = array(), $level = 0, $add = 2, $repeat = '  ') {

    $str_repeat = '';
    if ($level) {
        for($j = 0; $j  $v ['id'], 'catalog_name' => $v ['catalog_name'], 'catalog_name_alias' => $v ['catalog_name_alias'], 'parent_id' => $v ['parent_id'], 'level' => $level, 'sort_order' => $v ['sort_order'], 'seo_keywords' => $v ['seo_keywords'], 'seo_description' => $v ['seo_description'], 'attach_file' => $v ['attach_file'], 'attach_thumb' => $v ['attach_thumb'], 'status_is' => $v ['status_is'], 'data_count' => $v ['data_count'] , 'display_type' => $v ['display_type'], 'menu_is' => $v ['menu_is'],'template_list' => $v ['template_list'],'acl_browser' => $v ['acl_browser'],'acl_operate' => $v ['acl_operate'],'template_page' => $v ['template_page'],'template_show' => $v ['template_show'],'create_time' => $v ['create_time'], 'str_repeat' => $str_repeat, 'page_size'=>$v['page_size'] );

            $temparray = self::get ( $v ['id'], $array, ($level + $add) );
            if ($temparray) {
                $newarray = array_merge ( $newarray, $temparray );
            }
        }
    }
    return $newarray;
}
</code>
登录后复制
登录后复制

以上代码在Catalog model里,没理解错应该是递归获取子栏目这类的
现在我调用这货

<code>Catalog::get($catalogArr['id'],$catalogArr,2,2,'>>')
</code>
登录后复制
登录后复制

返回array(0) { }

<code>$catalogArr`打印出来的内容`array(22) { ["id"]=> string(2) "11" ["catalog_name"]=> string(12) "工作动态" ["catalog_name_alias"]=> string(4) "gzdt" ["parent_id"]=> string(1) "0" ["level"]=> int(0) ["sort_order"]=> string(1) "1" ["seo_keywords"]=> string(12) "工作动态" ["seo_description"]=> string(12) "工作动态" ["attach_file"]=> string(0) "" ["attach_thumb"]=> string(0) "" ["status_is"]=> string(1) "Y" ["data_count"]=> string(1) "0" ["display_type"]=> string(4) "list" ["menu_is"]=> string(1) "N" ["template_list"]=> string(9) "list_text" ["acl_browser"]=> string(0) "" ["acl_operate"]=> string(0) "" ["template_page"]=> string(9) "list_page" ["template_show"]=> string(9) "show_post" ["create_time"]=> string(10) "1409390084" ["str_repeat"]=> string(0) "" ["page_size"]=> string(2) "10" }
</code>
登录后复制
登录后复制

问我这样调用对吗》?为什么获取不了子栏目

贴一个我的代码:

<code>/**
     * 返回多层栏目
     * @param $data 操作的数组
     * @param int $pid 一级PID的值
     * @param string $html 栏目名称前缀
     * @param string $fieldPri 唯一键名,如果是表则是表的主键
     * @param string $fieldPid 父ID键名
     * @param int $level 不需要传参数(执行时调用)
     * @return array
     */
    static public function channelLevel($data, $pid = 0, $html = " ", $fieldPri = 'cid', $fieldPid = 'pid', $level = 1)
    {
        if (empty($data)) {
            return array();
        }
        $arr = array();
        foreach ($data as $v) {
            if ($v[$fieldPid] == $pid) {
                $arr[$v[$fieldPri]] = $v;
                $arr[$v[$fieldPri]]['_level'] = $level;
                $arr[$v[$fieldPri]]['_html'] = str_repeat($html, $level - 1);
                $arr[$v[$fieldPri]]["_data"] = self::channelLevel($data, $v[$fieldPri], $html, $fieldPri, $fieldPid, $level + 1);
            }
        }
        return $arr;
    }
</code>
登录后复制

楼主可以看下

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板