php - How do I create a category tree, where subcategories can have multiple parents? -
i need create functions return array of categories , subcategories, subcategories can have more 1 parent. have tried solve problem maybe 6 days there's nothing on internet.
here code have created, doesn't work.
private function getallsubs($category, $index, $parent = null) { $subs = $this->database->table('eshop_product_categoryo') ->where('parent', $category->id); $havesub = false; foreach($subs $sub) { $havesub = true; break; } if($havesub) { $maincategory = $this->database->table('eshop_product_categoryo') ->where('category', $category->id); $ismaincategory = true; foreach($maincategory $main) { $ismaincategory = false; break; } $ppp = 0; if(!$ismaincategory) { $ppp = $parent; } $this->somearray[] = array ( 'name' => $category->name, 'parent' => $ppp, 'index' => $index, 'id' => $category->id ); foreach($subs $sub) { $ctgry = $this->database->table('eshop_product_category') ->where('id', $sub->category) ->fetch(); $this->getallsubs($ctgry, ($index+1), $sub->parent); } } }
ok solved... ;-) here version
private function getallsubs($category, $index) { $subs = $this->database->table('eshop_product_categoryo') ->where('otec', $category->id); $hassub = false; foreach($subs $sub){$hassub = true; break;} if($hassub) { $this->somearray[] = array ( 'name' => $category->name, 'index' => $index ); foreach($subs $sub) { $parent = $this->database->table('eshop_product_category') ->where('id', $sub->category) ->fetch(); $this->getallsubs($parent, ($index + 1)); } } else { $this->somearray[] = array ( 'name' => $category->name, 'index' => $index ); } }
Comments
Post a Comment