I've been struggling with this problem for two days and i still couldn't come up with a solution. I an currently designing a website where a user can by the mean of his memberid register other users so that we can allow him points.One of the main game in the site requires that if a user has registered two other users his percentage equals 1.then the function must recursively check for each of those two users if they also have registered two users each then increase the percentage to 3.So for each level found apart from the 1st ,percentage must be incremented by two;
Here is a little diagram to show you ho it is suppose to work:
[A]
[B] [C]
[D] [E] [F] [G]
[H][I] [j][k] [l][m] [o][p]
A represents the member we want to find the percentage for.He registers [B] and[C] his percentage=1 B registers [D] and [E] percentage+=1.C registers [F] and [G]here also percentage+=1.So at this level of checking percentage will be equal to 3.
here is the function i came up with but its incomplete and im pretty sure it wont work.am stuck please help me.
function calcpercentage($id)
{
$count=0;
$percent=1;
$id1=$id;
$sql="SELECT * from bs100049 WHERE RefId='$id1'";
$checkh=mysql_query($sql) or die('error'.mysql_error());
while($result=mysql_fetch_array($checkh)&& $count<=2)
{
$count++;
$id1=$result['MemberId'];
$percent++;
}
}
As i am also working with mysql it doesnt make it easy. Am sure though once i figure out the function I'll be able to write a function to draw that tree above dynamically for each user.
here is a better explanation(i wanted to post this after Dan's answer but i couldnt) here is how this site am designing works.you can register only if an existing member is your referrer so in the "member" table your id and the referer's will be stored. in this format (id,name,memberid,refid).once you are registered you can apply to participate in different games.the bs100049 game requires that you make a purchase and register 2 users yourself.once this is done you can be sure that you already earn 1%.if and only if both your registered users have completed what you did above(each of them has registered two users ) then percentage increments from 1 to 3 and so the next value of percentage will be 5 and so on .if a user has made a purchase for the bs100049 game,he is automatically stored in the bs100049 table even though he doesnt have any registered users yet so if he queries to see what is his current percentage it' ll be 1%...
Here is my approach. Please note that you should consider switching to PDO or mysqli as the mysql functions are deprecated. For the sake of the example I have used them though. You also have to make sure that your data indeed forms a tree because otherwise you will run into an infinite loop.
If you can avoid the recursion try to.
I am assuming that the id of the referred user exists in the table in a column called id. I also assume that there can never be more than two referred users and limit them at 2.
function calc($id) {
$sql = "SELECT * FROM bs100049 WHERE RefId='$id' LIMIT 2";
$res = mysql_query($sql);
if(mysql_num_rows($res) == 2) {
// We have two referred users (1 point) and if calc returns 1 we add 1 for each user.
$user1 = mysql_result($res, 0, 'id');
$user2 = mysql_result($res, 1, 'id');
return 1 + (1 * calc($user1)) + (1 * calc($user2));
}
// If we get here we have 0 or 1 referred users.
return 0;
}
I think for it to be recursive you first must define the end case - when a user has NOT registered 2 users:
function calcpercentage($id){
//SQL to get $count (number of users this user has referred) + also array of usr ids at this 'level'
foreach($usr_ids as $usr_id){
if($count<2){
return 0;
}else{
return 1+calcpercentage($usr_id);
}
}
}