nested sets: mehrere bäume öffnen/schließen

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • nested sets: mehrere bäume öffnen/schließen

    Hallo,
    mit folgendem Code kann ich Teilbäume eines aktiven Links ($_GET['id']) öffnen. Wenn ich nun einen anderen Link anklicke, schließt sich der vorige und öffnet sich der aktuelle. Nun will ich, dass ich mehrere Bäume gleichzeit öffnen kann. Dazu befüllt ich einen Array mit id - Werten. Wie aber bringe ich das in den funktionen unter? Hat jemand eine Idee?

    PHP Code:
    public function getTreePart($id) {
            
    $sql "SELECT n." $this->pk ", n." $this->name ", COUNT(*)-1 AS level,  ROUND ((n.rgt - n.lft - 1) / 2) AS children FROM " $this->table " AS n, " $this->table " AS p WHERE n.lft BETWEEN p.lft AND p.rgt GROUP BY n.lft ORDER BY n.lft;";
            
    $result $this->db->query($sql);
            if (
    $this->db->affected_rows == 0) {
                return 
    $this->error(1true);
            }
            
    $tree = array();
            
    $i 0
            
    $level count($this -> getPath($id));
        
            while (
    $row $result->fetch_assoc()) {            
                
    $last_id $this -> getLastId($id,$row['level']); // $last_id und $id 
                            
    muss nach Möglichkeit mehrere Werte parallel beinhalten
             
    if($row['level'] == OR ($level $row['level'] AND 
                     
    in_array($row[$this->pk],$this -> getForwardTree($last_id))) 
                    OR (
    $level == $row['level'] AND is_array($this -> getForwardTree($id)) 
                   AND 
    in_array($row[$this->pk], $this -> getForwardTree($id))))  {
         
                
    $tree[$i] = $row;
                
    $i++;
            
                }     
            }
        
            return 
    $tree;
        }


    public function 
    getForwardTree($id) {     

            
    $sql "SELECT n." $this->pk "
                  FROM " 
    $this->table " as n
                  JOIN " 
    $this->table " as p
                  LEFT JOIN " 
    $this->table " as q 
                      ON q.lft < n.lft
                        AND q.rgt > n.rgt
                        AND q.lft > p.lft
                        AND q.rgt < q.rgt
                  WHERE p." 
    $this->pk " = " $id "
                         AND n.lft > p.lft
                        AND n.rgt < p.rgt
                   GROUP BY n.lft
               ORDER BY 
                      n.lft;"
    ;
            
    $result $this->db->query($sql);
            if (
    $this->db->affected_rows == 0) {
                return 
    $this->error(1true);
            }
            
    $tree = array();
            
    $i 0;
             
            while (
    $row $result->fetch_assoc()) {
              
                
    $tree[$i] = $row[$this->pk];
                
    $i++;
                
            }                           
            return 
    $tree;
        } 
    Last edited by sanktusm; 24-02-2012, 16:07.

  • #2
    Alternativer Lösungsansatz

    Zugegeben, war der erste Ansatz etwas kompliziert, es hat zwar fast geklappt aber eben nur fast. Der folgende Ansatz gibt alle punkte aus, die geöffnet sind. Nun möchte ich auch diejenigen Punkte geöffnet wissen, die innerhalb der nächsten und der ebene liegen. Hat jemand eine Idee?

    PHP Code:
        public function getTreePart() {
            
    $sql "SELECT n." $this->pk ", n." $this->name ", COUNT(*)-1 AS level,  ROUND ((n.rgt - n.lft - 1) / 2) AS children FROM " $this->table " AS n, " $this->table " AS p WHERE n.lft BETWEEN p.lft AND p.rgt GROUP BY n.lft ORDER BY n.lft;";
            
    $result $this->db->query($sql);
            if (
    $this->db->affected_rows == 0) {
                return 
    $this->error(1true);
            }
            
    $tree = array();
            
    $i 0

          
    $this -> section_handler(0,0,0); 

            while (
    $row $result->fetch_assoc()) {            
             if(
    $this -> open_sections[$row[$this->pk]] != $row[$this->pk]) {
         
         
    $lft $this -> getActiveLft($row[$this->pk]);
         
    $rgt $this -> getActiveRgt($row[$this->pk]);
         } 
         
             if(!
    in_array($this -> getActiveLft($row[$this->pk]), range($lft$rgt )))  {
                
    $tree[$i] = $row;
                
    $i++;
          }     
            }
          
            return 
    $tree;
        } 

    Comment


    • #3
      Neuer Lösungsansatz

      Folgender Ansatz gibt leider alle Menupunkte aus, wenn anstelle von p." . $this->pk . " IN ... eine einzelne Id angegeben wird, wird nur der Teilbaum unterhalb der aktiven Id ausgeben. Ich bitte um unterstützung.

      PHP Code:
      public function getForwardTree() {     

      $count_open_sections 0;
      $open_path "p." $this->pk " IN (";

      foreach(
      $this -> open_sections AS $id) {
      $open_path .= " '" $id "' ";
      $count_open_sections++;
      if(
      count($this -> open_sections) == $count_open_sections)
      $open_path .= " ";
      else
      $open_path .= " , "
      }
      $open_path .= " ) ";
              
      $sql "SELECT n." $this->pk "
                    FROM " 
      $this->table " as n
                    JOIN " 
      $this->table " as p
                    LEFT JOIN " 
      $this->table " as q 
                        ON q.lft < n.lft
                          AND q.rgt > n.rgt
                          AND q.lft > p.lft
                          AND q.rgt < q.rgt
                    WHERE " 
      $open_path "
                           AND n.lft > p.lft
                          AND n.rgt < p.rgt
                     GROUP BY n.lft
                 ORDER BY 
                        n.lft;"
      ;
              
      $result $this->db->query($sql);
              if (
      $this->db->affected_rows == 0) {
                  return 
      $this->error(1true);
              }
              
      $tree = array();
              
      $i 0;
               
              while (
      $row $result->fetch_assoc()) {
                
                  
      $tree[$i] = $row[$this->pk];
                  
      $i++;
                  
              }                           
              return 
      $tree;
          } 

      Comment


      • #4
        @sanktusm

        Bitte brich deinen Quellcode um! Kein Mensch schaut sich das an, wenn man kilometerweit horizontal nach rechts scrollen muss.

        Danke
        Peter
        Nukular, das Wort ist N-u-k-u-l-a-r (Homer Simpson)
        Meine Seite

        Comment


        • #5
          letzer Lösungsansatz

          Hallo, dieser Lösungsansatz bringt die beste Lösung. Das Problem allerding:
          es wird immer die ganze Evene unterhalb des Knotenpunkt geöffnet.

          Da ich jetzt schon eine ganze Weile daran arbeite, bin ich über jeden Tipp sehr dankbar.

          PHP Code:
          public function getTreePart() {
                  
          $sql "SELECT n." $this->pk ", n." $this->name ", COUNT(*)-1 AS level,  ROUND ((n.rgt - n.lft - 1) / 2) AS children FROM " $this->table " AS n, " $this->table " AS p WHERE n.lft BETWEEN p.lft AND p.rgt GROUP BY n.lft ORDER BY n.lft;";
                  
          $result $this->db->query($sql);
                  if (
          $this->db->affected_rows == 0) {
                      return 
          $this->error(1true);
                  }
                  
          $tree = array();
                  
          $i 0
                  
          $arr_rgt = Array();
                
          $this -> section_handler(0,0,0); 
                  while (
          $row $result->fetch_assoc()) {            
                   if(
          $this -> open_sections[$row[$this->pk]]) {
                   
          $level count($this -> getPath($row[$this->pk]));
               
          $last_id $this -> getLastId($row[$this->pk],$row['level']);  
               
          $id $row[$this->pk];
               
          $rgt $this -> getActiveRgt($row[$this->pk]);
               }
                 
                   if((
          $level $row['level'] AND 
                          
          $rgt $this -> getActiveLft($last_id)) OR 
                         (
          $level == $row['level'] AND 
                         
          is_array($this -> getForwardTree($id)) AND 
                         
          in_array($row[$this->pk], $this -> getForwardTree($id))))  {
               
                      
          $tree[$i] = $row;
                      
          $i++;
                }     
                  }
                
                  return 
          $tree;
              }

          public function 
          getIdfromRgt($lft) {     

          $sql "SELECT n." $this->pk " FROM " $this->table " WHERE p.lft = '$rgt'";

          $result $this->db->query($sql);
                  if (
          $this->db->affected_rows == 0) {
                      return 
          $this->error(1true);
                  }
              
                
          $row $result->fetch_assoc(); 
                
          $id $row[$this->pk];
                    
              
              
                                        
                  return 
          $id;
              }
          public function 
          getForwardTree($id) {     

                  
          $sql "SELECT n." $this->pk "
                        FROM " 
          $this->table " as n
                        JOIN " 
          $this->table " as p
                        LEFT JOIN " 
          $this->table " as q 
                            ON q.lft < n.lft
                              AND q.rgt > n.rgt
                              AND q.lft > p.lft
                              AND q.rgt < q.rgt
                        WHERE p." 
          $this->pk " = " $id "
                               AND n.lft > p.lft
                              AND n.rgt < p.rgt
                         GROUP BY n.lft
                     ORDER BY 
                            n.lft;"
          ;
                  
          $result $this->db->query($sql);
                  if (
          $this->db->affected_rows == 0) {
                      return 
          $this->error(1true);
                  }
                  
          $tree = array();
                  
          $i 0;
                   
                  while (
          $row $result->fetch_assoc()) {
                    
                      
          $tree[$i] = $row[$this->pk];
                      
          $i++;
                      
                  }                           
                  return 
          $tree;
              } 

          Comment

          Working...
          X