[eluser]mddd[/eluser]
For one thing: why are you reading every row separately and then putting them in an array? You do this in both functions. That is unneccessary. You could write your first function as:
Code:
function get_villages()
{
return $this->db->get('villages')->result_array();
}
and it would do exactly the same!
The other thing: you could combine both queries into one, using a JOIN. Thereby making only one query to the database.
Something like
Code:
SELECT * FROM villeges JOIN tasks ON tasks.villageid = villages.id
Then you must do a little extra work to find which rows of the results are about the same village, but that is not too bad. You could do:
Code:
function get_villages_with_tasks()
{
$sql = "SELECT * FROM villeges JOIN tasks ON tasks.villageid = villages.id";
$data = $this->db->query($sql)->result_array();
$list = array();
foreach($data as $row)
{
// add the village to the list if it is not already there
if (!isset($list[$row['id']]))
{
$list[$row['id']] = $row;
$list[$row['id']]['tasks'] = array(); // make a place to store tasks for this new village
}
// now add the task from this row
$list[$row['id']]['tasks'][] = $row;
}
return $list;
}
This list will be an array of villages, each containing an element 'tasks' containing the villages tasks.
I know that the village will also contain the data for the first task, and the tasks will also contain the village info, but I think that is ok. You could clean it up some more if you wanted to.