![]() |
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:
Die Daten übergebe ich als assoziatives Array. Der Aufruf (erster Datensatz des Baumes (parent_id=0,position=1) sieht dann z. B. so aus:
/**
* 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; } }
Code:
Allerdings wird nur der nächste Knoten kopiert und ich sehe meinen Fehler (PHP sagt: Invalid argument supplied for foreach()) nicht?!
$this->copyTree(array('parent_id' => 0,'item_id' => 40,'template_id' => 6,'position' => 1));
|
AW: Baumstruktur kopieren (MySQL/PHP)
Naja, in deinem Code steht
Delphi-Quellcode:
, aber im anderen Fall gibst du gar nichts zurück – und „gar nichts“ kann nicht per foreach durchlaufen werden.
if($query->num_rows()>0){ ... return ... }
|
AW: Baumstruktur kopieren (MySQL/PHP)
Hallo,
ok das habe ich (oben) angepasst, Fehler weg, aber er kopiert nur die ersten beiden Knoten :? |
AW: Baumstruktur kopieren (MySQL/PHP)
Hallo,
du könntest es mal so versuchen (die Methoden childList und copyNode wie gehabt):
Code:
Der Aufruf dann mit:
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); } } } }
Code:
Gruß
$this->copyTree($this->childList(0));
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 12:55 Uhr. |
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