phpmysqlmulti-levelmlm

Multi Level user groups PHP Mysql while infinite loop


I want to create a multilevel user script and I have the following code that works but shows me only one user thread. My goal is to show all users per individual with at least 3 of their sub-users in each group.

I have this:

Maximo Eladio
- Jose Anibal
  - Juan De Dios
    - Pedro David
      - Luis Alberto
        - Henry
          - Eury Alexander 

But I want This:

(0)Maximo Eladio
- (1)Jose Anibal
  - (2)Juan De Dios
    - (3)Pedro David
      - (4)Luis Alberto
        - (5)Henry
          - (6)Eury Alexander 

(1)Jose Anibal
  - (2)Juan De Dios
    - (3)Pedro David
      - (4)Luis Alberto
        - (5)Henry
          - (6)Eury Alexander 

(2)Juan De Dios
    - (3)Pedro David
      - (4)Luis Alberto
        - (5)Henry
          - (6)Eury Alexander 

(3)Pedro David
      - (4)Luis Alberto
        - (5)Henry
          - (6)Eury Alexander  

(4)Luis Alberto
    - (5)Henry
       - (6)Eury Alexander 

This is my code actually:

$sql = "SELECT * FROM t_usuarios ";
$results = mysqli_query($con,$sql) or die(mysqli_error()) ;
if($results)
{
    while($result = mysqli_fetch_array($results))
    {
        $category['categories'][$result['id']] = $result; 
        $category['parent_cats'][$result['id_referido']][] = $result['id']; 
    }
}

function getCategories($parent, $category) 
{
    $html = "";
    if (isset($category['parent_cats'][$parent]))
    {
        $html .= "<ul>\n";
        foreach ($category['parent_cats'][$parent] as $cat_id)
        {
            if (!isset($category['parent_cats'][$cat_id]))
            {
              $html .= "<li>*".$category['categories'][$cat_id]['nombres']."</li> \n";
            }
            if (isset($category['parent_cats'][$cat_id]))
            {
              $html .= "<li>". $category['categories'][$cat_id]['nombres'] . " \n";
              $html .= getCategories($cat_id, $category);
              $html .= "</li> \n";
            }
        }
        $html .= "</ul> \n";
    }
    return $html;
}

    echo $data['category'] = getCategories(0, $category);

Please help me what Can i do to find the way, I try this but not work:

$results2 = mysql_query( "SELECT * FROM t_usuarios ORDER BY id ASC", $link );
while ( $tagrow = mysql_fetch_array( $results2 )){

$sql = "SELECT * FROM t_usuarios WHERE id=".$tagrow["id"]."";
$results = mysqli_query($con,$sql) or die(mysqli_error()) ;
if($results)
{
    while($result = mysqli_fetch_array($results))
    {
        $category['categories'][$result['id']] = $result; 
        $category['parent_cats'][$result['id_referido']][] = $result['id']; 
    }
}

function getCategories($parent, $category) 
{
    $html = "";
    if (isset($category['parent_cats'][$parent]))
    {
        $html .= "<ul>\n";
        foreach ($category['parent_cats'][$parent] as $cat_id)
        {
            if (!isset($category['parent_cats'][$cat_id]))
            {
              $html .= "<li>*".$category['categories'][$cat_id]['nombres']."</li> \n";
            }
            if (isset($category['parent_cats'][$cat_id]))
            {
              $html .= "<li>". $category['categories'][$cat_id]['nombres'] . " \n";
              $html .= getCategories($cat_id, $category);
              $html .= "</li> \n";
            }
        }
        $html .= "</ul> \n";
    }
    return $html;
}

    echo $data['category'] = getCategories(0, $category);
}

Solution

  • I don't understand what you tried to do in your last code, but it should work if you change echo $data['category'] = getCategories(0, $category); in your first code block to:

    $data['category'] = "";
    foreach (array_keys($category['parent_cats']) as $parent)
    {
        $data['category'] .= getCategories($parent, $category);
    }
    echo $data['category'];
    

    It does the same thing as you already did but multiple times with a different $parent every time for each cat.