[Solved] How can I do select of all user referral id?


If you have the data in memory(which is problematic for a big table)

$users = array(
    array("id" => 1, "user_id" => 1 , "friend_id" => 2,),
    array("id" => 2, "user_id" => 2 , "friend_id" => 3,),
    array("id" => 3, "user_id" => 3 , "friend_id" => 4,),
    array("id" => 4, "user_id" => 10, "friend_id" => 15,),
);

function getRefferrals(&$users, $userId)
{
    $referrals = 1;

    foreach($users as $user) {
        if($user["user_id"] == $userId) {
            $referrals += getRefferrals($users, $user["friend_id"]);
        }
    }

    return $referrals;
}

$referrals = getRefferrals($users, 1) - 1;
var_dump($referrals);

If you do not have the data in memory you could check if the user has “friend_id” and if it exists in the table

2

solved How can I do select of all user referral id?