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

Asked

Viewed 354 times

0

I have a question about the best way to implement a method that runs through a common select using DAO standard with PDO.

It would be appropriate for me to do so, my entire 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 the best would be to do all this part within my example DAO class by mixing HTML and PHP inside the 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 within my view. You would have another suggestion?

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

1 answer

1


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

Say the user clicked on link that mounts a log screen, ideally this file or controller call the DAO 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 a include/require that has html, there do the foreach to display the options.

The contents of the template called by include/required la would be more or less like this:

Controller file or equivalent:

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

   include 'template.php';

php template.

<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 with methods manages 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 is the same as the template:

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

  • I guess you didn’t get my question. I know the class responsibility of the question I posed would be the best way to create select without mixing html in the dao class

  • @André Martins the second paragraph does not answer? Or the answer would have to say pq the first option is better than the second vice versa?

  • More or less I would like an example thank you

Browser other questions tagged

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