views:

419

answers:

3

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!

A: 

With your current model functions you could iterate the villages, fetching the tasks for each one.

function members_area()
{
    global $site_title;
    $this->load->model('membership_model');

    // Fetch villages
    $villages = $this->membership_model->get_villages();

    if($villages)
    { 
        $tasks = array();

        // Iterate through villages fetching tasks
        foreach ($villages as $village)
        {
            $tasks[$village->id] = $this->membership_model->get_tasks($village->village->id);
        }

        $data['villages'] = $villages;
        $data['tasks'] = $tasks;
    }

    $data['title'] = $site_title." | Your account";
    $data['main_content'] = 'account';
    $this->load->view('template', $data);
}

But this requires N+1 SQL queries, where N is the number of villages. A better solution would be to fetch all data in one query by joining the tasks table on the villages table.

Stephen Curran
A: 

I asked, what I think is a related question, in the Codeigniter forums earlier this week.

http://codeigniter.com/forums/viewthread/151624/

Hope this helps...

someoneinomaha
+1  A: 

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; ?>
Billiam
Nice one on implementing the join and converting the result set from 2D to 3D. This is the way to do it.
Stephen Curran