Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Producing a hierarchial listing from simple table
#1

[eluser]johnnytoobad[/eluser]
I'm slowly learning CI and things as going well as a newbie. I've managed basic select, update, delete etc code. I'm working on a learning some code for a basic '"categories" system. I went back and found a snippet I always trusted when i worked in plain php:
Code:
$result = somedb_query("SELECT id, parent_id, name FROM mytable ORDER BY name");

while( $row = somedb_fetch_object($result) )  {
  $tmpAry[$row->parent_id][$row->id] = $row->name;
}

sortCategories($tmpAry, $ary, 0, "");

function sortCategories(&$inAry, &$outAry, $level, $indent)  {
  if( is_array($inAry[$level]) )  {
    while( list($id, $name) = each($inAry[$level]) ) {
      $outAry[$id] = "$indent $name";                
       sortCategories($inAry, $outAry, $id, "$indent $name -- ");
    }
  }
}

I attempted to rewrite this in CI and got this far before my head started spinning:
Code:
$this->db->order_by("name", "asc");
$query = $this->db->get('mytable');

foreach ($query->result() as $row)
{
    $tmpAry[$row->parent_id][$row->id] = $row->name;
}

sortCategories($tmpAry, $ary, 0, "");
function sortCategories(&$inAry, &$outAry, $level, $indent)  {
  if( is_array($inAry[$level]) )  {
    while( list($id, $name) = each($inAry[$level]) ) {
      $outAry[$id] = "$indent $name";                
       sortCategories($inAry, $outAry, $id, "$indent $name -- ");
    }
  }
}

I'm stuck on the part that runs the sortCategories function, I'm thinking maybe that I haven't quite grasped the whole MVC thing and that I shouldn't be putting all that code in the controller. Can someone point me in the right direction as to how to achieve this in CI. Many Thanks




Theme © iAndrew 2016 - Forum software by © MyBB