分享

tp递归查找上下级

 程序猿小馆 2021-06-06

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

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

代码:

 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;

    }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约