Ciao a tutti e grazie in anticipo!
Ho una tabella del DB così formata:
____ id____|___name___|__parentId__|__owner__|
1 | gruppo1 | 0 | |
2 | gruppo2 | 1 | |
3 | gruppo3 | 1 | |
4 | gruppo4 | 2 | |
Una semplice gerarchia, gruppo1 è il principale, gruppo 2 e gruppo3 appartengono a gruppo 1 così via.
Io vorrei riportare questa struttura in un XML.
La mia funzione qui sotto ha qualche problemino nel chiudere i tag, la struttura dell'XML dovrebbe essere questa:
Codice PHP:
<GRUPPO>
<ID>5</ID>
<NAME>Gruppo 1</NAME>
<PARENT>0</PARENT>
<OWNER></OWNER>
<CHILDREN>
<GRUPPO>
<ID>2</ID>
<NAME>Gruppo 2</NAME>
<PARENT>1</PARENT>
<OWNER></OWNER>
<CHILDREN>
(non ne ha)
</CHILDREN>
</GRUPPO>
<GRUPPO>
<ID>3</ID>
<NAME>Gruppo 3</NAME>
<PARENT>1</PARENT>
<OWNER></OWNER>
<CHILDREN>
(non ne ha)
</CHILDREN>
</GRUPPO>
</CHILDREN>
</GRUPPO>
<GRUPPO>
<ID>4</ID>
<NAME>Gruppo 4</NAME>
<PARENT>0</PARENT>
<OWNER></OWNER>
<CHILDREN>
(non ne ha)
</CHILDREN>
</GRUPPO>
sbaglio qualcosa nella chiusura dei tag e non trovo l'errore...
AIUTATEMI PLEASE
ecco il mio script!
Codice PHP:
function get_organigramma($id=0){
$query = $this->db->query('SELECT * FROM groups WHERE id>4 AND parentId='.$id);
//debug
//echo " ha ".$query->num_rows." figli.
";
echo "parentId=".$id." -> trovati ".$query->num_rows." record
";
if($query->num_rows > 0) {
foreach($query->result() as $row){
$this->xml .= "<GRUPPO>\n";
$this->xml .= "<ID>".$row->id."</ID>\n";
$this->xml .= "<NAME>".$row->name."</NAME>\n";
$this->xml .= "<PARENT>".$row->parentId."</PARENT>\n";
$this->xml .= "<OWNER>".$row->owner."</OWNER>\n";
$this->xml .= "<CHILDREN>\n";
$hasChildren = $this->has_children($row->id);
//debug
//echo "il Gruppo ".$row->id;
if ($hasChildren){
$this->childrenCounter++;
$this->xml .= $this->get_organigramma($row->id);
}else{
$this->xml .= "</CHILDREN>\n";
$this->xml .= "</GRUPPO>\n";
}
}
for($i=0;$i<$this->childrenCounter;$i++){
$this->xml .= "</CHILDREN>\n</GRUPPO>\n";
}
//$this->childrenCounter = 0;
$this->xml .= "-----------------------------------------------------------\n";
return $this->xml;
}else {
return false;
}
}
function has_children ($id) {
$query = $this->db->query('SELECT * FROM groups WHERE parentId='.$id);
if($query->num_rows > 0) {
return true;
}else{
// gestire l'errore - rollback ?
return false;
}
}