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

Asked

Viewed 125 times

4

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

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

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

1 answer

4


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

Of course it is almost impossible to make a page that does not have any code, unless in some trivial case. Then some code will always be needed.

What is ideal is that everything is filled out ready to model and does not need to encode, but pragmatically it becomes complicated to do something like this, even impossible in some cases.

How will you create the appropriate HTML for several items without having a loop? HTML has no loops, you need PHP for this. A tie-free solution would be cast 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. Anything is possible to bring from a modelview ready, but almost always not ideal. If it is something you should define according to the presentation, do in it.

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

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

  • I agree, some doubts about the standard can be clarified at: http://www.php-fig.org/psr/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.