Home  >  Article  >  php教程  >  无限极分类相关代码

无限极分类相关代码

WBOY
WBOYOriginal
2016-06-06 19:35:01899browse

无详细内容 无 //非递归获取所有后代分类 function get_offspring($pids, $list) { $npid = array(); $offspring = array(); $has_child = true; while($has_child) { $has_child = false; foreach($list as $lk = $lv) { if(in_array($lv['pid'], $pids)) {

    //非递归获取所有后代分类
    function get_offspring($pids, $list)
    {
        $npid = array();
        $offspring = array();
        $has_child = true;
        while($has_child)
        {
            $has_child = false;
            foreach($list as $lk => $lv)
            {
                if(in_array($lv['pid'], $pids))
                {
                    $offspring[] = $lv;
                    $npid[] = $lv['cid'];
                    unset($list[$lk]);
                    $has_child = true;
                }
            }
            $pids = $npid;
        }
        return $offspring;
    }
    //利用路径字段获取后辈分类
    function get_offspring($pid)
    {
        $offspring = array();
        $cats = $this->getList('cat_id,cat_name,parent_id,cat_path', array(), 0, -1);
        foreach($cats as $cv)
        {
            if(in_array($pid, explode(',', $cv['cat_path'])))
            {
                $offspring[] = $cv;
            }
        }
        return $offspring;
    }
    //更新后辈分类路径
    function update_offspring_path($pid, $ppath)
    {
        if($ppath == ',')
        {
            $ppath = '';
        }
        $offspring = $this->get_offspring($pid);

        foreach($offspring as $ov)
        {
            $ov['cat_path'] = substr($ov['cat_path'], 0, strlen($ov['cat_path'])-1);
            $old_path = explode(',', $ov['cat_path']);
            foreach($old_path as $oldk => $oldv)
            {
                if($oldv == $pid)
                {
                    break;
                }
                unset($old_path[$oldk]);
            }
            $new_path = $ppath.implode(',', $old_path).',';
            if(!$this->update(array('cat_path'=>$new_path), array('cat_id'=>$ov['cat_id'])))
            {
                return false;
            }
        }
        return true;
    }
    //将列表整理为树形 返回二维数组
    function get_tree_list($pid, $arr, &$r)
    {
        foreach($arr as $k => $v)
        {
            if($v['parent_id'] == $pid)
            {
                if(isset($r[$pid]))//设置含有子类标记
                {
                    $r[$pid]['has_child'] = 1;
                }

                $v['cat_path'] = trim($v['cat_path']);//计算深度
                $path_str = substr($v['cat_path'], 0, strlen($v['cat_path'])-1);
                if($path_str == '')
                {
                    $v['deep'] = 0;
                }
                else
                {
                    $v['deep'] = count(explode(',', $path_str));
                }

                $v['format'] = str_repeat(' ', 6*$v['deep']);//计算缩进

                $r[$v['cat_id']] = $v;//加入有序列表

                unset($arr[$k]);//已加入 从无序列表中剔除

                $this->get_tree_list($v['cat_id'], $arr, $r);
            }
        }
    }
	//整理数据 返回多维树形数组
	public function getTree()
	{
		$key = 'operate_gettree';
		$cache = Bin_Cache::instance('Bin_Cache_File');
		$tree  = $cache->get($key);
		if(!$tree)
		{
			$men_sql = '
				SELECT
					`cat_id`,`alias_name`,`cat_name`,`cat_type`,`parent_id`,`cat_path`,`img`,`url`,`brand`,`condition`
				FROM
					`'.$this->_prefix.'b2c_virtual_cat`
				ORDER BY
					`order`
			';
			$items = $this->findAll($men_sql);
			$all_brand = $this->getAllBrand();
			foreach($items as $item)
			{
				$item['brands'] = $this->getBrand($item['brand'], $all_brand);
				$tmp[$item['cat_id']] = $item;
			}
		    $tree = array();
		    foreach($tmp as $k => $item){

		        if(isset($tmp[$item['parent_id']]))
	            {
		            $tmp[$item['parent_id']]['son'][] = &$tmp[$item['cat_id']];
		        }
	            else
	            {
		            $tree[] = &$tmp[$item['cat_id']];
		        }
		    }
		    $cache->set($key, $tree);
		}
	    return $tree;
	}
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