Ho il seguente codice:
Codice PHP:
$Sql->select("list", "*", "prefix_cat_for");
while ( $list = $Sql->assoc("list") )
{
$this->list_out[ $list['id'] ] = Array(
'id' => $list['id'],
'parent' => $list['parent'],
'name' => $list['name'],
'f_description' => $list['f_description'],
);
if ( $list['parent'] > 0 )
{
$this->sons[ $list['parent'] ][ $list['id'] ] = $list;
}
else
{
$this->fathers[ $list['id'] ] = $list;
}
}
foreach ( $this->list_out as $cat_id => $cat_data )
{
foreach ( $this->fathers as $for_id => $for_data )
{
if ( $for_data['parent'] == $cat_id )
{
$this->output_list .= $this->prova($for_data);
}
}
if ( $cat_data['parent'] == -1 && $for_data['parent'] == $cat_data['id'] )
{
echo "> ".$cat_data['name']."
".$this->output_list."
";
}
}
In pratica estraggo i dati di una tabella che contiene i dati delle varie categorie e sotto-categorie (forum).
Ora mettiamo che i dati sono:
id | parent | name
0 | -1 | Categoria 1
1 | 0 | Forum 1
2 | -1 | Categoria 2
3 | 0 | Forum 2
4 | 2 | Forum 3
5 | 0 | Forum 4
Quel codice mi stampa:
> Categoria 1
>> Forum 1
>> Forum 2
>> Forum 4
Il quale è esatto, ma sotto dovrebbe restituirmi anche:
> Categoria 2
>> Forum 3
E invece non lo fa.
Come suggeritomi da daniele_dll, ho stampato l'output di $this->list_out tramite print_r; questo è il risultato:
codice:
Array
(
[0] => Array
(
[id] => 0
[parent] => -1
[name] => Categoria 1
[f_description] =>
)
[1] => Array
(
[id] => 1
[parent] => 0
[name] => Forum 1
[f_description] => Descrizione forum 1
)
[2] => Array
(
[id] => 2
[parent] => -1
[name] => Categoria 2
[f_description] =>
)
[3] => Array
(
[id] => 3
[parent] => 0
[name] => Forum 2
[f_description] => Descrizione forum 2
)
[4] => Array
(
[id] => 4
[parent] => 2
[name] => Forum 3
[f_description] => Descrizione forum 3
)
[5] => Array
(
[id] => 5
[parent] => 0
[name] => Forum 4
[f_description] => Descrizione Forum 4
)
)
La classe MySql è correttissima (ho provato ad eseguire mysql_query e mysql_assoc senza $Sql ecc. e funziona allo stesso modo).
Grazie