多级用户组 PHP Mysql 无限循环

Multi Level user groups PHP Mysql while infinite loop

我想创建一个多级用户脚本,我有以下代码可以工作,但只显示一个用户线程。我的目标是显示每个人的所有用户,每个组中至少有 3 个子用户。

我有这个:

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

但我想要这个:

(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 

这实际上是我的代码:

$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);

请帮我找路,我试过了,但不行:

$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);
}

我不明白你在上一个代码中试图做什么,但如果你将第一个代码块中的 echo $data['category'] = getCategories(0, $category); 更改为:

,它应该可以工作
$data['category'] = "";
foreach (array_keys($category['parent_cats']) as $parent)
{
    $data['category'] .= getCategories($parent, $category);
}
echo $data['category'];

它做的事情和你已经做过的一样,但每次对每只猫多次使用不同的$parent