记录在查看页面上获得两次

时间:2019-02-23 12:05:37

标签: php mysql codeigniter-3

我正在使用CodeIgniter。我在查看页面上获得了两次记录。

$data['upcomingForsecondary']=$this->Access_model->upcomingsecondary($data['getLogininfo']->customer_id);

获取输出

Array
(
    [0] => stdClass Object
        (
            [member_id] => 337
            [first_name] => zxs
            [middle_name] => 
            [last_name] => asd
            [email] => qwe@gmail.com
            [dob] => 22-04-1984
            [phone] => 1231231231
            [landline] => 
            [membershipForTheYear] => 2018-2019
        )

    [1] => stdClass Object
        (
            [member_id] => 209
            [first_name] => sdf
            [middle_name] => 
            [last_name] => asd
            [email] => asdas@gmail.com
            [dob] => 24-07-1982
            [phone] => 1231231231
            [landline] => 
            [membershipForTheYear] => 2018-2019
        )

)

现在,我使用foreach传递模型的MembershipForTheYear来获取记录。

控制器代码

$data['upcomingForsecondary']=$this->Access_model->upcomingsecondary($data['getLogininfo']->customer_id);

foreach ($data['upcomingForsecondary'] as $key => $sec_m_id) {
  $secClubfees[]=$this->Access_model->getfeesFornewyear($sec_m_id->membershipForTheYear);
 }
 $data['newFees'] = $secClubfees;

** print_r($ data ['newFees'])输出**

Array
(
    [0] => Array
        (
            [0] => stdClass Object
                (
                    [clubMembershipFees_id] => 1
                    [clubDuration] => 2019-2020
                    [clubPrimaryMemberFees] => 100
                    [startCutoffDate] => 16-02-2019
                    [endCutoffDate] => 31-03-2019
                    [is_clubFeesActive] => 1
                )

        )

    [1] => Array
        (
            [0] => stdClass Object
                (
                    [clubMembershipFees_id] => 1
                    [clubDuration] => 2019-2020
                    [clubPrimaryMemberFees] => 100
                    [startCutoffDate] => 16-02-2019
                    [endCutoffDate] => 31-03-2019
                    [is_clubFeesActive] => 1

                )

        )

)

查看代码

I tried on view like 

 foreach ($upcomingForsecondary as $key => $secPersonalInfo) {
   /*Displaying personal information which is dislaying perfeclty*/

   foreach ($newFees as $key => $value) {
     foreach ($value as $key => $rows) {

      /*getting issue here. I a getting the twice output*/


              }

      }


}

查看我正在得到的输出

first member name
fees details
fees details

second member name
fees details
fees details

我使用了多个foreach来解决问题。我认为视图页面或控制器上存在一些问题。

在这个问题上您能帮我吗?

1 个答案:

答案 0 :(得分:1)

一无所知,这是怎么回事-而且没有codeigniter的提示:) ..

foreach ($data['upcomingForsecondary'] as $key => $sec_m_id) {
    $secClubfees[]=$this->Access_model->getfeesFornewyear($sec_m_id->membershipForTheYear);
}

在这里,您将两个结果放在一个数组中,并失去了与$data['upcomingForsecondary']中对象的关系。

然后在视图中

foreach ($upcomingForsecondary as $key => $secPersonalInfo) {
    // Displaying personal information
    foreach ($newFees as $key => $value) {
        // show the rows
    }
}

您将两个结果在newFees中循环两次($upcomingForsecondary中每个对象一次)。

因此,您需要使用$key$secPersonalInfo将内部循环与外部循环相关联。我可以建议三种方法。

#1希望键相同:

foreach ($upcomingForsecondary as $key1 => $secPersonalInfo) {
    // Displaying personal information
    foreach ($newFees[$key1] as $key2 => $rows) {
        // show the row
    }
}

#2确保密钥相同:

foreach ($data['upcomingForsecondary'] as $key => $sec_m_id) {
    $secClubfees[$key] = $this->Access_model->getfeesFornewyear($sec_m_id->membershipForTheYear);
}

然后使用#1中的查看代码

#3嵌套结果:

控制器:

foreach ($data['upcomingForsecondary'] as $key => $sec_m_id) {
    $sec_m_id->newFees
        = $this->Access_model->getfeesFornewyear($sec_m_id->membershipForTheYear);
}

在这种情况下,您不需要$data['newFees']

查看:

foreach ($upcomingForsecondary as $key1 => $secPersonalInfo) {
    // Displaying personal information
    foreach ($secPersonalInfo->newFees as $key2 => $rows) {
        // show the row
    }
}

我个人更喜欢#3