gpt4 book ai didi

php - 记录在查看页面上出现两次

转载 作者:行者123 更新时间:2023-11-29 07:23:53 25 4
gpt4 key购买 nike

我正在使用 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 传递给模型以获取记录。

Controller 代码

$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,这就是问题所在。我认为 View 页面或 Controller 存在一些问题。

你能帮我解决这个问题吗?

最佳答案

没有想法,它是关于什么的 - 没有 codeigniter 的线索:) ..

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

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

然后在 View 中

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

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

因此您需要使用 $key$secPersonalInfo 将内循环与外循环关联起来。我可以建议三种方法来做到这一点。

#1 希望 key 相同:

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

#2 确保 key 相同:

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

然后使用#1 中的 View 代码

#3 嵌套结果:

Controller :

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

关于php - 记录在查看页面上出现两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54841405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com