need a logic for this problem...any think tanks?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rami
Forum Contributor
Posts: 217
Joined: Thu Sep 15, 2005 8:55 am

need a logic for this problem...any think tanks?

Post by rami »

Scenario:
i have made a point system for user

ok system is like this
a user is referred by some body to be member ..s/he send the link to friends like
http://www.site.com/signup.php?ref=userid

i have database table which has
id name add,email,password,refferedby field...
in that refferedby field the id of the person who has referred gets in and for every such refferral sign up the user(who has

sent that signup link with id) gets some points.
Interesting things begins here
if the person who was referred by x (supppose y) refers it to somebody with his/her id ,/she she earns to point but at the

same time the original referrer who has referred y (the x) also earns the points

who ever below y makes member along with y x also gets points and so on..

The problem:
i was trying to display the total members made made by member (directly or indirectly,means by memebers made by me)
so the simple logic i could think of is

Code: Select all

select * from table where referredby=userid;
but as you know this will not solve the problem..as i get point of other members also who was referred by the people who were

reffered by me and this query will only display people who were directly reffered by user of me.

so how can i solve the problem?


graphical view
x-->y (reffred by x)-->z (reffred by x)
when z becomes member, y as well as x earns point and so on in link...
i am trying to calcuate points of x (or members made by x)

i think i need a genuie loop for it but to be frank i thought and thought but seems like beyond reach of my mind

what i hope to get:
may be logic.....or some codes(of that loop will be great)
by the way do i need to introduce additional field in database table

any solution welcomed until it solves problem...

thanks

Note:I pray that i was able to make scenario clear...if i wasn't i am sorry and i hope i can make things clear in further

posts in this topic..
pray people have got it and there any be some logic genius to work out a innovative logic..or code..


thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd store the userid of both the new person and the referrer instead of name and password. One thing that comes to mind is using the hierarchical storage method of presorted trees since this is a pyramid reward system.

http://dev.mysql.com/tech-resources/art ... -data.html The Nested Set is what I'm referring to on this page.
rami
Forum Contributor
Posts: 217
Joined: Thu Sep 15, 2005 8:55 am

Post by rami »

feyd wrote:I'd store the userid of both the new person and the referrer instead of name and password. One thing that comes to mind is using the hierarchical storage method of presorted trees since this is a pyramid reward system.

http://dev.mysql.com/tech-resources/art ... -data.html The Nested Set is what I'm referring to on this page.
i also storing the user id and referrer id
when users sign in the id gets entered as
$query = "INSERT INTO users (email, pass, first_name, last_name,username, active,reffered, registration_date) VALUES ('$e', SHA('$p'), '$fn', '$ln','$u', '$a',$uid, NOW() )";

see referer id aslo gets in

but please i hope i am clear

the problem is when displaying the total points earns by the person or total person under a person
x---y---z
in y referred field thier is x add but in z refferal field thier is y address so x and z has no relation but when dispalying data the points earned by z is also added x account
here is the problem

any idea
thanks for some answer.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

they do have a relation. z's referred is y and y's referrer is x so by going through the branches of the tree you can determine that x is at the top of the tree (igt has no referre)...ah ha! I see your problem! you can't start at the roots! hum..easiest thing to do is to add a third field (person referred) which gets filled in if someone they referred joins, i recommend making this a list of people, perhaps using comma's to seperate? and that way you acn start at the roots and go out to the leaves... :-D
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Actually, with the link feyd provided it's pretty easy to determine and update the parents that need that get another refferer point... Just have a look at the 'retrieving single path' sections in the article...
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

sorry, I didn't read the link. Just thought of how I'd do it :-D
rami
Forum Contributor
Posts: 217
Joined: Thu Sep 15, 2005 8:55 am

Post by rami »

to be frank i didnt got the answer
by the way if i change the referred field then going to root then things will go wrong
ok simple logic i can think of is
for user 1
select * from users where referredby=1
suppose he has reffred 5 people 2.,34,5,6
then for each user(2-6)
select * from users where referredby=2
{
suppose he has reffred 2 people 7,8
then
then for each user(7,8)
select * from users where referredby=7
......so on
}
when 2 is done
then
}
select * from users where referredby=3

{
then people reffred by 3..
}

goes on so
.....1 gets points as he is root.....so does 2(even though 8 makes users)

now this should be kept to a loop how can it be done
that all i am asking but i feel its getting harder and harder
any way hope may be answer is coming
may be up to 6th level
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

In the most braindead solution you would:

(1) find referer (with the help of $_GET['refid']), let's name it the current_referrer (null if not found)
(2) if the current_referrer equals null go to (6) otherwise to (3)
...(3) update the current_referrer 'make new friends counter' to 'make new friends counter' + 1
...(4) find the referrer of the current_referrer and make him the current_referrer (null if not found)
...(5) go back to (2)
(6) end.
rami
Forum Contributor
Posts: 217
Joined: Thu Sep 15, 2005 8:55 am

Post by rami »

timvw wrote:In the most braindead solution you would:

(1) find referer (with the help of $_GET['refid']), let's name it the current_referrer (null if not found)
(2) if the current_referrer equals null go to (6) otherwise to (3)
...(3) update the current_referrer 'make new friends counter' to 'make new friends counter' + 1
...(4) find the referrer of the current_referrer and make him the current_referrer (null if not found)
...(5) go back to (2)
(6) end.
no think tanks
any way thanks for reply
by the way when i thought and thought
as i have said up to six level
may be rough algorithm may be like this

1(for 1 there is null in reffered id filed)
--2---------------------------3---------------------4
5,6 7 8,9

for 2

select id from table where reffid=1
while(numrows!=0)//stop condition...no body referred ny that person...
do
{for each result 2,3,4
fo 2 first
echo "one of people under you is id(2)

now working node=id(2)
select id from table

select id from table where id=workingnode
while(numofrows!=0)
//this is stop condition as for lowest user s/he will not have referred any one as s/he is just registered

then
it gives 5,6
for each result
do same for 5, as we did for 2

so the rought algorithm may be just like this
any way i know it should be put to loop but no real help up to now
can some body just give me some minutes to put this idea in to coding

may be last hope....for this

any way thanks for all help
by the way there are some sites which are running under this concept
fo eg
may be site called
7dollars.com
how did they did then?
thanks
Post Reply