电脑维修 笔记本维修 手机维修 打印机维修 IT外包
24小时服务电话:0731-84688748  18229718750
地址:长沙市天心区青园街道友谊社区友谊小区A3栋15号

服务区域:湖南省政府方圆5公里内(友谊社区 青园小区 阳光嘉园 天一康园 湘超景园 国际公寓 HOPSCA写字楼 豪布斯卡 天城·泰祥苑 国检园 梦网景园 石人村金石·蓉园安置小区 鑫隆家园 湘诚嘉园 童话里 七星车城 华铁佳苑 巢之恋 鑫远杰座 鑫远华城 满庭芳 宽域 鑫天山水洲城 星语林名园 青园街道进湾子社区 白沙世纪佳园 长沙欧洲城 长沙奥林匹克花园 高升安置小区 湘府名邸 嘉尚君远 富景园 鑫源公寓 岳泰理想城 等等...)

php获取分类目录并赋值给多维函数,二维或多维函数保存PHP树形分类目录 - 全文内容:

php获取分类目录并赋值给多维函数,二维或多维函数保存PHP树形分类目录

超短链接

  php获取分类目录并赋值给多维函数,二维或多维函数保存PHP树形分类目录:

/**
 * 查询分类下的所有子分类.此处可优化数据库查询
 *
 *
 * @param int $sort_id
 *            must. 父ID
 * @param string $type
 *            Optional. id/name/slug/description. Default is id.
 *@param  bool $self
 *              optional,false/true 包括自己本身
 *   @param  boll $isarr 
 *              optional. false/true retern string/array 
 *   @param  boll $taxonomy
 *              optional.   category/post_tag 
 *   @param  $tier
 *              no input.                                        
 * @return string
 */

function getchildrenids($sort_id=0, $type = 'id',$self=false,$isarr=FALSE,$taxonomy='category',$tier=0)
{
    $tier+=1;
    if($tier==1&&$self&&$sort_id>0){
        $dbids = db_sql("select yh_terms.term_id as id,yh_terms.name as name,yh_terms.slug as slug,yh_term_taxonomy.taxonomy as taxonomy,yh_term_taxonomy.description as description from yh_term_taxonomy left join yh_terms on yh_term_taxonomy.term_id=yh_terms.term_id  where yh_term_taxonomy.taxonomy='{$taxonomy}' and yh_terms.term_id='{$sort_id}'");
        if(!$dbids) return false;
        $row = $dbids->fetch_array(MYSQLI_ASSOC);
        $arr[]=array(
            'tier' => 0,
            '0_id' => $row['id'],
            '0_name' => $row['name'],
            '0_slug' => $row['slug'],
            '0_description' => $row['description']
        );
    }elseif($tier==1){
        $arr[]=array();
    }
    $dbids = db_sql("select yh_terms.term_id as id,yh_terms.name as name,yh_terms.slug as slug,yh_term_taxonomy.taxonomy as taxonomy,yh_term_taxonomy.description as description from yh_term_taxonomy left join yh_terms on yh_term_taxonomy.term_id=yh_terms.term_id  where yh_term_taxonomy.parent='{$sort_id}'  and yh_term_taxonomy.taxonomy='{$taxonomy}' ORDER BY `yh_terms`.`name` ASC");
    while($row = $dbids->fetch_array(MYSQLI_ASSOC)) {
        //var_dump($result);
        //$arr['str'] .= ',' . $row[$type];
//         $arr[$tier.'_id']=$row['id'];
//         $arr[$tier.'_name']=$row['name'];
//         $arr[$tier.'_slug']=$row['slug'];
//         $arr[$tier.'_description']=$row['description'];

        $arr[]=array(
            'tier' => $tier,
            $tier.'_id' => $row['id'],
            $tier.'_name' => $row['name'],
            $tier.'_slug' => $row['slug'],
            $tier.'_description' => $row['description']
            
        );
        $arrre=getchildrenids($row['id'], $type,FALSE,TRUE,$taxonomy,$tier);
        if($arrre) $arr=array_merge_recursive($arr,array($arrre));
//         echo "循环输出\n";
//         var_dump($row);
//         echo "返回值输出\n";
//         var_dump($arr);
        //$arr=array_merge_recursive($arr,array(getchildrenid($row['id'], $type,$taxonomy,TRUE,$tier)));
    }
    //$dbids->close();
    //unset($dbids);
    IF($isarr&&isset($arr)&&is_array($arr)){
        if($tier==1&&empty($arr['0'])) unset($arr['0']);
        return $arr;
    }elseIF($tier==1&&!$isarr&&isset($arr)&&is_array($arr)){
        return arrtostr($arr,$type,',');
    }else{
        return false;
    }
}

这么简单的代码,我居然花了一天时间才做出来,是因为不熟练还是因为不是编程的料?undecided