How to return data from a class within an option

Asked

Viewed 74 times

-2

Good afternoon, people, I have a problem. I’m a beginner in object-oriented programming and I’m trying to make a select/option that gets values from within the database in an employee table. The problem is that when making the request to the database by a Select class it returns me the connections with the database, I am not able to return the data and put them within 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 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 getting how to give the Return or pass the data to the html page by creating objects and instantiating them. Just then put in an option. People give me a light, I’ve been trying for a few days and I couldn’t. I appreciate it. 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 connection object saved within the array.

1 answer

0

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

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>

Browser other questions tagged

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