Welcome Guest, Not a member yet? Register   Sign In
More recursive madness
#6

[eluser]Mischievous[/eluser]
Builders:
Code:
var $locations =  array();
var $map = array();
var $count = 0;
function mapKey($keyterm, $array)
    {
        foreach($array as $key => $value)
        {
            if ($keyterm == $key)
            {
                $this->locations[] = $key;
                return TRUE;
            }
           if(is_array($value))
           {
                if($this->mapKey($keyterm, $value) == TRUE)
                {
                    $this->locations[] = $key;
                    return TRUE;
                } else {
                     continue;
                }
          }
           }
    }
function returnKeyValue($key, $array)
    {
        unset($this->locations);
        if($this->mapKey($key, $array) == TRUE){
            $result = array_reverse($this->locations);
            //$result = array_slice($result, 0, count($this->locations)-1);
            return $result;
        } else {
            return "FALSE";
        }
    }
function buildTree($id_field, $parentid_field, $child_field, $array)
    {
        $final = array();
        $temparray = array();
        foreach($array as $arr)
        {
            if($arr[$parentid_field] == 0)
            {
                $final[$arr[$id_field]] = $arr;
            } else {
                if($arr[$child_field] == 1)
                {
                    $temparray[$arr[$parentid_field]][$arr[$id_field]] = $arr;
                } else {
                    $temparray[$arr[$parentid_field]][$arr[$id_field]] = $arr;
                }
            }
        }
        foreach($temparray as $key => $value)
        {
            $map = $this->returnKeyValue($key, $final);
            $mapcount = count($map);
            if(is_array($map))
            {
                $string = "final";
                for($i = 0; $i < $mapcount; $i++)
                {
                        if(is_int($map[$i]))
                        {
                            $string .= "[".$map[$i]."]";
                        } else {
                            $string .= "['".$map[$i]."']";
                        }
                }
                $string .= "['children']";
                eval("\$$string = \$value;");
            }
        }
        return $final;
    }

Display[ers]
Code:
function formatMenu($menu)
    {
          foreach($menu as $m)
        {
            if($m['mi_published'] == 1)
            {
            $this->string .= "<li class='".$m['css_class']."'>";
            $link = "<a >config->slash_item('base_url').$m['href']."' title='".$m['name']."'>".$m['name']."</a>";
            if($this->use_wrapper)
            {
                if($m['parent'] == 0)
                {
                    if(is_array($this->wrapper))
                    {
                        $this->string .= $this->wrapper[0].$link.$this->wrapper[1];
                    } else {
                        $this->string .= $link;
                    }
                } else {
                    $this->string .= $link;
                }
            } else {
                $this->string .= $link;
            }
            if(array_key_exists('children', $m))
            {
                if(is_array($m['children']))
                {
                    $this->string .= "<ul>";
                    $this->formatMenu($m['children']);
                    $this->string .= "</ul>";
                }
            }
            $this->string .= "</li>";
            }
        }
    }
Use
Code:
$menu_items = $this->grabMenu($result->row('id'));
$request = $this->AM->buildTree('id','parent','has_sub', $menu_items);
$this->formatMenu($request);

There is quite a bit going on but what is nice is that the "builders" i set in an array model that handles arrays and all i have to do with any multi-level array i have to build is call out to the array_model. The builder is just a iterative function while the display function is recursive... in this instance i have a a wrapper that can be set but i didnt include that function. Hope this helps out.


Messages In This Thread
More recursive madness - by El Forum - 04-20-2010, 06:11 PM
More recursive madness - by El Forum - 04-20-2010, 07:06 PM
More recursive madness - by El Forum - 04-20-2010, 07:09 PM
More recursive madness - by El Forum - 04-20-2010, 08:55 PM
More recursive madness - by El Forum - 04-21-2010, 03:06 AM
More recursive madness - by El Forum - 04-21-2010, 09:11 AM
More recursive madness - by El Forum - 04-21-2010, 02:05 PM



Theme © iAndrew 2016 - Forum software by © MyBB