How to credit commisions to all the parents of a child php mysql

i am implementing chain system with auto bidding. when one user referred by parent(referral) then direct referral account have to credited with 10% of the user package amount and all parents of the direct referral credited 5% of the amount. how can i implement it. please tell me in basics not in oops.

Check this image

plan_id                  plan_amount                daily_commision
p1                          $100                       0.7%

p2                          $250                       1.0%

p3                          $500                       1.5%

p4                          $1000                       2%

user_id password plan plan_amount referred_by income status 
usr0    22222    p1     $100      Global       $57.5     1
usr1    12345    p1     $100       usr0        $72.5     1
user2   123sad   p1     $100       usr1        $135      1
user3   asdf4f   p2     $250       user2       $10       1
user4   321423   p4     $1000      user2       0         1  

here is users table user4 referred by user2 then 10% commision credited to user2 and 5% commision added to indirect referral usr1 and usr0.

then user gets daily commision according to their package. daily commision is added to user only not referral. every user got their daily commission every day.

please help me and save my job thanking you

“chain system”, “direct referral account”, “user package amount”???

You’re going to have to explain in more detail what your system is meant to do, I’m afraid. check this image

registered users referred by another user.
commission(referral amount) is some percentage of user package amount. for details please check above tables

checkout PostgreSQL’s WITH RECURSIVE feature.

I believe to more correct term is “MLM scheme”

1 Like


yes like this


Where is the “commission income” coming from?

Because you mentioned “my job” I’m assuming this is not for a fun exercise and unless someone is a generous philanthropist the money will need to come from somewhere.

check users table above

depends on the package amount the referral gets percentage. direct referral gets 10% and indirect referral gets 5%. these percentage of amount added to their income column in users table.

And they aren’t subtracted from anywhere?

The company’s assets?

no more any subtractions

// create user record
$commrate = 0.1;
while ($referred_by !="Global" or nothing or however you detect top of tree) {
  // write credit transaction to referrer, however to note they have a commission
  // set commrate to 0.05 for 5% for all remaining referrers
  // read row where user_id = referred_by, to get new referred_by id
  } // run through loop again

can you explain briefly with mysql query

That was the brief explanation. I don’t know enough about your database to go into much more detail, for example where are the transactions written?

All you need to do is to store the new user details, read the first referrer_id and assign them 10% of the value, then read their referrer and give them 5% of the value, and keep doing that last step until the referrer_id is blank, or “Global”, or whatever tells you to stop.

Have a go at converting my pseudo-code above into PHP code, and post your code if you have trouble with it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.