Best form tag Select in Php without framework with DAO project pattern

0

I'm having a question about how best to implement a method that traverses a common select using DAO standard with PDO.

It would be appropriate for me to do this, my whole code in the view:

$conexao = new PDOUtil ();
$consulta = $conexao->getStance()->prepare( "SELECT id_pagina, tema FROM pagina" );
$consulta->execute ();

?>    
      <select required="" name="id_pagina">
          <option disabled="">Selecione uma página</option>
          <?php while ($linha = $consulta->fetch(PDO::FETCH_OBJ)) { ?> 
          <option value="<?php echo $linha->id_pagina;?>"><?php echo   $linha->tema;?></option>
          <?php } ?>
      </select>

Or it would be best to do all this part within my DAO class example by blending HTML and PHP within querys:

        class Dao
    $conexao = new PDOUtil ();
     public function buscarTudo() {
     $consulta = $conexao->getStance()->prepare( "SELECT id_pagina, tema FROM pagina" );
$consulta->execute ();


      echo "<select required="" name="id_pagina">";
      echo"<option disabled="">Selecione uma página</option>";
      while ($linha = $consulta->fetch(PDO::FETCH_OBJ)) {
          echo "<option value=" $linha->id_pagina;">"
          echo "$linha->tema;?></option>"
          }
      echo "</select>";
}

and then just call this method inside my view. Would you have another suggestion?

If the code is wrong do not call it is why I typed it right here, thank you all.

    
asked by anonymous 29.04.2015 / 05:24

1 answer

1

Your DAO class should only be responsible for running queries in the database, avoid text outputs to have problem with header aready sent.

Let's say the user clicked on the link that assembles a sign-up screen, ideally this file or controller call DAO to store the result in a variable and dispatch it to the view this can be done with a template engine like smarty , twig or can be done manually, just call an include / require that has the html, there make the foreach to display the options.

The content of the template called by include / required would look something like this:

Controller file or equivalent:

<?php
   $dao = new Dao();
   $paginas = $dao->buscarTodo();

   include 'template.php';

template.php

<select name="id_pagina">
   <option value="">Selecione</option>
   <?php foreach($paginas as $pagina) {?>
      <option value="<?php echo $pagina->id_pagina;?>"><?php echo $pagina->tema;?></option>
    <?php } ?>
</select>

Another approach is to create a class that uses methods to generate HTML.

public static function montarOptions($itens){
    $options = '';
    foreach($itens as $item){
        $options .= sprintf('<option value="%s">%s</option>', $item->id_pagina, $item->tema);
    }
    return $options;
}

The controller stays the same, what changes a bit is the template:

<select name="id_pagina">
    <option value="">Selecione</option>
    <?php echo montarOptions($paginas); ?>
</select>
    
29.04.2015 / 13:06