Table with variable size in HTML [closed]

-2

I need an HTML table to be fed by a database but the amount of data is variable. The number of columns is fixed however the number of rows can vary. The table must match the volume of data provided by the database. The user just selects from which company he wants the data, and the table is created and populated according to the choice.

    
asked by anonymous 02.02.2018 / 11:29

1 answer

1

Given the few data you have provided, I believe your solution looks something like this:

<?php
$con = new PDO("mysql: host=localhost;dbname=stackoverflow;charset=utf8", "root", "");
$con->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$con->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );


$stmt = $con->prepare("select * from dados where empresa = ?");
$stmt->bindValue(1, $_GET['empresa']);
$stmt->execute();
 $resultado = $stmt->fetchAll();

?>

<table>
    <thead>
        <tr>
            <th>Dado1</th>
            <th>Dado2</th>
            <th>Dado3</th>
        </tr>
    </head>
    <tbody>
        <?php
            foreach($resultado as $dados)
            {
                echo
                '
                <tr>
                    <td>'.$dados->dado1.'</td>
                    <td>'.$dados->dado2.'</td>
                    <td>'.$dados->dado3.'</td>
                </tr>
                ';
            }
        ?>
    </tbody>
</table>

Now, if you want to only show part of the result and when the user clicks on a link or button shows another part, your query will require the use of OFFSET and LIMIT and learn how to make pagination. For example:

select * from dados where empresa = ? limit 3 offset 5

In this case, this query will select a maximum of 3 lines from the sixth line.

    
02.02.2018 / 11:48