1,递归找出指定用户的指定层级的下级用户

    1. /**
    2. * 找到指定用户的指定级别的下级用户
    3. * @param $pidStr 指定用户id
    4. * @param $num 指定第几个下级
    5. * @return string 返回下级id组成的字符串
    6. */
    7. function getSonsId($pidStr,$num){
    8. $sql = "SELECT user_id FROM ".$GLOBALS['ecs']->table('users')." WHERE parent_id IN (".$pidStr.")";
    9. $sonArr = $GLOBALS['db']->getAll($sql);
    10. foreach($sonArr as $key => $val){
    11. $sonArr[$key] = $val["user_id"];
    12. }
    13. $sonStr = implode(',',$sonArr);
    14. if($num == 1){
    15. return $sonStr;
    16. }else{
    17. $num--;
    18. return getSonsId($sonStr,$num);
    19. }
    20. }

    2,递归找当前产品最顶级分类

    1. //找到该商品的分类
    2. $sql = "SELECT cat_id FROM ecs_goods WHERE goods_id";
    3. $scat_id = $db->getOne($sql);
    4. $bigCatId = getBigCatByCatId($scat_id)
    5. //根据商品分类找到其所属的最顶级分类
    6. function getBigCatByCatId($cat_id){
    7. $sql = "SELECT parent_id FROM ecs_category WHERE cat_id = ".$cat_id;
    8. $fcat_id = $db->getOne($sql);
    9. if($fcat_id == '0'){
    10. return $cat_id;
    11. }else{
    12. return getBigCatByCatId($fcat_id);
    13. }
    14. }