index.php

<< Click to Display Table of Contents >>

Navigation:  Outros > CRUD simples mas otimizado  >

index.php

Previous pageReturn to chapter overviewNext page

<?php
// limpa o POST (quando usuario pressionar "Filtrar")
$post_array = filter_input_array(INPUT_GETFILTER_SANITIZE_STRING);
 
if ($post_array) {
    $filtro = $post_array['filtro'];
}
 
$titulo = "Página Inicial";
$style = ".btn  { margin-right: 5px; }";
include __DIR__ . "/cabecalho.php";
?>
<body>
    <div class="container">
        <header>
            <h2>CRUD em PHP</h2>
        </header>
 
        <form class="form-horizontal" action="index.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">Id</th>
                        <th scope="col">Nome</th>
                        <th scope="col">Endereço</th>
                        <th scope="col">Telefone</th>
                        <th scope="col">Email</th>
                        <th scope="col">Sexo</th>
                        <th scope="col">Ação</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    include 'banco.php';
                    $pdo = Banco::conectar();
                    $where = isset($filtro) ? "where lower(nome) like '%" . mb_strtolower($filtro) . "%'" : "";
                    $sql = "SELECT * FROM pessoa {$where} ORDER BY nome";
 
                    foreach ($pdo->query($sql)as $row) {
                        echo '<tr>';
                        echo '<th style="text-align:right">' . $row['id'] . '</th>';
                        echo '<td><a href="cadastro.php?id=' . $row['id'] . '">' . $row['nome'] . '</a></td>';
                        echo '<td>' . $row['endereco'] . '</td>';
                        echo '<td>' . $row['telefone'] . '</td>';
                        echo '<td>' . $row['email'] . '</td>';
                        echo '<td style="text-align:center">' . $row['sexo'] . '</td>';
                        echo '<td>';
                        echo '<a class="btn btn-sm btn-danger" onclick="return confirm(\'Confirma e exclusão?\')" href="delete.php?id=' . $row['id'] . '">Excluir</a>';
                        echo '</td>';
                        echo '</tr>';
                    }
                    Banco::desconectar();
                    ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?php include __DIR__ . "/js_terceiros.php"; ?>
</body>
 
</html>