Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Loop de categoria para subcategoria em PHP


tente isso
function fetchCategoryTreeList($parent = 0, $user_tree_array = '') {
global $con;
if (!is_array($user_tree_array))
$user_tree_array = array();

$sql = "SELECT * FROM `location` WHERE 1 AND `parent_id` = $parent ORDER BY id ASC";
$result=$con->query($sql);

if (mysqli_num_rows($result) > 0)
{
 $user_tree_array[] = "<ul>";
 while ($row =$result->fetch_object())
 {
  $user_tree_array[] = "<li>". $row->name."</li>";
      $user_tree_array = fetchCategoryTreeList($row->id, $user_tree_array);
 }
 $user_tree_array[] = "</ul><br/>";
}
  return $user_tree_array;
}

chame a função aqui
$res = fetchCategoryTreeList();
foreach ($res as $r)
{
   echo  $r;
}