Affiliate script with team help (Problem with recursion)

Asked

Viewed 257 times

3

I am writing a code in Codeigniter, for the dissemination of a free and non-profit Storage cloud software.

However, to work, we will have an affiliate system, which assigns a certain amount of storage to whom more disclose, as well as to the members above.

The biggest difficulty, is that the affiliate system is forced, IE, each level can only have a maximum of 5 users, when it is complete, search the levels below to incorporate in the members below.

This is the structure of the users table:

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| created      | datetime     | NO   |     | NULL    |                |
| join_ip      | varchar(30)  | NO   |     | NULL    |                |
| is_active    | tinyint(1)   | NO   |     | 0       |                |
| is_deleted   | tinyint(1)   | NO   |     | 0       |                |
| name         | varchar(100) | NO   |     | NULL    |                |
| email        | varchar(50)  | NO   |     | NULL    |                |
| country_id   | int(2)       | NO   |     | NULL    |                |
| sponsor_id   | int(20)      | NO   |     | NULL    |                |
| upline_id    | int(11)      | YES  |     | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+

The function I have in Model in PHP (Codeigniter) is this:

public function getAvailableSponsor($sponsor = NULL)
{
    if ($sponsor == NULL) return array();
    $this->db->select('id, sponsor_name, sponsor_id, upline_id')
        -> from('users')
        -> where('upline_id', $sponsor);

    $query = $this->db->get();
    $result = $query->result();
    $total = count($result);

    if ($total < 2){
        return $sponsor;
    }else {
        foreach ($result as $row){
            return $this->getAvailableSponsor($row->id);
        }           
    } 

}

The sequence I’m having as a result is this:

Sponsor ID 1 has 2 sponsored users FULL. 
Sponsor ID 2 has 2 sponsored users FULL. 
Sponsor ID 4 has 0 sponsored users Available :)

Next upline: 4 (AQUI deveria de ser 3, e nao 4.)

Query in the database:

 select id, sponsor_id, upline_id from users;

 +----+------------+-----------+
 | id | sponsor_id | upline_id |
 +----+------------+-----------+
 |  1 |          0 |         0 |
 |  2 |          1 |         1 |
 |  3 |          1 |         1 |
 |  4 |          1 |         2 |
 |  5 |          1 |         2 |
 +----+------------+-----------+

Any suggestions?

  • Explain your problem better. What exactly do you want to do?

  • Hi Jorge. What I want to do is that the records are entered in order their respective levels. For example if the system is 2 users per level, user 1 inserts 2 and 3. Then 1 inserts 2 more again, but will be inserted under 2. This article explains in detail what it will be necessary to do, but applied to this free and opensource software.

  • So you want a tree-based user insertion system?

  • Yes, so that the tree is filled automatically up to N levels.

  • The idea is more or less the same as this one: http://jam.jrox.com/kb/article/what-is-forced-matrix

  • 1

    I get it. You’re going through the sub-levels from left to right, so maybe 4 is on the same level as 3 but show up first, do a select all the database and put here the answer.

  • Edited question. I only brought the most important fields

  • As you do recursively what happens is that you will follow two different paths, by different routes. id=4 has parent id=2 id=3 has parent id=1 , that is, they are two different paths in recursion. And for some reason the path by father id=2 is quicker to check that you have a free child.

  • Yeah, that’s my problem. I’m a programmer with average knowledge, but when it comes to recursion I have a lot of difficulties.

  • In your foreach, when you run Return it does not continue to be, it exits in the first iteration.

Show 5 more comments

1 answer

1

You can try something with sub-select like:

SELECT a.id FROM users a
WHERE (SELECT count(*) FROM users b WHERE upline_id = a.id) < 2
ORDER BY a.id

Browser other questions tagged

You are not signed in. Login or sign up in order to post.