Home  >  Article  >  Backend Development  >  子栏目递归问题

子栏目递归问题

WBOY
WBOYOriginal
2016-06-06 20:40:40894browse

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

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

Catalog::get($catalogArr['id'],$catalogArr,2,2,'>>')

返回array(0) { }

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

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

回复内容:

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

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

Catalog::get($catalogArr['id'],$catalogArr,2,2,'>>')

返回array(0) { }

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

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

贴一个我的代码:

/**
     * 返回多层栏目
     * @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;
    }

楼主可以看下

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn