I am using codeIgniter. I need to create a multidimensional array of the data from the DB nammed college. The database has 3 columns: id, OfID and name. The OfID column contains the ID of the parent of that college. For colleges who don't have any parent have OfID as 0.
The array should contain the name, ID and OfID of colleges who have OfID=0 as the elements of first dimension. For colleges which have OfID!=0 should be put as 2nd (and so on) dimension array for the college whose ID they have as OfID.
I thought to do this recursively , but I am unable to finish this. I know there are a lot of mistakes in this please help.
The model class follows: (the controller calls meth() function)
class Model extends CI_Model
{
var $return_this=array();
function meth()
{
$loop_id=0;
getit($loop_id);
var_dump($return_this);
}
function getit($loop_id)
{
$index=0;
$query = $this->db->query("select * from college where OfID=$loop_id ORDER BY `OfID` ASC;");
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$pass=$row->id;
$temp=getit($pass);
if($temp==0)
$return_this[$loop_id]= $query->result();
}
}
else return 0;
}
}