Return SQL data mysqli_fetch_assoc organized in a table

Asked

Viewed 98 times

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 data "ID" and "title" that I remove from SQL correctly in the table, but I’m hitting my head a lot. It is a very beginner doubt, but really I am without a path, rs. I am beginner in PHP and SQL and is my first system.

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 - table.

  • Buddy, perfect! The result was on the face, but it cleared my mind you putting it that way. I set it the way my code here and it was perfect, of course, I just had to change the SQL query to ASC that gave everything OK. Thanks again for the help, again!

  • @Rodrigobrf quiet friend, needing we are there!!

Browser other questions tagged

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