I don’t see a query that can solve the problem well, so I suggest doing it in PHP.
<?php
$sql = "SELECT * FROM modulos WHERE idMoludoBase = 0";
// Execute sua query
$modulos = Array();
while ($row = $res->fetch()) {
$sql = "SELECT * FROM modulos WHERE idMoludoBase = {$row['idModulo']}";
// Execute sua query
// Se ten submodulos
if ($sres){
// Adicione todos na chave submodulos
$row['submodulos'] = $sres->fetchAll();
}
// Adicona a linha ao array de modulos
$modulos[] = $row;
}
The code acime is "only an algorithm", must be adapted according to its drive of database.
Variable output $modulos
:
$modulos => Array(
[
idModulo => 1
idModuloBase => 0
modulo => Cadastros
...
submodulos => Array(
[
idModulo => 2
idModuloBase => 1
modulo => Cedentes
...
],
[ ... ]
)
],
[ ... ]
)
You only have 2 levels of categories, or this is undetermined?
– bfavaretto
There are only two levels: 0 - main category - >0 is the category that is filled... To avoid making two separate tables of category and subcategory...
– Sr. André Baill