phpcodeigniter

codeIgniter: pass parameter to a select query from previous query


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!


Solution

  • Agreed with both of the above, join 'em :)

    membership_model function:

    function get_villages_and_tasks() {
      $query_results = $this->db
      ->select('villages.id,villages.name AS villagename,tasks.name AS taskname',false)
      ->join('tasks','tasks.villageid = villages.id','left')
      ->get('villages')->result_array();
    
      $return_array = array();
    
      foreach($query_results as $row) {
        if(!isset($return_array[$row['id']]) {
          $return_array[$row['id']] = array(
            'villagename'=>$row['villagename']
          );
        } 
        if(!empty($row['taskname'])) {
          $return_array[$row['id']]['tasks'][]=array('taskname'=>$row['taskname']);
        }
      }
      return $return_array;
    }
    

    View:

    <?php foreach($villages as $villageid=>$village) : ?>
      <div class="village">
        <h3><?php echo $village['villagename']; ?></h3>
        <ul>
          <?php if(!empty($village['tasks'])): ?>
            <?php foreach($village['tasks'] as $task): ?>
              <li><?php echo $task['taskname']; ?></li>
            <?php endforeach; ?>
          <?php endif; ?>
        </ul>
        <?php echo anchor('site/add_village/'.$villageid.'', '+ add new task'); ?>
      </div>
    <?php endforeach; ?>