Page 1 of 1

referral system

Posted: Tue Nov 02, 2010 6:43 pm
by nite4000
Hey everyone I am working on my referral system but ran into a bug.

I have it a total of 6 levels

Me>someone
someone>someone2
someone2>someone3
and so on

well when i get up to level 6 i need it to not update me anymore but if someone else adds one to have it update. its not allowing any other members to add referrals but instead is changing mine via the last one on level 6.

I will show you my code hoever i am sure you will see alot of if statements. I plan to clean it up just trying to get it to work.

Here is my code

Code: Select all

if($myinfo['username'] != '$refinfo') {
	
$q1 = mysql_query("INSERT INTO referrals(id, username)  VALUES(null, '$d_user')")or die( mysql_error() );
  
}

if($_POST['referral_info'] != NULL) {
	
if($myinfo['referral'] == NULL) {


$r=mysql_query("UPDATE referrals SET referral='$d_user' WHERE username = '$refinfo'")or die(mysql_error());

}
}
	
if($_POST['referral_info'] != NULL) {
	
if($myinfo['level_2'] == NULL) {


$r=mysql_query("UPDATE referrals SET level_2='$d_user' WHERE referral ='$refinfo' LIMIT 1")or die( mysql_error() );
}
}

if($_POST['referral_info'] != NULL) {

if($myinfo['level_3'] == NULL) {

$r=mysql_query("UPDATE referrals SET level_3='$d_user' WHERE level_2 ='$refinfo' LIMIT 1")or die( mysql_error() );
}

}
if($_POST['referral_info'] != NULL) {

if($myinfo['level_4'] == NULL) {

$r=mysql_query("UPDATE referrals SET level_4='$d_user' WHERE level_3 ='$refinfo' LIMIT 1")or die( mysql_error() );
}

}

if($_POST['referral_info'] != NULL) {

if($myinfo['level_5'] == NULL) {

$r=mysql_query("UPDATE referrals SET level_5='$d_user' WHERE level_4 ='$refinfo' LIMIT 1")or die( mysql_error() );

}

}
if($_POST['referral_info'] != NULL) {

if($myinfo['level_6'] == NULL) {

$r=mysql_query("UPDATE referrals SET level_6='$d_user' WHERE level_5 ='$refinfo' LIMIT 1")or die( mysql_error() );

}

}
I just need it to allow everyone to get up to level 6 field but right now its only allowing me to get to level 6

I hope someone can help me out

Thanks

Re: referral system

Posted: Wed Nov 03, 2010 9:48 am
by twinedev
Not knowing anything else about the way it works other than what you gave, here is how I would set it up in general:

[text] id | ref_id | name | email
----+--------+------+------------------
1 | 0 | Greg | greg@example.com
2 | 1 | Dave | dave@example.com
3 | 0 | Matt | matt@example.com
4 | 2 | Brad | brad@example.com
5 | 4 | Mark | mark@example.com
6 | 3 | Luke | luke@example.com
7 | 4 | Rick | rick@example.com
8 | 5 | Josh | josh@example.com
9 | 1 | Lisa | lisa@example.com[/text]
This data gives the following referral tree
[text]Greg ---> Dave ---> Brad ---> Mark ---> Josh
| |
+-> Lisa +-> Rick

Matt ---> Josh[/text]

Now when you have a new user, you insert it here, giving NULL for id, but giving it the id of who referred them (and their other data)
Now you can notify people by calling this function with the new row's id and optionally how many levels to notify (defaults to 6)

Code: Select all

function notifyTree($id,$level=6) {
	$id = (int)$id; // sanatize, make sure it is a number
	if ($id > 0) {
		$rsTree = mysql_query('SELECT * FROM tblReferals WHERE id='.$id);
		if ($rsTree && mysql_num_rows($rsTree)>0) {
			$aryTree = mysql_fetch_assoc($rsTree);
			// DO notification to $aryTree['email'];
			mysql_free_result($rsTree);
			if ($level > 0 && $aryTree['ref_id']>0) {
				notifyTree($aryTree['ref_id'],$level-1);
			}
		}
	}
}