J'essai de creer une classe qui me permetrait de manipuler un arbre de ce type
Il y a une incoherence que je ne comprend pas, pouvez vous me dire ou est mon erreur ?
Voici une exemple :
Class arbre{
// +--> Declaration des variables
var $id;
var $idgauche;
var $iddroit;
var $niveau;
var $titre;
var $nbenfants;
var $enfants;
// +--> Compte les noeuds enfants
function nbenfants($niveau,$idgauche,$iddroit){
// Cherche le nombre de noeuds enfants (niveau+1)
$sql = "
SELECT count(*)
FROM NEW_FAMILLE
WHERE NFM_BG > ".$idgauche."
AND NFM_BD < ".$iddroit."
AND NFM_NV = ".($niveau+1)."";
$rsql = mysql_query($sql);
if (mysql_num_rows($rsql) == 0){
$this->id = -1;
}else{
// Enregistre le nombre d'enfants
$this->nbenfants = mysql_result($rsql, 0, "count(*)");
}
}
// +--> Cherche le neoud racine
function racine(){
// Rechercher la racine (intervalle le plus grand)
$sql = "SELECT *
FROM NEW_FAMILLE
WHERE (NFM_BD - NFM_BG) = (SELECT MAX(NFM_BD - NFM_BG)
FROM NEW_FAMILLE)";
$rsql = mysql_query($sql);
// Si pas de racine
if (mysql_num_rows($rsql) == 0)
{
$this->id = -1;
}else{
for($i=0;$i<mysql_num_rows($rsql);$i++)
{
// Enregistre les resultats
$this->id = mysql_result($rsql, $i, "id");
$this->iddroit = mysql_result($rsql, $i, "NFM_BG");
$this->idgauche = mysql_result($rsql, $i, "NFM_BD");
$this->titre = mysql_result($rsql, $i, "NFM_LIB");
$this->niveau = mysql_result($rsql, $i, "NFM_NV");
// Compte les enfants
$this->nbenfants($this->niveau,$this->idgauche,$this->iddroit);
}
}
}
// +--> Cherche les neouds enfants
function enfants($idgauche,$iddroite,$niveau,$enfant){
// Cherche les enfants du noeud passé en parametre
$sql = "SELECT * FROM NEW_FAMILLE
WHERE NFM_BG > ".$idgauche."
AND NFM_BD < ".$iddroite."
AND NFM_NV = ".($niveau+1)."";
$rsql = mysql_query($sql);
for($i=0;$i<mysql_num_rows($rsql);$i++){
if($enfant == $i){
// Enregistre les resultats
$this->id = mysql_result($rsql, $i, "id");
$this->iddroit = mysql_result($rsql, $i, "NFM_BD");
$this->idgauche = mysql_result($rsql, $i, "NFM_BG");
$this->niveau = mysql_result($rsql, $i, "NFM_NV");
$this->titre = mysql_result($rsql, $i, "NFM_LIB");
// Compte les enfants
$this->nbenfants($this->niveau,$this->idgauche,$this->iddroit);
// Cherche les noeuds enfants de chaque enfant
for($j=0;$j<$this->nbenfants;$j++){
$this->enfants[$j] = new arbre;
$this->enfants[$j]->enfants($this->idgauche,$this->iddroit,$this->niveau,$j);
}
}
}
}
// +--> Parcours de l'arbre complet
function cre_arbre(){
// Rechercher la racine (intervalle le plus grand)
$sql = "SELECT *
FROM NEW_FAMILLE
WHERE (NFM_BD - NFM_BG) = (SELECT MAX(NFM_BD - NFM_BG)
FROM NEW_FAMILLE)";
$rsql = mysql_query($sql);
if (mysql_num_rows($rsql) == 0){
$this->id = -1;
}else{
// Enregistre les resultats
$this->id = mysql_result($rsql, $i, "id");
$this->iddroit = mysql_result($rsql, 0, "NFM_BD");
$this->idgauche = mysql_result($rsql, 0, "NFM_BG");
$this->niveau = mysql_result($rsql, 0, "NFM_NV");
$this->titre = mysql_result($rsql, 0, "NFM_LIB");
// Compte les enfants
$this->nbenfants($this->niveau,$this->idgauche,$this->iddroit);
// Cherche les enfants
for($i=0;$i<$this->nbenfants;$i++){
$this->enfants[$i] = new arbre;
$this->enfants[$i]->enfants($this->idgauche,$this->iddroit,$this->niveau,$i);
}
}
}
}// fin de classe
Lorsque que je fait appel à :
$arbre = new arbre();
$arbre->cre_arbre();
J'obtiens bien l'abre souhaité :Code : Tout sélectionner
arbre Object
(
[id] => 1
[idgauche] => 1
[iddroit] => 44
[niveau] => 1
[titre] => Transport
[nbenfants] => 3
[enfants] => Array
(
[0] => arbre Object
(
[id] => 2
[idgauche] => 2
[iddroit] => 21
[niveau] => 2
[titre] => Aérien
[nbenfants] => 6
[enfants] => Array$arbre = new arbre();
$arbre->racine();
La variable 'nbenfants' n'est pas bonne :Code : Tout sélectionner
arbre Object
(
[id] => 1
[idgauche] => 44
[iddroit] => 1
[niveau] => 1
[titre] => Transport
[nbenfants] => 0
[enfants] =>
)Merci d'avance