[eluser]Bart Mebane[/eluser]
Your $data array only saves the last row because it is local to the function and isn't preserved during the recursive calls. The array and its index need to be declared static. (This is edited. I originally made them class properties, but static is better in this case.) The array also needs to be 2-dimensional in order to hold all the categories and the information about each category.
Code:
function getcategorylist($parentid, $space = "") {
static $data = array();
static $index = 0;
$this->db->where('parentid', $parentid);
$query = $this->db->get('categories');
$numrows = $query->num_rows();
if ($numrows > 0) {
$space .= "-";
foreach ($query->result_array() as $row) {
$data[$index]['catid'] = $row['catid'];
$data[$index]['catname'] = $row['catname'];
$data[$index]['dispcatname'] = $space . $row['catname'];
$data[$index]['parentid'] = $row['parentid'];
$index++;
$this->getcategorylist($row['catid'], $space);
}
}
return $data;
}
If you're going to have a lot of categories, it would be much more efficient to just hit the database once and get all the rows, and then loop through them recursively to build the $data array.