多个foreach将第一个数组清空

时间:2018-12-30 05:37:25

标签: php codeigniter-3

我正在使用CodeIgniter。 我正在做的是,我从表中获取所有记录,并将ID传递给模型以获取所有相关记录。因此,在视图页面中,我使用第一个foreach显示所有用户NameEmail,在第二个foreach中显示与ID相关的所有记录。

所以我的输出是

Name: alex
Email: alex@gmail.com

Active Name:poiuy
Active Duration: 3m

Active Name:wer
Active Duration: 6m

Active Name:pojfd
Active Duration: 12m

控制器

$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);

模型

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;    
        }

    }

视图

<?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 }}?>

每个都有两个。foreach ($SActivity as $sec_activities)我得到多个记录,但是在第一个数组中我得到了空记录,然后得到了输出。

例如:$sec_activities中有两条记录。所以我得到的输出就像

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
)

0 个答案:

没有答案