php - 仅获取 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;
}
}
解决方案
您为每次迭代为 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;
}
}
推荐阅读
- javascript - How to replace string with \r\n on node.js?
- sql-server-2012 - 钻探不返回维度键
- java - LibGdx project setup - Project generated with gradle error
- java - Reports API Google - 日历审核活动事件
- typescript - 禁止在 TypeScript 中创建带大括号的类对象
- javascript - 如何获取 TAG 值/文本(如 innerHTML)?
- ios - 使 UIImageView 圆形而不用它自己的高度划分它
- qt - 如何使 Qt 5.11.2 附带的 QDoc 在 Windows 7 下工作?
- android - Android——一键发送消息
- c# - 我可以在不知道使用 unity3d c# 的唯一密钥 ID 的情况下从 firebase 中删除数据吗?