简体   繁体   English

Codeigniter如何从同一表中回显多个INNER JOIN

[英]Codeigniter how to echo multiple INNER JOIN from same table

I've got two tables movie and director , in movie's table I've got two fields: ' director1 ' and ' director2 '. 我有两个表movieDirector ,在电影表中我有两个字段:' director1 '和' director2 '。

With inner join I've match the two table but when I do a foreach of the field 'name' and 'surname' of 'director' I've got only one result, the last one. 使用内部联接,我匹配了两个表,但是当我对“ director”的字段“ name”和“ surname”进行foreach时,我只有一个结果,最后一个。

This is my model: 这是我的模型:

function getLastMoviesId($id)
    {
        $this->db->select('*');
        $this->db->from('movie as m');
        $this->db->join('director as d1', 'd1.id = m.director1');
        $this->db->join('director as d2', 'd2.id = m.director2');
        $this->db->where('m.id', $id);
        $query = $this->db->get();
        if($query->num_rows() > 0){
          return $query->result();
        } else {
          return 0;
        }   
    }

This is my view: 这是我的看法:

<?php foreach ($archivepage as $m): ?>
  <?= html_entity_decode($m->name) ?>
  <?= html_entity_decode($m->surname) ?>
<?php endforeach ?>

Your fields values are being over-written as it contains the same names, use something like this instead. 您的字段值被覆盖,因为它包含相同的名称,请改用类似的名称。

$this->db->select('m.*, d1.name d1name, d1.surname d1surname, d2.name d2name, d2.surname d2surname')
$this->db->from('movie as m');
$this->db->join('director as d1', 'd1.id = m.director1', 'left');
$this->db->join('director as d2', 'd2.id = m.director2', 'left');
$this->db->where('m.id', $id);

in your view now you can access the values like this 在您的视图中,现在您可以访问像这样的值

for first one 对于第一个

->d1name ->d1surname 

for second one 第二个

->d2name ->d2surname 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM