Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1026337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:04:02+00:00 2026-05-16T12:04:02+00:00

I will try my best to be both succinct and fully explanatory in this

  • 0

I will try my best to be both succinct and fully explanatory in this predicament.

On a site I manage, we allow a manager to view their “Recruiting Downline” which entails a list of all agents they personally recruited, as well as the recruits that particular agent (and so on and so on) brought to the Team.

For example:

  • I recruit two Agents, A and B.
  • A recruits two agents, C and D.
  • B recruits two agents, E and F.
  • D recruits two agents G and H.
  • C, E, and F do nothing.

alt text

In the database, every individual agent record has a field for ‘referring agent’, which lists their recruited agent.

  • As the top level, when I click “My
    Recruits”, I am shown a list of all
    sub agents (because they ALL fall
    under my umbrella).
  • A is able to view C, D, G, and H.
  • B is only able to view E and F as
    they are his only downline recruits
    and they have brought nobody on
    board.

While this functionality works great it is flawed for two reasons:

Because of the way our PHP scripts are built, we are unable to sort the commission level data as a whole. Example: Even as the top man and I can see everybody, sorting by ‘commission level’ sorts my immediate agents by the criteria, then their downline as an item, then continues the sort based on my criteria. This is difficult to understand so to demonstrate, assume the table below displays the ‘commission level’ for ALL agents:

  • A, 7
  • B, 6
  • C, 5
  • D, 6
  • E, 5
  • F, 2
  • G, 5
  • H, 1

Note: an agent can NEVER recruit another agent at a higher level than they sit but they can recruit at ANY level below them (e.g. a 7 can recruit at 1,2,3,4,5,6 while a 3 can only recruit a 1,2).

FROM MY (high level) perspective,

While it would make sense for the data to be ‘sorted by commission level’ as:
A, D, B, G, C, E, F, H – this is not the case.

Instead (view from top agent’s perspective mind you) is:
A, D, G, H, C, B, E, F

basically, every while loop depends on the DIRECT upline agent number to determine who falls next in line.

I understand this is ‘very’ difficult to understand but let me know if I can provide any additional understanding into our current ‘sort’ issue.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:04:03+00:00Added an answer on May 16, 2026 at 12:04 pm

    i think i understood you. you want to sort by commission_level within a given agent hierarchy. the following may help (http://pastie.org/1111097)

    drop table if exists agent;
    
    create table agent
    (
    agent_id int unsigned not null auto_increment primary key,
    name varchar(32) not null,
    commission_level tinyint unsigned default 0,
    parent_agent_id int unsigned default null
    )
    engine = innodb;
    
    insert into agent (name, commission_level, parent_agent_id) values
    
    ('I', 99, null),
      ('A', 7, 1),
      ('B', 6, 1),
        ('C', 5, 2),
        ('D', 6, 2),
        ('E', 5, 3),
        ('F', 2, 3),
          ('G', 5, 5),
          ('H', 1, 5);
    
    
    delimiter ;
    
    drop procedure if exists agent_hier;
    
    delimiter #
    
    create procedure agent_hier
    (
    in p_agent_id int unsigned
    )
    proc_main:begin
    
    declare done tinyint unsigned default 0;
    declare dpth smallint unsigned default 0;
    
    
    create temporary table hier(
     parent_agent_id int unsigned, 
     agent_id int unsigned, 
     depth smallint unsigned default 0
    )engine = memory;
    
    insert into hier values (p_agent_id, p_agent_id, dpth);
    
    /* http://dev.mysql.com/doc/refman/5.0/en/temporary-table-problems.html */
    
    create temporary table tmp engine=memory select * from hier;
    
    while done <> 1 do
    
        if exists( select 1 from agent a inner join hier on a.parent_agent_id = hier.agent_id and hier.depth = dpth) then
    
            insert into hier 
                select a.parent_agent_id, a.agent_id, dpth + 1 from agent a
                inner join tmp on a.parent_agent_id = tmp.agent_id and tmp.depth = dpth;
    
            set dpth = dpth + 1;            
    
            truncate table tmp;
            insert into tmp select * from hier where depth = dpth;
    
        else
            set done = 1;
        end if;
    
    end while;
    
    
    select 
     a.agent_id,
     a.name as agent_name,
     if(a.agent_id = b.agent_id, null, b.agent_id) as parent_agent_id,
     if(a.agent_id = b.agent_id, null, b.name) as parent_agent_name,
     hier.depth,
     a.commission_level
    from 
     hier
    inner join agent a on hier.agent_id = a.agent_id
    inner join agent b on hier.parent_agent_id = b.agent_id
    order by
     -- dont want to sort by depth but by commision instead - i think ??
     -- hier.depth, hier.agent_id; 
     a.commission_level desc;
    
    drop temporary table if exists hier;
    drop temporary table if exists tmp;
    
    end proc_main #
    
    
    delimiter ;
    
    
    /*
    
    select * from agent;
    
    call agent_hier(1);
    call agent_hier(2);
    call agent_hier(3);
    call agent_hier(5);
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm going to try to explain this best I can I will provide more
This will be a bit difficult to explain but I will try my best.
I will try to explain this the best I can. I created a CMS
It's kinda hard to explain, but I will try my best. I have this
I will try and set the scene as best i can. *lights candle What
I will try to make this as clear as I can, but if you
I will try to keep this as simple as possible. I have a rather
HI, I have the following scenario which I will try to explain as best
I am having difficulty to explain this problem, but I will try anyway. I
I'm not too sure how to explain this, but I will try. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.