仅获取ID的最后结果

时间:2018-11-14 09:41:16

标签: php codeigniter-3

我正在使用CodeIgniter,模型中出现了一些问题。我只得到ID的最后结果。

例如$get_id=2,它将获取与team_id 2相关的人员的记录并显示。所以我得到了输出5,7,8

现在我将我的输出(5,7,8)传递给foreach以从另一个表获取记录,但是问题是我正在获取id(8)的最后结果。

模型

public function getCSRList($get_id){
    $this->db->select('id');
    $this->db->from('tbl_employee');
    $this->db->where('team_id', $get_id);
    $this->db->where('is_archive',0);
    $query = $this->db->get();
    $result = $query->result();
    if($result)
      {
          foreach($result as $id){
            //print_r($id->id);// getting more then one id
                $this->db->select('*');
                $this->db->from('tbl_customer');
                $this->db->where('created_by',$id->id);
                $query = $this->db->get();
                $result_2 = $query->result();

          }
          return $result_2;
      }
      else 
      {
        return 0;
      }
}

答案后的编辑代码

控制器

public function order_listdetails(){
$send_id =$this->session->userdata['login_session']['id'];// section already set
$order_list=$this->Customer_model->getCSRList($send_id);
if(is_array($order_list)){
    foreach($order_list as $r) { 
    //foreach($order as $r) {// 
        $encryption_id=base64_encode($this->encryption->encrypt($r->cust_id));
        $fname=$r->firstname;
        // more here
          }
    }
//}
}

模型 在模型中,它将检查三个访问角色条件4,3,否则。 4,其他正在工作。我在3种elseif条件下遇到问题。

public function getCSRList($get_id){
 if ($this->session->userdata['login_session']['access_role']==4) {
            $this->db->select('*');
            $this->db->from('tbl_customer');
            $this->db->where('created_by',$get_id);
            $query = $this->db->get();
            $result_4 = $query->result();
                return $result_4; 
 }
  elseif ($this->session->userdata['login_session']['access_role']==3) {
          $this->db->select('id');
          $this->db->from('tbl_employee');
          $this->db->where('team_leadername', $get_id);
          $this->db->where('is_archive',0);
          $query = $this->db->get();
          $result = $query->result();
              if($result)
                {
              $result_2= array();
              foreach($result as $id){
                      $this->db->select('*');
                      $this->db->from('tbl_customer');
                      $this->db->where('created_by',$id->id);             
                      $query = $this->db->get();
                      $result_2[] = $query->result();
              }
              return $result_2;
                }
                else 
                {
                  return 0;
                }

  }
 else{
      $this->db->select('*');
      $this->db->from('tbl_customer');
      $query = $this->db->get();
      $result = $query->result();
      return $result;
    }         
}

2 个答案:

答案 0 :(得分:1)

您正在为每次迭代的result_2分配一个新值。改用数组存储结果。

public function getCSRList($get_id){
$this->db->select('id');
$this->db->from('tbl_employee');
$this->db->where('team_id', $get_id);
$this->db->where('is_archive',0);
$query = $this->db->get();
$result = $query->result();
if($result)
  {
      //define your array 
      result_2= array();
      foreach($result as $id){
            $this->db->select('*');
            $this->db->from('tbl_customer');
            $this->db->where('created_by',$id->id);
            $query = $this->db->get();
            //store results in result_2 array
            $result_2[] = $query->result();

      }
      // return result_2 after the foreach loop
      return $result_2;
  }
  else 
  {
    return 0;
  }

}

答案 1 :(得分:0)

您每次在foreach循环中都为$ result_2分配新值。您应该使用数组并将其返回

public function getCSRList($get_id){
$this->db->select('id');
$this->db->from('tbl_employee');
$this->db->where('team_id', $get_id);
$this->db->where('is_archive',0);
$query = $this->db->get();
$result = $query->result();
if($result)
  {
      $result_2 = [];
      foreach($result as $id){
        //print_r($id->id);// getting more then one id
            $this->db->select('*');
            $this->db->from('tbl_customer');
            $this->db->where('created_by',$id->id);
            $query = $this->db->get();
            $result_2[] = $query->result();

      }
      return $result_2;
  }
  else 
  {
    return 0;
  }
}