index.php

<< Click to Display Table of Contents >>

Navigation:  Outros > CRUD simples com Bootstrap >

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'];
}
?>
 
<!DOCTYPE html>
<html lang="pt-br">
 
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
        <title>Página Inicial</title>
        <style>
            .btn  {
                margin-right5px;
            }
        </style>
    </head>
 
    <body>
        <div class="container">
            <div class="jumbotron">
                <div class="row">
                    <h2>CRUD em PHP</h2>
                </div>
            </div>
            <br/>
 
            <form class="form-horizontal" action="index.php" method="get">
                <div class="row">
                    <div class="col-2">
                        <a href="create.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">
                    <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 scope="row">' . $row['id'] . '</th>';
                            echo '<td>' . $row['nome'] . '</td>';
                            echo '<td>' . $row['endereco'] . '</td>';
                            echo '<td>' . $row['telefone'] . '</td>';
                            echo '<td>' . $row['email'] . '</td>';
                            echo '<td>' . $row['sexo'] . '</td>';
                            echo '<td width=250>';
                            echo '<a class="btn btn-sm btn-primary" href="read.php?id=' . $row['id'] . '">Info</a>';
                            echo '<a class="btn btn-sm btn-warning" href="update.php?id=' . $row['id'] . '">Atualizar</a>';
                            echo '<a class="btn btn-sm btn-danger" href="delete.php?id=' . $row['id'] . '">Excluir</a>';
                            echo '</td>';
                            echo '</tr>';
                        }
                        Banco::desconectar();
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</body>
 
</html>