Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Baumstruktur kopieren (MySQL/PHP) (https://www.delphipraxis.net/174868-baumstruktur-kopieren-mysql-php.html)

fillibuster 21. Mai 2013 10:11

AW: Baumstruktur kopieren (MySQL/PHP)
 
Hallo,

habe jetzt die Lösung mit PHP umgesetzt, aber irgendwo befindet sich noch ein Fehler, daher möchte ich euch noch einmal um Hilfe bitten:
Code:
    /**
     * Copy a single node and return the new id
     *
     * @param $data
     * @return mixed
     */
    public function copyNode($sn_data){
        $this->db2->insert('items_configurations', $sn_data);
        return $this->db2->insert_id();
    }

    /**
     * Return a list of child nodes as an assoziative array
     * from a given parent
     *
     * @param $parent_id
     * @return mixed
     */
    public function childList($parent_id){
        $tmp = 'SELECT parent_id,item_id,template_id,position FROM items_templates WHERE parent_id='.$parent_id;
        $query=$this->db2->query($tmp);
        return $query->result_array();
    }


    /**
     * Copy the whole tree structure through an recursive function
     *
     * @param $node_data
     */
    public function copyTree($node_data){
        $new_parent = $this->copyNode($node_data);
        $new_data  = $this->childList($node_data['parent_id']);
        if(is_array($new_data)){
            foreach($new_data as $new_node_data) :
                $new_node_data['parent_id'] = $new_parent;
                $this->copyTree($new_node_data);
            endforeach;
        }
    }
Die Daten übergebe ich als assoziatives Array. Der Aufruf (erster Datensatz des Baumes (parent_id=0,position=1) sieht dann z. B. so aus:
Code:
$this->copyTree(array('parent_id' => 0,'item_id' => 40,'template_id' => 6,'position' => 1));
Allerdings wird nur der nächste Knoten kopiert und ich sehe meinen Fehler (PHP sagt: Invalid argument supplied for foreach()) nicht?!

Namenloser 21. Mai 2013 10:37

AW: Baumstruktur kopieren (MySQL/PHP)
 
Naja, in deinem Code steht
Delphi-Quellcode:
if($query->num_rows()>0){ ... return ... }
, aber im anderen Fall gibst du gar nichts zurück – und „gar nichts“ kann nicht per foreach durchlaufen werden.

fillibuster 21. Mai 2013 10:51

AW: Baumstruktur kopieren (MySQL/PHP)
 
Hallo,

ok das habe ich (oben) angepasst, Fehler weg, aber er kopiert nur die ersten beiden Knoten :?

Volker Z. 21. Mai 2013 20:11

AW: Baumstruktur kopieren (MySQL/PHP)
 
Hallo,

du könntest es mal so versuchen (die Methoden childList und copyNode wie gehabt):
Code:
  private function setParentId(&$nodes, $value){
    foreach($nodes as &$node)
      $node['parent_id'] = $value;
  }

  public function copyTree($nodes){
    if(is_array($nodes)){
      foreach($nodes as $node){
        $children = $this->childList($node['item_id']);
        if(is_array($children) == false)
          $this->copyNode($node);
        else{
          $this->setParentId($children, $this->copyNode($node));
          $this->copyTree($children);
        }
      }
    }
  }
Der Aufruf dann mit:
Code:
$this->copyTree($this->childList(0));
Gruß


Alle Zeitangaben in WEZ +1. Es ist jetzt 12:55 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz