Table with variable size in HTML

Asked

Viewed 133 times

-2

I need an HTML table to be powered by a database but the amount of data is variable. The number of columns is fixed but the number of rows may vary. It is necessary that the table fits 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.

  • What’s wrong? Just add a new <tr> and their <td> for each field

  • Every month the amount of data will vary, I need it to be automated.

  • As you can see in the answer, the work to write two lines is the same as to write 100000 lines in the table. I don’t understand the difficulty yet

  • But I edit the code every month is not automation, I have no way to change every file I create because new data entered, understand?

  • You are using two language tags of your own to serve dynamic web pages/languages 2.0. If you only serve static pages, then you should automate the writing of this static page. So, what is your difficulty?

1 answer

1


Considering the few data you provided, I believe your solution is something similar to 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 just want to show part of the result and when the user presses a link or button shows another part, in your query you will need to use OFFSET and LIMIT and learn how to make paging. For example:

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

In this case this query will select at most 3 lines from the sixth line.

  • 2

    When there is little information, the ideal is through comments go asking the AP the doubt or problem that the same is facing, until it is clarified, also many of the times the same has to add a MCVE.

Browser other questions tagged

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