tp递归查找上下级

递归必须要有参数,因为他是根据参数来作为条件执行的。

原理:自己调用自己,在自己函数内写了一个调用函数,调用的自己,当条件成立了调用一下自己。

代码:

 private $members = array();
 //声明空的数组我们下面要进行使用

public function Recursion( $userid, $price ) {

        //查询当前用户的下一级
        $Huomerbers = Db::name( 'user' )->where( 'id', $userid )->field( 'id ,userid,margin' )->select();
        $alls = $this->members = array_merge( $this->members, $Huomerbers );
        $authorities = array();
        //装我们上级内容使用的
        $price = $price;
        if ( count( $Huomerbers ) > 0 ) {
            foreach ( $Huomerbers as $value ) {
                $this->Recursion( $value['userid'], $price );
                //将用户id给我们执行的函数
                $authorities[] = $value;
            }
        }
}

上面将内容传给一个数组了。

 

 

上面结束

分润,查询上级给上级增加利润。

 public function Recursion( $userid, $price ) {

        //查询当前用户的下一级
        $Huomerbers = Db::name( 'user' )->where( 'id', $userid )->field( 'id ,userid,margin' )->select();
        $alls = $this->members = array_merge( $this->members, $Huomerbers );
        $authorities = array();
        //装我们上级内容使用的
        $price = $price;
        if ( count( $Huomerbers ) > 0 ) {
            foreach ( $Huomerbers as $value ) {
                $this->Recursion( $value['userid'], $price );
                //将用户id给我们执行的函数
                $authorities[] = $value;
            }
        }
        /*
        * 得到我们的数据内容所有的上级id和信息
        *  更新我们的数据,首先循环得到我们的上级id
        *   然后将我们的数据进行计算,然后进行插入。
        */
        foreach ( $authorities as $k => $v ) {
            $user = Db::name( 'user' )->where( 'id', $v['userid'] )->find();
            //当上级的利润大于我的然后才可以挣钱,拿着上级的利润减去我的利润,计算利润
            if ( $v['margin'] < $user['margin'] ) {
                $poor = $user['margin'] - $v['margin'];
                $prices = $price * $poor;
            } else {
                $prices = '0';

            }
            // dump( '我的上级id:--'.$v['userid'] );
            // dump( '我的id:--'.$v['id'] );
            // dump( $prices );
            // echo '<hr>';
            $data = Db::name( 'user' )->where( 'id', $v['userid'] )->setInc( 'profit', $prices );
            return $data;
        }

        // $succeed = $this->names( $userid, $price, $authorities );
        // return $succeed;
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章