Welcome Guest, Not a member yet? Register   Sign In
Multi Dimensional Array for multi level menu
#1

[eluser]Unknown[/eluser]
Hello,I'm working on a class that will have a function for getting the results and a function that will format the results from a standard array to a multi dimensional array suitable for looping to create a multi level menu.

when i get my results without formatting i get what i expected a single, simple array mixed of level one categories and sub categories. Example Below.

original result:
Quote:http://bit.ly/VFbVUy

Now when i try running the Array through my format function intending to sort the array into categories, sub categories as children. a sample of what i get when using the function is below and a snippet of what i want to achieve but just cant.

What i get:
Quote:http://bit.ly/VFbmtN

What i intended to achieve:
Quote:http://bit.ly/VFelTd

My model functions are as follows.
Code:
/* Get Categories Menu
   ******************************/
function categoryMenu() {
  // Get categories from database.
  $query = $this->db->get('blog_categories');
  
  // Count the number of rows. Must be >= 1.
  if($query->num_rows() >= 1)
   // Return an array of the results.
   $result = $query->result_array();
  
  // Format array to a multi dimensional array based on parent feild.
  //return $this->format($result); // Returns formatted array.
  return $result; // Returns single mixed array.
}

/* Get format single array to multi dimensional array.
   ******************************/
function format($data){

  // Each node starts with 0 children
  foreach ($data as &$menuItem)
      $menuItem['children'] = array();
  
  // If menu item has ParentID, add it to parent's Children array    
  foreach ($data as $ID => &$menuItem)
  {
      if ($menuItem['cat_parent'] != 0)
          $data[$menuItem['cat_id']]['children'][$ID] = &$menuItem;
  }
  
  // Remove children from $data so only top level items remain
  foreach (array_keys($data) as $ID)
  {
      if ($data[$ID]['cat_parent'] != 0)
          unset($data[$ID]);
  }
  
  return $data;

}

My controller function.
Code:
function index() {
  $this->load->model('BlogMdl');
  
  $categories = $this->BlogMdl->categoryMenu();
  
  print_r($categories); // simply prints the array.

This topic is well explained and plenty of examples of my code to help fix the problem i can only help think this will help others in the future better than other posts. Really hope you can help. Been at Google and notepad for days now Sad....

Regards Script Gecko :coolmad:.
#2

[eluser]jmadsen[/eluser]
@Script Gecko

reading fast, I'd say look at:

Code:
$data[$menuItem['cat_id']]['children'][$ID] = &$menuItem;

Your $result is an array of arrays, so I think $menuItem is your row array, and causing your problem.


However, your menu is only going to work one level deep, which may be enough.

Not to sound like a shill for my own stuff, but:

http://www.codebyjeff.com/blog/2012/10/n...hy-library

Has a function:

get_grouped_children($top_id=0)

Fetch all descendent records based on the parent id, ordered by their lineage, and groups them as a multi-dimensional array.

that might help you out. (or you can just steal the bits you need from there :-) )
#3

[eluser]Unknown[/eluser]
The way of being a good programmer is learning other peoples code and learning new and faster methods. This is allot better for me than you telling me what to do now i can see exactly how it works!! Thanks a lot mate!




Theme © iAndrew 2016 - Forum software by © MyBB