codeIgniter: pass parameter to a select query from previous query
- by krike
I'm creating a little management tool for the browser game travian. So I select all the villages from the database and I want to display some content that's unique to each of the villages. But in order to query for those unique details I need to pass the id of the village. How should I do this?
this is my code (controller):
function members_area()
{
global $site_title;
$this->load->model('membership_model');
if($this->membership_model->get_villages())
{
$data['rows'] = $this->membership_model->get_villages();
$id = 1;//this should be dynamic, but how?
if($this->membership_model->get_tasks($id)):
$data['tasks'] = $this->membership_model->get_tasks($id);
endif;
}
$data['title'] = $site_title." | Your account";
$data['main_content'] = 'account';
$this->load->view('template', $data);
}
and this is the 2 functions I'm using in the model:
function get_villages()
{
$q = $this->db->get('villages');
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
function get_tasks($id)
{
$this->db->select('name');
$this->db->from('tasks');
$this->db->where('villageid', $id);
$q = $this->db->get();
if($q->num_rows() > 0) {
foreach ($q->result() as $task) {
$data[] = $task;
}
return $data;
}
}
and of course the view:
<?php foreach($rows as $r) : ?>
<div class="village">
<h3><?php echo $r->name; ?></h3>
<ul>
<?php foreach($tasks as $task): ?>
<li><?php echo $task->name; ?></li>
<?php endforeach; ?>
</ul>
<?php echo anchor('site/add_village/'.$r->id.'', '+ add new task'); ?>
</div>
<?php endforeach; ?>
ps: please do not remove the comment in the first block of code!