简体   繁体   English

我无法显示来自具有不同记录的两个不同表中的所有数据

[英]I am having trouble to show all data from two different tables which are having different records

For information in using codeigniter 有关使用Codeigniter的信息

I have two tables: 我有两个表:

  1. table A has 4 records picture 1 表A有4条记录, 图1
  2. table B has 5 records picutre 2 表B有5条记录picutre 2

I want to show that records from 2 tables I have above to my view but in my view, it just shows 4 records. 我想显示上面有2个表的记录,但在我看来,它仅显示4条记录。

  1. 4 records from table A 表A中的4条记录
  2. 4 records from table B 表B中的4条记录

Even though in table B has 5 records it just shows 4 records 即使表B中有5条记录,它也只显示4条记录

This is my code : 这是我的代码:

Model 模型

function getAlldata(){
   $this->db->select('*');
   $this->db->from('data_skm_organisasi'); 
   $this->db- >JOIN('data_skm_kejuaraan','data_skm_kejuaraan.id=data_skm_organisasi.id');
   $results = $this->db->get();     
   return $results->result();
}   

controller 调节器

public function V_home()
{
   $dataSimpanOrganisasi = array();
   $dataSimpanOrganisasi ['organisasi']= $this->M_main->getAlldata(); ;
   $this->load->view('V_mahasiswa',$dataSimpanOrganisasi);
} 

view 视图

<?php foreach ($organisasi as $dataSimpan) { ?>
     <div>
          <?php echo $dataSimpan->Lembaga; ?>
          <?php echo $dataSimpan->tingkatan_O; ?><br><br>
     </div>
          <?php } 
?><br><br>

<?php foreach ($organisasi as $dataSimpan) { ?>
      <div>
          <?php echo $dataSimpan->jenisKegiatan; ?>
          <?php echo $dataSimpan->tingkatan; ?><br><br>
      </div>
          <?php } 
?>

what should I do to my code? 我应该对我的代码做什么? is there is any problem in my code? 我的代码有什么问题吗?

You are using an normal $db->join command, which is an inner join. 您正在使用普通的$db->join命令,这是一个内部联接。 This is why you only see the rows of table B, when there is a corredsponding row in table A. In your case, it seems you need an outer join, not an inner join. 这就是为什么当表A中有对应的行时,您只看到表B的行的原因。在您的情况下,似乎您需要一个外部联接,而不是一个内部联接。 you can specify it by adding a parameter to your join statement: 您可以通过在连接语句中添加参数来指定它:

$this->db->join('data_skm_kejuaraan', 'data_skm_kejuaraan.id=data_skm_organisasi.id', 'right outer');

I think you used the wrong column to join on the data_skm_kejuaraan table. 我认为您在data_skm_kejuaraan表上使用了错误的列。 Try change : 尝试更改:

$this->db->JOIN('data_skm_kejuaraan','data_skm_kejuaraan.id=data_skm_organisasi.id');

to

$this->db->JOIN('data_skm_kejuaraan','data_skm_kejuaraan.id_juara=data_skm_organisasi.id');

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

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