我有一个简单的PHP MVC概念,并在控制器中做了一些嵌套的foreach循环。
在这种情况下,仅执行嵌套的foreach之一,而忽略另一个。在这种情况下,这是嵌套foreach的正确方法吗?
控制器:
$positions = $this->positionModel->getOrderPositionsById($id);
foreach($positions as &$o) {
$o->measure = $this->positionModel->listMeasuresTable($o->id);
foreach($o->measure as &$o) {
$o->measurep = $this->positionModel->getMeasureNamesById($o->measurep);
}
foreach($o->measure as &$o) {
$o->measurec = $this->positionModel->getMeasureNamesById($o->measurec);
}
}
查看:
foreach($position->measure as $k => $m){
$html.= '
<tr nobr="true" style="padding:5px;">
<th style="padding:5px;"><b>'._releaseSuS1.'</b></th>
<th style="padding:5px;">';
foreach($m->measurep as $k => $m){
$html.= $m->content;
}
$html.= '</th>
<th style="padding:5px;">';
foreach($m->measurec as $k => $m){
$html.= $m->content;
}
$html.= '</th>
</tr>
';
}
型号: 公共函数getOrderPositionsById($ id){ $ this-> db-> execute();
$this->db->query('
SELECT
' . $this->table_name . '.id,
' . $this->table_name . '.last_o_id,
' . $this->table_name . '.last_mod,
' . $this->table_name . '.uid,
' . $this->table_name . '.comp_id,
' . $this->table_name . '.order_id,
' . $this->table_name . '.position,
.....
FROM ' . $this->table_name . '
WHERE
' . $this->table_name . '.order_id = :id
');
$this->db->bind(':id', $id);
$this->db->bind(':session_id', $session_id);
$results = $this->db->resultSet();
return $results;
}
public function listMeasuresTable($id){
$company_id = $_SESSION['company_id'];
$this->db->query('
SELECT
measure_list.id,
measure_list.positionid,
measure_list.p_company,
measure_list.measurep,
measure_list.measurec,
measure_list.user_id,
measure_list.modified,
measure_list.created
FROM
measure_list
WHERE
measure_list.positionid = :positionid
ORDER BY
id DESC
');
$this->db->bind(':positionid', $id);
$results = $this->db->resultSet();
return $results;
}
public function getMeasureNamesById($id){
$this->db->query('
SELECT
id,
keynr,
content
FROM
measure
WHERE
id = :id
');
$this->db->bind(':id', $id);
return $this->db->resultSet();
}
答案 0 :(得分:1)
请尝试使用其他变量名。在代码中,您使用相同的变量名=>&$ o(在控制器中)和$ m(在视图中)
foreach($position->measure as $parentkey => $parentValue){
$html.= '
<tr nobr="true" style="padding:5px;">
<th style="padding:5px;"><b>'._releaseSuS1.'</b></th>
<th style="padding:5px;">';
foreach($parentValue->measurep as $childKey1 => $childValue1){
$html.= $childValue1->content;
}
$html.= '</th>
<th style="padding:5px;">';
foreach($parentValue->measurec as $childKey2 => $childValue2){
$html.= $childValue2->content;
}
$html.= '</th>
</tr>
';
}