我正在使用CodeIgniter。我遇到错误
Message: `Invalid argument supplied for foreach()`.
让我们详细解释一下。 下面是我在控制器中使用的代码
$send->customer_id=1;//This is just for information
$fetchdata=$this->Member_model->getAll($send->customer_id);
我得到了输出。我发现与two
1
条记录
Array
(
[0] => stdClass Object
(
[member_id] => 825
[customer_id] => 123
[profile_pic] => sbc.jpg
[first_name] => Shagun
)
[1] => stdClass Object
(
[member_id] =>
[customer_id] =>
[profile_pic] => abfg.jpg
[first_name] => Mayra
)
)
请注意,我没有在第二个数组中获得member_id
和customer_id
。
下面是我得到输出的模型代码
模型
public function getAll($id){
$get_s_member = array('members.is_Approved'=>1,'relation_member.primary_customer_id' =>$id);
$this->db->select('*');
$this->db->from('members');
$this->db->join('relation_member', 'relation_member.secondary_member_id = members.member_id','LEFT');
$this->db->join('relations', 'relations.relations_id = relation_member.relation_with_primary_member','LEFT');
$this->db->join('membership_details', 'members.member_id = membership_details.member_id','LEFT');
$this->db->where($get_s_member);
$query = $this->db->get();
$res = $query->result();
return $res;
}
为什么我会空着,因为membership_details
表中没有记录。我至少需要在成员表中显示所有记录。
我必须在这里传递member_id
$allData['allActivitys']=$this->Member_model->getActivity($member_id);
foreach ($allData['allActivitys'] as $key => $sec_activities) {
}
型号
public function getActivity($gotSecondaryMemberId){
$getDetails = array('members.member_id'=>$gotSecondaryMemberId);
$result = $this->db->where($getDetails)
->select('*')
->from('members')
->join('relation_member', 'relation_member.secondary_member_id = members.member_id')
->join('relations', 'relations.relations_id = relation_member.relation_with_primary_member')
->join('member_activity', 'members.member_id = member_activity.member_id','LEFT')
->join('activity_name', 'activity_name.activity_name_id = member_activity.activity_id','LEFT')
->get()
->result();
if($result)
{
return $result;
}
else
{
return 0;
}
}
但是它只获取第一个数组,而在第二个错误中则是错误消息:Invalid argument supplied for foreach()
。
答案 0 :(得分:1)
您只需要在if
循环之前添加一些foreach
条件
$allData['allActivitys']=$this->Member_model->getActivity($member_id);
if(!empty($allData) && isset($allData['allActivitys'] )){
foreach ($allData['allActivitys'] as $key => $sec_activities) {
//your code goes here
}
}