david olson
david olson

Reputation: 31

Multi level referral system

username    referrer
--------    --------
admin
kelly88     admin   // UPDATE USERNAME ADMIN WITH +5 POINTS //
jacob       kelly88 // UPDATE USERNAME ADMIN WITH +3 POINTS AND USERNAME kelly88 WITH +5 POINTS //
david16     jacob   // UPDATE USERNAME ADMIN WITH +1 POINTS AND USERNAME kelly88 WITH +3 POINTS AND USERNAME jacob WITH +5 POINTS //

Is this possible. If yes - HOW?

I know only how to do it with level one.

$query = 'UPDATE users SET points = points + :points WHERE username = :username';
$select = $db->prepare($query);
$select->bindValue(':points', 5.00, PDO::PARAM_STR);
$select->bindValue(':username', 'admin', PDO::PARAM_STR);// admin = $userInfo['referrer']
$select->execute();

Upvotes: 1

Views: 2000

Answers (1)

MBaas
MBaas

Reputation: 7530

Seems you're looking for a PHP-Solution. Since you did not indicate that you're using a DB, I assume the data is in an array. Unfortunately, I can not test it right now, but something like this should do the job:

$refs = array("admin" => "", "kelly88" => "admin", "jacob" => "kelly88", "david16" => "jacob");
$points = array();

foreach ($refs as $usr => $ref)
{
   reward($ref,5);
}

function reward($ref,$rew)
{// rewards: 5-3-1 points
   global $refs, $points;
   if ($ref && array_key_exists("ref",$refs))
   {
       if (!array_key_exists($ref,$points)) $points[$ref]=0;
       $point[$ref]+=$rew;  
       if ( $rew > 1)  reward($refs[$ref] , $rew-2); // referrer's referrer and onwards...

   }
}

Upvotes: 1

Related Questions