How to return data from a class within an option

-2

Good afternoon, guys, I have a problem. I'm a beginner in object-oriented programming and I'm trying to do a select / option that takes values from within the database into an employee table. The problem is that when I make the request to the bank for a Select class it returns the connections to the bank, I am not able to return the data and place them inside an option.

class Select {
private static $conn;

public function __construct() {
    $this->conn = new Conexao();
}

public function Selecionar(){
try{
    $pdo = $this->conn->Conectar()->prepare('SELECT * FROM funcionario');
    $pdo->execute();
    $pdo->fetchAll(PDO::FETCH_ASSOC);
    foreach ($pdo as $resultado){
        echo $resultado;
        var_dump($resultado);
    }  
} catch (PDOException $ex) {
    echo"<script language='javascript' type='text/javascript'>alert('Houver um erro, entrar em contato com administrador.('.$ex->getLine().');window.location.href='/DivinaBeleza/login.php';</script>";
}
}

}

and here is the HTML:

                    <select>
                        <option value="1">Chosen</option>
                            <?php
                                $a = new Select();
                                $a->Selecionar();                              
                            ?>
                            <option value="<?php echo $result['idFuncionario']; ?>"><?php echo $result['funcaoFuncionario'];?></option>
                    </select>

I'm not understanding how to give the return or pass the data to the page in html by creating objects and instantiating them. Only then put in an option. Guys give me a light, I've been trying for some days and I could not. Appreciate. What appears to me is the connection, if I use var_dump or echo in the select () method in the $ result variable, it shows me the object connection stored inside the array.

    
asked by anonymous 22.11.2018 / 19:47

1 answer

0

It was very confusing for your implementation, I think it would be easier for you to create the option in another file and in html you call that new file with the options filled out.

class Select {
private static $conn;

public function __construct() {
    $this->conn = new Conexao();
}

public function Selecionar(){
try{
    $pdo = $this->conn->Conectar()->prepare('SELECT * FROM funcionario');
    $pdo->execute();
    $pdo->fetchAll(PDO::FETCH_ASSOC);
    foreach ($pdo as $resultado){
            echo $resultado;
        }
}

Controller

    <?php 
     $select =  new Select();
     $items = $select->Selecionar();
     foreach($items as  item)
     {
       echo '<option value="">'.item['elemento'].// mesmo atributo salvo no banco'</option>'
     }

?>

html

<select>
    <option value="0" disabled>Escolha um item</option>
      <?php include_once("Controller.php")?>                        
</select>
    
23.11.2018 / 18:40