Table showing only a few columns of the database

Asked

Viewed 34 times

0

I have a system that:

On one page there is a search filter that when filled takes to another page with a table that only shows the records that meet the restrictions required by the filter.

The only way I know how to make a table showing the filtered database records is by printing the table equal to the bank.

Only for this page I wanted a table to show only a few columns of the filtered records, but I don’t know how to do that.

This is the filter page:

<?php 
    require 'strcon.php';
    $queryConsult = mysqli_query ($strcon, "SELECT * FROM  `pedidos` ORDER BY  `pedidos`.`CONSULTORIO` ASC"); 
    $id            = ISSET($_POST["ID"]);
    $consultorio      = ISSET($_POST["CONSULTORIO"]);
?>
<?php 
    require 'strcon.php';
    $queryAno = mysqli_query ($strcon, "SELECT * FROM  `pedidos` ORDER BY  `pedidos`.`ANO` ASC"); 
    $ida            = ISSET($_POST["ID"]);
    $ano      = ISSET($_POST["ANO"]);
?>


<form method="post" action="nota-pag.php">
    <div class="col-lg-6">
        <div class="form-group">
            <label for="CONSULTORIO">Consulorio: </label>
            <select  class="form-control" id="CONSULTORIO" name="CONSULTORIO">
                <option>Selecione...</option>
                <?php while($consult = mysqli_fetch_array($queryConsult)) { ?> 
                <option value="<?php echo $consult['CONSULTORIO']; ?>"><?php echo $consult['CONSULTORIO']; ?></option>
                <?php } ?>
            </select>
        </div>
    </div>
    <div class="col-lg-6">
        <div class="form-group">
            <label for="ANO">Ano: </label>
            <select  class="form-control" id="ANO" name="ANO">
                <option>Selecione...</option>
                <?php while($ano = mysqli_fetch_array($queryAno)) { ?> 
                <option value="<?php echo $ano['ANO']; ?>"><?php echo $ano['ANO']; ?></option>
                <?php } ?>
            </select>
        </div>
    </div>
    <div class="col-lg-6">
        <div class="form-group">
            <label for="MES">Mês: </label>
            <select  class="form-control" id="MES" name="MES">
                <option>Selecione...</option>
                <option>JAN</option>
                <option>FEV</option>
                <option>MAR</option>
                <option>ABR</option>
                <option>MAI</option>
                <option>JUN</option>
                <option>JUL</option>
                <option>AGO</option>
                <option>SET</option>
                <option>OUT</option>
                <option>NOV</option>
                <option>DEZ</option>
            </select>
        </div>
    </div>
    <button type="submit" class="btn btn-primary" style="margin-top: 22;">Buscar</button>
</form>

And this is the page that shows the filtered table:

<!--content--> 
<?php error_reporting(E_ERROR | E_PARSE);
    $lnk = mysqli_connect('localhost','root','') or die(mysqli_error()) or die ('Nao foi possível conectar ao MySql: ' . mysqli_error($lnk));
    mysqli_select_db($lnk,'sis_tam') or die ('Nao foi possível ao banco de dados selecionado no MySql: ' . mysqli_error($lnk));

    $sql = 'SELECT * FROM pedidos ORDER BY CONSULTORIO, MES, ANO ASC';
    $consultorio = $_POST['CONSULTORIO'];
    $mes = $_POST['MES'];
    $ano = $_POST['ANO'];

    if(!is_null($consultorio) && !empty($consultorio)) 
        $sqli = "SELECT * FROM pedidos WHERE CONSULTORIO LIKE '%".$consultorio."%' AND MES LIKE '%".$mes."%'";

    $qry = mysqli_query($lnk, $sqli) or die(mysqli_error($lnk));
    $count = mysqli_num_rows($qry);
    $num_fields = mysqli_num_fields($qry);//Obtém o número de campos do resultado
    //$fields[] = array();
    if($num_fields > 0) {
        for($i = 0;$i<$num_fields; $i++){//Pega o nome dos campos
            $fields[] = mysqli_fetch_field_direct($qry,$i)->name;
        }
    } 
?>

<body>

<!--Tabela com as buscas-->
<?php
//Montando o cabeçalho da tabela
$table = '<table class="table table-hover table-inverse" style="margin-top:50;background-color: #881216; color:lightgrey;"> <tr>';

for($i = 0;$i < $num_fields; $i++){
    $table .= '<th>'.$fields[$i].'</th>';
}

//Montando o corpo da tabela
$table .= '<tbody style="
    background-color: #86979e;
    color: #37444a;    
">';
while($r = mysqli_fetch_array($qry)){
    $table .= '<tr>';
    for($i = 0;$i < $num_fields; $i++){
        $table .= '<td>'.$r[$fields[$i]].'</td>';
    }

    // Adicionando botão de edição

                $table .= '</form></td>';
            }
            //Finalizando a tabela
            $table .= '</tbody></table>';

            //Imprimindo a tabela
            echo '<div class="table-table" style="overflow-x:auto;">';
            echo $table;
            echo '</div>';

?>

This is my table:

ID, DENTIST, PATIENT, CONSULTATION, SERVICE, IDSERV, ADDITIONAL, REQUEST, FORECAST, VALUE, HIT, BALANCE, MES, YEAR, STATUS

The columns I wanted you to show are these:

ID, DENTIST, OFFICE, SERVICE, VALUE, MONTH

Can someone show me how to do this? Thank you. :)

1 answer

2


Instead of putting,

SELECT * FROM pedidos

just put the following:

SELECT ID, DENTISTA, CONSULTORIO, SERVICO, VALOR, MES FROM pedidos

Browser other questions tagged

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