Einzelnen Beitrag anzeigen

fillibuster

Registriert seit: 3. Nov 2010
Ort: Coesfeld
245 Beiträge
 
Delphi 2010 Professional
 
#11

AW: Baumstruktur kopieren (MySQL/PHP)

  Alt 21. Mai 2013, 10:11
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?!

Geändert von fillibuster (21. Mai 2013 um 10:50 Uhr)
  Mit Zitat antworten Zitat