I have a simple PHP MVC concept and do some nested foreach loops in my controller.
In this case, only one of the nested foreach is executed and the other is ignored. Is that the correct way of nested foreach in this case?
Controller:
$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);
}
}
View:
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>
';
}
Model: public function 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();
}