Php 当外键记录不止一条时,如何显示一次用户信息?

Php 当外键记录不止一条时,如何显示一次用户信息?,php,mysql,codeigniter,Php,Mysql,Codeigniter,我正在使用协调器。我有两个带有主键和外键连接的表 用户表 薪资表 emp_id是id的用户表的外键 现在我在编辑页面上,我必须显示用户的单个记录详细信息。例如,用户id为1 我的HTML中有两个部分。第一节显示记录的用户表,第二节显示工资记录 预期输出 <section> <h2>User details</h2> name:Mark image:1512288124.png </section> <

我正在使用协调器。我有两个带有主键和外键连接的表

用户表

薪资表

emp_id是id的用户表的外键

现在我在编辑页面上,我必须显示用户的单个记录详细信息。例如,用户id为1

我的HTML中有两个部分。第一节显示记录的用户表,第二节显示工资记录

预期输出

   <section>
   <h2>User details</h2>
   name:Mark
   image:1512288124.png
   </section>

   <section>
   <h2>Salary details</h2>
   sal_id:1
   salary_amt:100.00
   emp_id:1

   sal_id:3
   salary_amt:1500.00
   emp_id:1

   </section>
查看

    <?php foreach ($get_single_emp_records as $post){?>
      <section>
    <td>Name:<?php echo $post->name ?></td>
    <td>Image<?php echo $post->image ?></td>
    </section>

    <section>
    <td>Sal_id:<?php echo $post->Sal_id ?></td>
    <td>Salary_amt:<?php echo $post->Salary_amt ?></td>
    <td>emp_id:<?php echo $post->emp_id ?></td>
    </section>

  <?php }?>

姓名:
形象
Sal_id:
工资金额:
环境管理计划编号:
按组使用

public function fetch_single_emp_records($edit_key_id)
{
    $get_single_emp_record = array('tbl_user.id' => $edit_key_id);
    $this->db->select('*');
    $this->db->from('tbl_user');
    $this->db->group_by('tbl_salary.emp_id');
    $this->db->join('tbl_salary ', 'tbl_user.id = tbl_salary.emp_id', 'left');
    $this->db->where($get_single_emp_record);
    $query = $this->db->get();
    $result = $query->result();
      if($result)
      {
        return $result;
      }
      else 
      {
        return 0;
      } 
}
另一种方法是使用row()而不是result(),如:


因为row()只返回单个记录。

我不建议在单个联接查询中获取员工详细信息和薪资详细信息。在一段时间内,这两个表都会有大量的行,执行联接查询会花费很多时间。我建议使用两个查询,一个用于获取员工详细信息,另一个用于获取该特定员工的工资详细信息。在同一个循环中,您有两个
标记,因此您当然会两次获取该员工的姓名和图像。您需要检查员工id是否已更改,以及是否未禁止名称的
。@SynapseIndia,是的,这是正确的,它将有大量行。那么加入不好?为什么?考虑到这种情况,我不推荐join,因为从salary table中,您只需要一行[很可能是最新的一行],因此与另一个答案中建议的join和group by相比,使用两个查询会更快。当表中有大量行时,效果会更明显。@SynapseIndia,是的,在你的想法的帮助下,我找到了我的解决方案。我只是想知道,如果我在salary表中找到了记录,那么我将得到输出,但是如果emp_id的数据库中没有记录,那么我将得到错误消息:为foreach()提供的参数无效。它来自salary表,我只得到一行。它应该显示为两行。正确的?
    <?php foreach ($get_single_emp_records as $post){?>
      <section>
    <td>Name:<?php echo $post->name ?></td>
    <td>Image<?php echo $post->image ?></td>
    </section>

    <section>
    <td>Sal_id:<?php echo $post->Sal_id ?></td>
    <td>Salary_amt:<?php echo $post->Salary_amt ?></td>
    <td>emp_id:<?php echo $post->emp_id ?></td>
    </section>

  <?php }?>
public function fetch_single_emp_records($edit_key_id)
{
    $get_single_emp_record = array('tbl_user.id' => $edit_key_id);
    $this->db->select('*');
    $this->db->from('tbl_user');
    $this->db->group_by('tbl_salary.emp_id');
    $this->db->join('tbl_salary ', 'tbl_user.id = tbl_salary.emp_id', 'left');
    $this->db->where($get_single_emp_record);
    $query = $this->db->get();
    $result = $query->result();
      if($result)
      {
        return $result;
      }
      else 
      {
        return 0;
      } 
}
$result = $query->row();