PinoyTech.org

CodeIgniter, Kohana, Mootools, jQuery and CSS

Hierarchical Data in a Relational Database

Posted by teejay on March 10, 2009

A Google Search would produce "30,800,000". There are quite a few people looking for a solution to hierarchical data queries.

I, myself have been looking through it for a while and ended up using the The Adjacency List Model due to the need of my data to be updated once in a while.

Here's the function I used for those in need of a similar function:

function _get_structure($parent = 0, $level = 0)
{
 $this->db->where(array('parent_kb_category' => $parent));
 $query = $this->db->get('categories');
  
 if ($query->num_rows() > 0)
 {
  $result = $query->result();

  foreach ($result as $category)
  {
   $id = $category->parent_kb_category;
   echo str_repeat('_', $level), $category->name;
   $this->_get_structure($category->id, $level+1);
  }
 }
}

Resources:

Categories: How To, Web Development

Tags: codeigniter, sql, php

No Comments

Comments are not allowed