0

I'm trying to build a tree with the exact specifications of..

This Question

Basically I need to create a tree from a parent - id table structure. I'm using this function to try to achieve the above;

private static function fetch_recursive($src_arr, $currentid = 0, $parentfound = false, $cats = array())
{
    foreach($src_arr as $row)
    {
        if((!$parentfound && $row['category_id'] == $currentid) || $row['parent_id'] == $currentid)
        {
            $rowdata = array();
            foreach($row as $k => $v)
                $rowdata[$k] = $v;
            $cats[] = $rowdata;
            if($row['parent_id'] == $currentid)
                $cats = array_merge($cats, CategoryParentController::fetch_recursive($src_arr, $row['category_id'], true));
        }
    }
    return $cats;
}

But I'm getting an error from PHP :

Maximum function nesting level of 100 reached, aborting!

I'm ordering the db results by parent_id and then by id to help out with the issue but it still persists.

As a side note by table contains ~250 records.

Community
  • 1
  • 1
jeanfrg
  • 2,366
  • 2
  • 29
  • 40
  • Please don't link to your specifications -- I won't follow any link from SO while I'm in work, and neither will thousands of other users. Further, if the link disappears, then the question makes even less sense and becomes useless for others. Please paste the important bits of what you're trying to achieve inline. – Software Engineer Jul 17 '14 at 19:16
  • Dollery the link point to SO ;) – ins0 Jul 17 '14 at 19:18
  • edited the post.. feel free to add suggestions if the information is not useful enough. Thanks again :) – jeanfrg Jul 17 '14 at 19:32

1 Answers1

1

Finally found a solution that fits my needs! Thanks for all the help everyone and also for the constructive criticism :)

Laravel 4 - Eloquent. Infinite children into usable array?

Solution:

<?php

class ItemsHelper {

    private $items;

    public function __construct($items) {
      $this->items = $items;
    }

    public function htmlList() {
      return $this->htmlFromArray($this->itemArray());
    }

    private function itemArray() {
      $result = array();
      foreach($this->items as $item) {
        if ($item->parent_id == 0) {
          $result[$item->name] = $this->itemWithChildren($item);
        }
      }
      return $result;
    }

    private function childrenOf($item) {
      $result = array();
      foreach($this->items as $i) {
        if ($i->parent_id == $item->id) {
          $result[] = $i;
        }
      }
      return $result;
    }

    private function itemWithChildren($item) {
      $result = array();
      $children = $this->childrenOf($item);
      foreach ($children as $child) {
        $result[$child->name] = $this->itemWithChildren($child);
      }
      return $result;
    }

    private function htmlFromArray($array) {
      $html = '';
      foreach($array as $k=>$v) {
        $html .= "<ul>";
        $html .= "<li>".$k."</li>";
        if(count($v) > 0) {
          $html .= $this->htmlFromArray($v);
        }
        $html .= "</ul>";
      }
      return $html;
    }
}
Community
  • 1
  • 1
jeanfrg
  • 2,366
  • 2
  • 29
  • 40
  • 1
    Would you be able to elaborate on how to include additional attributes to the returned array? @jeanfrg – dcolumbus Sep 01 '14 at 22:28