Récursivité

cs_eltyty Messages postés 86 Date d'inscription mercredi 31 janvier 2007 Statut Membre Dernière intervention 22 novembre 2011 - 21 déc. 2009 à 18:42
cs_eltyty Messages postés 86 Date d'inscription mercredi 31 janvier 2007 Statut Membre Dernière intervention 22 novembre 2011 - 29 déc. 2009 à 16:47
Bonjour,

Je souhaite faire une boucle récursive afin d'afficher mes rubriques et sous rubriques. Mais pour éviter une trop grande longueur de page je souhaiterai faire ça en deux colonnes quand le nombre de rubrique est trop important.
Voici ma table :
id_rubrique id_parent last (j'indique "oui" quand c'est la dernière rubrique)

Pour ma boucle voici le code ci-dessous qui fonctionne en fonction d'un id passé dans l'url :
CLASS Affichage_recap_rubrique	{
public $liste;
public $erreur;
FUNCTION __construct() {

}



public function Liste_rubriques($parent,$profondeur) {
$connexion =  SPDO::getInstance();


$sql =' SELECT id_rubrique,id_parent,id_groupe,last, titre,archive, statut,descriptif FROM rubriques WHERE id_parent= ? AND id_groupe!="user" AND archive="'.VISUALISER_ARCHIVE.'"';
$sql .=' OR id_parent= ? AND id_groupe="user"';


try{
$select=$connexion->prepare($sql);

$select->bindParam(1,$parent, PDO::PARAM_INT);
$select->bindParam(2,$parent, PDO::PARAM_INT);
}
catch (PDOException $e)        {
throw new myException('la requête préparée est incorrecte');
exit();
}			 
 
$select->execute();
$nb = $select->rowCount();

IF ($nb===0){
RETURN FALSE;
}
//SI DES ENREGISTREMENTS EXISTE
IF ($nb>0) {
WHILE ($fils=$select->fetch(PDO::FETCH_OBJ)) {
$this->titre=$fils->titre;
$this->id_rubrique=$fils->id_rubrique;
$this->id_parent=$fils->id_parent;
$this->last=$fils->last;

//MET UNE MARGE À 0 POUR LA RUBRIQUE PRINCIPALE ET APRÈS ÇA VA DE 40 EN 40
$espace=40*$profondeur;
//SI 'EST LA DENIÈRE RUBRIQUE
//ON MET LE LIEN PUR AFFICHER LA LISTE DES INFOS D'OÙ ACTON=LISTER
IF ($this->last===oui){

$this->liste.= '
[./index.php?p=articles&rub='.$this->id_rubrique.'&action=lister '.$this->titre.']';
$this->liste.= '
';
}
//SI C'EST PAS LA DERNIERE RUBRIQUE
ELSE{

$this->liste.= '
[./index.php?p=articles&rub='.$this->id_rubrique.'&action=recap&nom='.Filtres::enlever_accents($this->titre).' '.$this->titre.']';
$this->liste.= '
';
}
$this->Liste_rubriques($this->id_rubrique,$profondeur + 1);
}
}
}
/*******************************************************************
OBTIENT LE NOM DE LA RUBRIQUE DANS LE GET ET
SON ID_APRENT QUE L'ON ENVOIE À LA FONCTION LISTE_RUBRIQUES
******************************************************************/
PUBLIC function set_rubrique($g_id_rub=NULL){

IF (!EMPTY($g_id_rub)){
$connexion = SPDO::getInstance();
$sql =' SELECT id_parent,titre FROM rubriques WHERE id_rubrique= ? AND id_groupe!="user" ';
$sql .=' OR id_rubrique= ? AND id_groupe="user"';					

try{
$select=$connexion->prepare($sql);
$select->bindParam(1,$g_id_rub, PDO::PARAM_INT);
$select->bindParam(2,$g_id_rub, PDO::PARAM_INT);
}
catch (PDOException $e)        {
throw new myException('la requête préparée est incorrecte');
exit();
}
$select->execute();
$nb_tt =  $select->rowCount();

IF ($nb_tt>0){
$fils=$select->fetch(PDO::FETCH_OBJ);
$g_titre=$fils->titre;
$id_parent=$fils->id_parent;

$this->titre_rubrique= 'Sommaire de '.$g_titre;
//SI ON EST À LA RACINE ON NE MET PAS LE LIEN CAR ON PEUT PAS REMONTER PLUS HAUT
IF ($id_parent>0){

$this->lien=' [./index.php?p=articles&rub='.$id_parent.'&action=recap   ../ ]

';
}
$this->Liste_rubriques($g_id_rub,0);
}
//SI LA RUBRIQUE NE RETOURNE AUCUN ENREGISTREMENT
ELSE{
$this->titre_rubrique = "<center>Aucun élément ne peut être affiché
</center>";
}
}
ELSE {
ECHO 'Valeur incorrecte';
}
}
}



Cette classe marche mais bien sûr tout est en ligne. Et comme la fonction est récursive je ne vois pas comment je peux faire en sorte que ça s'affiche en deux colonnes par exemple. Voici ce que ça donne :
 BAT  
   Liens
   témoignages
 JAVA 
   exemples
      Bout de codes
   témoignages
   
 SQL  
   exercices
      tests
   exemples


Donc si quelqu'un à partir de ce code peut me montrer, merci beaucoup d'avance.

21 réponses

cs_eltyty Messages postés 86 Date d'inscription mercredi 31 janvier 2007 Statut Membre Dernière intervention 22 novembre 2011
29 déc. 2009 à 16:47
En fait pour cet exempole :
 BAT 
    Liens
    témoignages
 JAVA 
    exemples
    témoignages
    Bout de codes
 SQL 
   exercices
   exemples

 PHP 
   Exemples 
   Partiel
      Bruno
      Seb
   complets

Là on voit bien 4 rubriques de niveau 1 donc je souhaiterai obtenir qq chose comme ça :
BAT                 SQL
    Liens             exercices
    témoignages       exemples
 JAVA               PHP
    exemples          exemples
    témoignages        partiel
    Bout de codes         bruno
                          seb

Tu vois il s'agit de mettre en deux colonnes afin d'éviter de devoir trop descendre dans la page mais le fractionnement doit se faire uniquement sur les rubriques principales (BAT, JAVA, SQL, PHP comme dans l'exemple ci-dessus).
Ai-je été assez clair ?
J'espère donc encore une fois merci pour cette motivation.
0
Rejoignez-nous