Is it correct to use PHP code in the view layer along with HTML?

3

I'm starting now in the area and I have a college job to do and it's necessary to use the MVC standard, I've even separated things well, but I came across the following code in a file from my view layer >, the filename is novoAtendimento.php (name is suggestive).

In the following code, I have a <select> field in HTML and the <option> of it is generated according to the information we have in the database, if we add 1 value in the database, that value will appear in% and if we remove it from the database, this value will disappear from <option> , the code is working right, I just wonder if it's right to do that.

<div class="col-xs-4">
     <div class="form-group">
          <label for="tipoAtendimento">Tipo do Atendimento:</label>
          <?php
          $tipoAt = new TipoAtendimentoController(); 
          $retornoTipoAt = $tipoAt->select(); 
          if(count($retornoTipoAt) > 0)
          {
               echo "<select class=".'selectpicker'." data-size=".'5'." data-live-search=".'true'." data-width=".'100%'.">";
               foreach ($retornoTipoAt as $dados => $value) 
               {    
                    echo "<option data-subtext=".$retornoTipoAt[$dados]->id_GAPtipoatendimento.">".$retornoTipoAt[$dados]->nome_GAPtipoatendimento."</option>"; 
               }
               echo "</select>"; 
          }
          else
          { 
               header("Location: erro.php");
          }
          ?>
     </div>
</div>
    
asked by anonymous 20.08.2017 / 20:14

1 answer

3

This is always a bit controversial, it's part of that area of computing that's an art.

Of course, it's almost impossible to make a page that has no code, other than some trivial case. So some code will always be needed.

What is considered ideal is that everything is filled ready of the model and does not need to code anything, but pragmatically it is complicated to do something like this, even impossible in some cases.

How will you create the right HTML for multiple items without having a loop? HTML has no ties, you need PHP for this. A solution without a tie would be plastered and too long, out of the question in almost every situation.

Even a decision, a simple calculation, a formatting may be required in the code. Everything is possible to bring from a ready modelview , but almost always is not ideal. If it is something that you should define according to the presentation, do it.

I find it very strange to create a controller within the view , this seems to me wrong, the controller should provide the model the way view in> need, not the other way around, and then maybe the controller is wrong as well. The rest seems reasonably appropriate.

Some people will prefer to have all the HTML code outside the PHP code and not with a echo .

    
20.08.2017 / 20:26