遞歸必須要有參數,因爲他是根據參數來作爲條件執行的。
原理:自己調用自己,在自己函數內寫了一個調用函數,調用的自己,當條件成立了調用一下自己。
代碼:
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;
}