gpt4 book ai didi

php - 多级用户组 PHP Mysql 无限循环

转载 作者:行者123 更新时间:2023-11-29 07:34:42 25 4
gpt4 key购买 nike

我想创建一个多级用户脚本,我有以下代码可以工作,但只显示一个用户线程。我的目标是显示每个人的所有用户,每个组中至少有 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 多次。

关于php - 多级用户组 PHP Mysql 无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49517218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com