简体   繁体   中英

Populating table with select dropdown (from database)

As shown in the code below, I have a dropdown list, populated from table "tabela". Below it I have a table which I need it to be populated depending on the option select from the dropdown?

How can I do this?

<div class="box">
    <?php
        $pdo = new PDO('mysql:host=localhost;dbname=dbname; charset=utf8', 'user', 'pass');
        $sql = "SELECT id, pref, nome FROM tabela GROUP BY pref,nome ORDER BY nome";
        $stmt = $pdo->prepare($sql);
        $stmt->execute();
        $users = $stmt->fetchAll();
    ?>
    <select>
        <?php foreach($users as $user): ?>
            <option value="<?= $user['id']; ?>"><?= $user['pref']; ?> - <?= $user['nome']; ?></option>
        <?php endforeach; ?>
    </select>
    <table class="gradienttable">
        <thead>
            <tr>
                <th colspan="7" class="tabelas">tipo1</th>
            </tr>
            <tr>
                <th>Tipo1</th>
                <th>Modelo</th>
                <th>Qtde</th>
                <th>Tipos</th>
                <th>Pessoas</th>
                <th>Vazios</th>
                <th>Porcent</th>
            </tr>
        </thead>
        <tbody>
            <?php
                foreach ($dbh->query("SELECT *, (ocupacao)*100 as ocupacao1 FROM tabela WHERE prefixo=8510") as $row) {
                    printf(
                        "<tr onmouseover='this.style.fontWeight=700;' onmouseout='this.style.fontWeight=400;'>
                            <td style='padding:8px;'>%s - %s</td>
                            <td style='padding:8px;'>%s</td>
                            <td>%s</td>
                            <td>%s</td>
                            <td>%s</td>
                            <td>%s</td>
                            <td>%s</td>
                        </tr>",
                        $row->pref, $row->nome, $row->model, $row->qtde, $row->tipos, $row->pessoas, $row->vazios, $ocupacao1 = round($row ->ocupacao1 * 100)/100 . '%');
                }
            ?>
        </tbody>
    </table>
</div>

Using the way you are gathering the data you need to echo the results into the html.

<?php foreach ($dbh->query("SELECT *, (ocupacao)*100 as ocupacao1 FROM infografico WHERE prefixo=8510") as $row) { ?>
   <tr onmouseover='this.style.fontWeight=700;' onmouseout='this.style.fontWeight=400;'>

     <td><?php echo $row->pref; ?></td>
     ...
   </tr>

<?php } ?>

It would most likely be better to gather the data you need in the table first and form your array as required, then loop through that array and echo into the table.

I think first of all you should change your foreach() statement into:

<?php foreach($users as $user) { ?>
  <option value="<? echo $user['id']; ?>"><? echo $user['pref']; ?> - <?echo $user['nome']; ?></option>
<?php }; ?>

I find this a bit more clean and readable :)

Then you will need to enable the page to "filter" the rows by select - this can be done either by posting the select contents each time you change the select value (so basically you create a form that will submit itself every time you change what is in select - not very modern solution) or use AJAX to send the contents and retrieve the results.

If you decide to go second option (which I would highly recommend) you should take a look at some tutorials on changing page content basing on AJAX response. I would recommend to go to jQuery for that - since you should find quite some functions there that would help you out...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM