dongyun8138 2011-12-14 14:34
浏览 28
已采纳

Codeigniter从概述中的其他表中获取名称

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>
         <td><?=$interaction['gebruikers_id']?></td>
         <td><?=$interaction['gesproken_met']?></td>
         <td><?=$interaction['interactie']?></td>
      </tr>
   <?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->select('*');
    $this->db->where('bedrijf_id', $id);
    $query = $this->db->get('interacties');

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

}
  • 写回答

2条回答 默认 最新

  • doujianqin5172 2011-12-15 08:03
    关注

    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 = 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:

    <?=$interaction['naam']?>
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波