Return SQL data mysqli_fetch_assoc organized into a table

0

I have the following SQL

<?php

   require_once("php/conexao.php");

   $strSQL = "SELECT id, titulo FROM questoes ORDER by id DESC";

   if($result = mysqli_query($conexao, $strSQL)) {

      while($row = mysqli_fetch_assoc($result)) {
         $ID = $row['id'];
         $titulo = $row['titulo'];
      }
   }
?>

And the following table:

<div class="row tabela">
            <div class="col-md-12">
                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <h3 class="panel-title">Lista de Questões</h3>
                        <div class="pull-right">
                            <span class="clickable filter" data-toggle="tooltip" title="Toggle table filter" data-container="body">
                                <i class="glyphicon glyphicon-filter"></i>
                            </span>
                        </div>
                    </div>
                    <div class="panel-body">
                        <input type="text" class="form-control" id="dev-table-filter" data-action="filter" data-filters="#dev-table" placeholder="Filter Developers" />
                    </div>
                    <table class="table table-hover" id="dev-table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Título da Questão</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>1</td>
                                <td>Exemplo</td>
                            </tr>
                            <tr>
                                <td>2</td>
                                <td>Exemplo</td>
                            </tr>
                            <tr>
                                <td>3</td>
                                <td>Exemplo</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

I'm trying to organize the "ID" and "title" data that I pull from SQL correctly in the table, but I'm pounding my head. It is a very beginner doubt, but I really am without a path, lol. I'm a beginner in PHP and SQL and it's my first system.

    
asked by anonymous 03.09.2015 / 16:16

1 answer

2

You have to print the data in the middle of your html:

<table class="table table-hover" id="dev-table">
    <thead>
        <tr>
            <th>ID</th>
            <th>Título da Questão</th>
        </tr>
    </thead>
    <tbody>

        <?php

        while($row = mysqli_fetch_assoc($result)) {
            $ID = $row['id'];
            $titulo = $row['titulo'];
        ?>

        <tr>
            <td><?php echo $ID; ?></td>
            <td><?php echo $titulo; ?></td>
        </tr>

        <?php 
        } // fecha while
        ?>

    </tbody>
</table>

Of course I'm considering that this query is being done in the same file - and before - the table.

    
03.09.2015 / 16:19