$query1 = User::where('sponser_id',$_SESSION['ruserid'])->get('userid');
$count1= count($query1);
$query2 = User::whereIn('sponser_id',$query1)->get('userid');
$count2= count($query2);
$query3 = User::whereIn('sponser_id',$query2)->get('userid');
$count3= count($query3);
$query4 = User::whereIn('sponser_id',$query3)->get('userid');
$count4= count($query4);
$query5 = User::whereIn('sponser_id',$query4)->get('userid');
$count5= count($query5);
$query6 = User::whereIn('sponser_id',$query5)->get('userid');
$count6= count($query6);
$query7 = User::whereIn('sponser_id',$query6)->get('userid');
$count7= count($query7);
$allcount=$count1+$count2+$count3+$count4+$count5+$count6+$count7;
我想计算用户的总下线。这段代码运行良好。但是如何使用循环来简化这些代码呢?
$allcount = 0;
$query1 = User::where('sponser_id',$_SESSION['ruserid'])->get('userid');
$count1 = count($query1);
$allcount = $allcount + $count1;
for ($i=2; $i<8; $i++) {
if ($i>2){
$j = $i-1;
${"query".$j} = User::whereIn('sponser_id',${"query".($j-1)})->get('userid');
}
${"query".$i} = User::whereIn('sponser_id',${"query".($i-1)})->get('userid');
${"count".$i} = count(${"query".$i});
$allcount = $allcount + ${"count".$i};
}
return $allcount;