问题
I am using CodeIgniter.
What I am doing is, I am getting all the records from the table and I am passing the id to the model to get all the related records. So in the view page, I am displaying all users Name
and Email
using the first foreach and in the second foreach I am displaying all the records related to the id.
So my output is,
Name: alex
Email: alex@gmail.com
Active Name:poiuy
Active Duration: 3m
Active Name:wer
Active Duration: 6m
Active Name:pojfd
Active Duration: 12m
Controller
$getDetails['secMember']=$this->Member_model->getSecMemberinfo();//getting all the member details
$getDetails['getSecMemberActivity']=$this->Member_model->getSecMemberActivity($getDetails['secMember']->id);//getting all the details related to the id
$this->load->view('profile',$getDetails);
Model
public function getSecMemberinfo(){
$get_s_member = array('members.member_type' =>2,'members.is_Approved'=>1,'members.is_status'=>1);
$res =$this->db->where($get_s_member);
->from('members');
->join('relation_member', 'relation_member.secondary_member_id = members.member_id');
->join('relations', 'relations.relations_id = relation_member.relation_with_primary_member');
->get();
->result();
return $res;
}
public function getSecMemberActivity($gotSecondaryMemberId){
$getDetails = array('members.member_id'=>$gotSecondaryMemberId);
$result = $this->db->where($getDetails)
->from('members')
->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;
}
}
view
<?php
foreach ($secMember as $secPay) {//first foreach
if($secPay->member_type == 2){?>
<div class="perSet">
<h2><?php echo $secPay->name?></h2>
<h2><?php echo $secPay->email?></h2>
<?php
$SActivity=$getSecMemberActivity;
if ($SActivity){
foreach ($SActivity as $sec_activities) {?> <!--2nd foreach-->
<div><p><?php echo $sec_activities->activeName;?></p></div>
<div><p><?php echo $sec_activities->activeDuration;?></p></div>
<?php }}?>
</div>
<?php }}?>
I have two for each.foreach ($SActivity as $sec_activities)
I am getting the multiple records but in the first array I am getting the empty records then I am getting my output.
for example: I have two records in the $sec_activities
. so I am getting the output like
stdClass Object
( [id] =>
[activeName] =>
[activeDuration] =>
)
stdClass Object
( [id] =>1
[activeName] => qwer
[activeDuration] => 6m
)
stdClass Object
( [id] =>
[activeName] =>
[activeDuration] =>
)
stdClass Object
( [id] =>2
[activeName] => lkjh
[activeDuration] => 3m
)
来源:https://stackoverflow.com/questions/53975520/multiple-foreach-getting-the-first-array-empty