首页 > 解决方案 > 仅获取 id 的最后一个结果

问题描述

我正在使用 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;
    }         
}

标签: phpcodeigniter-3

解决方案


您为每次迭代为 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;
  }

}


推荐阅读