0

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

1 Answer 1

1

Please try with different variable name. In your code you use same variable name => &$o (in controller) and $m (in view)

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

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.