2011-12-14 14:34



Im getting a list from my database with the following (in my controller):

$view["interactions"] = $this->interacties->getInteractionsId($id);

This gives an array with everything in my database in the table 'interactions'.

Im posting this to my view with the following foreach loop:

<?php if($interactions): ?>
   <?php foreach($interactions as $interaction): ?>
      <tr class="gradeX">
         <td><a href="<?=site_url('interactie/single/'.$interaction['id'])?>"><?=$interaction['datum']?></a></td>
   <?php endforeach; ?>
<?php else: ?>
   <p>Geen interacties</p>
<?php endif; ?>

This works fine, but now I want to change the $interaction['gebruikers_id'] to a name which is in my 'users' table which is called 'naam'. Normaly I would get the id and use another get function. But because this is in a foreach loop I'm not sure how to do this.

This is the function in the Model:

public function getInteractionsId($id) {
    $this->db->where('bedrijf_id', $id);
    $query = $this->db->get('interacties');

    return $query->num_rows() > 0 ? $query-> result_array() : FALSE;

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • doujianqin5172 doujianqin5172 10年前

    If you change your model function to this:

    public function getInteractionsId($id) {
      $this->db->select('interacties.*, gebruikers.naam');
      $this->db->join('gebruikers', 'interacties.gebruikers_id =');
      $this->db->where('bedrijf_id', $id);
      $query = $this->db->get('interacties');
      return $query->num_rows() > 0 ? $query-> result_array() : FALSE;

    You can do this in your view:


    If you are not familiar with the basics of database joins, check out this article.

    点赞 评论 复制链接分享
  • dongmacheng3222 dongmacheng3222 10年前

    Since you are in a view, you should only do presentation stuff, no further database calls.

    You could do this in the controller in an extra foreach loop.

    Still, the best option IMHO is to patch your method getInteractionsId() to join with users table so that you will have both $interaction['gebruikers_id'] and $interaction['users_naam'].

    Whatever you end up to, think of fat models and skinny controllers.

    点赞 评论 复制链接分享