Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

刚接触php对代码不太懂,关于下面那段代码,有没有关于分类中的父类能显示所有子分类下面所有内容的函数呢?在哪里修改可以让父类显示当前父类下面所有子分类的数据内容呢

public function fetch_category_data($type, $parent_id = 0, $order = 'sort ASC,id ASC')
    {
        static $category_list_all;

        if (!$category_list_all[$type])
        {
            if ($type)
            {
                $category_list_all_query = $this->fetch_all('category', '`type` = '' . $this->quote($type) . ''', $order);
            }
            else
            {
                $category_list_all_query = $this->fetch_all('category', '', $order);
            }

            if ($category_list_all_query)
            {
                foreach ($category_list_all_query AS $key => $val)
                {
                    $category_list_all[$type][$val['parent_id']][] = $val;
                }
            }
        }

        if (!$category_all = $category_list_all[$type][$parent_id])
        {
            return array();
        }

        return $category_all;
    }

    /* 获取分类数组 */
    public function fetch_category($type, $parent_id = 0)
    {
        $category_list = array();

        if (!$category_all = $this->fetch_category_data($type, $parent_id))
        {
            return $category_list;
        }

        foreach ($category_all AS $key => $val)
        {
            if (!$val['icon'])
            {
                $val['icon'] = G_STATIC_URL . '/css/default/img/default_class_imgs.png';
            }
            else
            {
                $val['icon'] = get_setting('upload_url') . '/category/' . $val['icon'];
            }

            $category_list[$val['id']] = array(
    'id' => $val['id'],
    'title' => $val['title'],
    'type' => $val['type'],
    'icon' => $val['icon'],
    'description' => $val['description'],
    'parent_id' => $val['parent_id'],
    'sort' => $val['sort'],
    'url_token' => $val['url_token']
);

            if ($child_list = $this->fetch_category($type, $val['id']))
            {
                $category_list[$val['id']]['child'] = $child_list;
            }
        }

        return $category_list;
    }

    /* 获取分类 HTML 数据 */
    public function build_category_html($type, $parent_id = 0, $selected_id = 0, $prefix = '', $child = true)
    {
        if (!$category_list = $this->fetch_category($type, $parent_id))
        {
            return false;
        }

        if ($prefix)
        {
            $_prefix = $prefix . ' ';
        }

        foreach ($category_list AS $category_id => $val)
        {
            if ($selected_id == $val['id'])
            {
                $html .= '<option value="' . $category_id . '" selected="selected">' . $_prefix . $val['title'] . '</option>';
            }
            else
            {
                $html .= '<option value="' . $category_id . '">' . $_prefix . $val['title'] . '</option>';
            }

            if ($child AND $val['child'])
            {
                $html .= $this->build_category_html($type, $val['id'], $selected_id, $prefix . '--');
            }
            else
            {
                unset($prefix);
            }
        }

        return $html;
    }

    /* 获取分类 JSON 数据 */
    public function build_category_json($type, $parent_id = 0, $prefix = '')
    {
        if (!$category_list = $this->fetch_category($type, $parent_id))
        {
            return false;
        }

        if ($prefix)
        {
            $_prefix = $prefix . ' ';
        }

        foreach ($category_list AS $category_id => $val)
        {
            $data[] = array(
    'id' => $category_id,
    'title' => $_prefix . $val['title'],
    'type' => $val['type'],
    'description' => $val['description'],
    'sort' => $val['sort'],
    'parent_id' => $val['parent_id'],
    'url_token' => $val['url_token']
);

            if ($val['child'])
            {
                $data = array_merge($data, json_decode($this->build_category_json($type, $val['id'], $prefix . '--'), true));
            }
            else
            {
                unset($prefix);
            }
        }

        return json_encode($data);
    }

    /* 获取数组信息 */
    public function get_category_info($category_id)
    {
        static $all_category;

        if (!$all_category)
        {
            if ($all_category_query = $this->fetch_all('category'))
            {
                foreach ($all_category_query AS $key => $val)
                {
                    if (!$val['url_token'])
                    {
                        $val['url_token'] = $val['id'];
                    }

                    $all_category[$val['id']] = $val;
                }
            }
        }

        return $all_category[$category_id];
    }

    /* 获取数组信息 */
    public function get_category_info_by_url_token($url_token)
    {
        static $all_category;

        if (!$all_category)
        {
            if ($all_category_query = $this->fetch_all('category'))
            {
                foreach ($all_category_query AS $key => $val)
                {
                    if (!$val['url_token'])
                    {
                        $val['url_token'] = $val['id'];
                    }

                    $all_category[$val['url_token']] = $val;
                }
            }
        }

        return $all_category[$url_token];
    }

    public function get_category_list($type)
    {
        $category_list = array();

        $category_all = $this->fetch_all('category', '`type` = '' . $this->quote($type) . ''', 'id ASC');

        foreach($category_all as $key => $val)
        {
            if (!$val['url_token'])
            {
                $val['url_token'] = $val['id'];
            }

            $category_list[$val['id']] = $val;
        }

        return $category_list;
    }

    public function get_category_with_child_ids($type, $category_id)
    {
        $category_ids[] = intval($category_id);

        if ($child_ids = $this->fetch_category_data($type, $category_id))
        {
            $category_ids = array_merge($category_ids, fetch_array_value($child_ids, 'id'));
        }

        return $category_ids;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
261 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...