consulta.php

<< Click to Display Table of Contents >>

Navigation:  Outros > CRUD com persistencia Orientada Objeto > projeto >

consulta.php

Previous pageReturn to chapter overviewNext page

<?php
 
include_once filter_var(getenv('DOCUMENT_ROOT')) . '/init.php';
 
use \modelo\Pessoa;
use \persistencia\DAL;
use \persistencia\Filtro;
use \classes\Util;
 
// limpa o POST (quando usuario pressionar "Filtrar")
$post_array = filter_input_array(INPUT_GETFILTER_SANITIZE_STRING);
 
// tem algo no POST? (filtro de pesquisa)
if ($post_array) {
    $filtro = $post_array['filtro'];
}
 
$titulo = "Página Inicial";
$style = ".btn  { margin-right: 5px; }";
include __DIR__ . "/visual/cabecalho.php";
?>
<body>
    <div class="container">
        <header>
            <h2>CRUD em PHP</h2>
        </header>
        <form class="form-horizontal" action="consulta.php" method="get">
            <div class="row">
                <div class="col-2">
                    <a href="cadastro.php" class="btn btn-success">Adicionar</a>
                </div>
                <div class="col-4">
                    <div class="controls">
                        <input size="20" class="form-control" name="filtro" type="text" placeholder="Filtro" value="<?= $filtro ?? "" ?>">    
                    </div>
                </div>
                <div class="col-2">
                    <button type="submit" class="btn btn-primary">Filtrar</button>
                </div>
            </div>
            <br/>
        </form>
        <div class="row">
            <table class="table table-striped table-hover table-sm">
                <thead>
                    <tr>
                        <th scope="col" style="text-align:right">Id</th>
                        <th scope="col">Nome</th>
                        <th scope="col">Endereço</th>
                        <th scope="col">Email</th>
                        <th scope="col" style="text-align:center">Sexo</th>
                        <th scope="col">Nasc.</th>
                        <th scope="col" style="text-align:right">Limite</th>
                        <th scope="col" style="text-align:center">Hora</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    
                    // vamos montar o filtro caso usuário escolheu um...
                    $f = new Filtro();
                    if (isset($filtro)) {
                        $f->addContem("nome"$filtro);
                    }
 
                    /** @var Pessoa[] lista */                   
                    $lista = DAL::GetListObjects(new Pessoa(), $f"nome");
 
                    // @var Pessoa $pessoa 
                    foreach ($lista as $pessoa) {
                        echo '<tr>';
                        echo '<th style="text-align:right">' . $pessoa->id_pessoa . '</th>';
                        echo '<td><a href="cadastro.php?id=' . $pessoa->id_pessoa . '">' . $pessoa->nome . '</a></td>';
                        echo '<td>' . $pessoa->endereco . '</td>';
                        echo '<td>' . $pessoa->email . '</td>';
                        echo '<td style="text-align:center">' . $pessoa->sexo . '</td>';
                        echo '<td>' . Util::fmtData($pessoa->nascimento) . '</td>';
                        echo '<td style="text-align:right">' . Util::fmtValor($pessoa->limite) . '</td>';
                        echo '<td style="text-align:center">' . Util::fmtHora($pessoa->hora) . '</td>';
                        echo '</tr>';
                    }
 
                    DAL::Desconectar();
                    ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?php include __DIR__ . "/visual/scripts_terceiros.php"; ?>
</body>
 
</html>