I'm sending the variable's value through the url to filter by employees, like this:
<form method="POST" action="">
<ul class="flex-outer">
<li>
<label for="colb">Colaborador</label>
<select class="form-control" id="colb" name="colb" required="" style="width: 25% !important; min-width: 25%; max-width: 25%;">
<option></option>
<?php
$sql = "SELECT id, nome FROM raddb.usuarios WHERE id NOT IN ('107', '93', '94', '95', '96', '97', '98', '99') AND situacoe_id = '1' ORDER BY nome ASC";
$qr = mysqli_query($conn, $sql);
while($ln = mysqli_fetch_assoc($qr)){
echo '<option value="'.$ln['id'].'">'.$ln['nome'].'</option>';
}
?>
</select>
</li>
<button style="float:right" type="button" class="btn btn-success" id="btn_entrar" data-toggle="modal">Consultar</button>
</ul>
</form>
JavaScript:
$(function () {
$('#btn_entrar').on("click", function() {
var col = $('#colb').val();
$.getJSON('./editarfa?col' + '&col=' + col, function (data) {
});
});
});
As an array comes from json_encode
, I'm looping it inside the javascript like this:
var arrayLength = data.length;
for (var i = 0; i < arrayLength; i++) {
DataAtrib = data[i][0];
Farda = data[i][1];
Tamanho = data[i][2];
Qtdfar = data[i][3];
nome = data[i][4];
Funcao = data[i][5];
}
Then inside the javascript I have to fill the table in html like this:
$('#daat').html(DataAtrib);
document.getElementById("daat").value = DataAtrib;
$('#datf').html(Farda);
document.getElementById("datf").value = Farda;
$('#datt').html(Tamanho);
document.getElementById("datt").value = Tamanho;
$('#datq').html(Qtdfar);
document.getElementById("datq").value = Qtdfar;
$('#datn').html(nome);
document.getElementById("datn").value = nome;
$('#datfu').html(Funcao);
document.getElementById("datfu").value = Funcao;
Before showing the table, I put the full javascript:
$(function () {
$('#btn_entrar').on("click", function() {
var col = $('#colb').val();
$.getJSON('./editarfa?col' + '&col=' + col, function (data) {
var arrayLength = data.length;
for (var i = 0; i < arrayLength; i++) {
DataAtrib = data[i][0];
Farda = data[i][1];
Tamanho = data[i][2];
Qtdfar = data[i][3];
nome = data[i][4];
Funcao = data[i][5];
}
$('#daat').html(DataAtrib);
document.getElementById("daat").value = DataAtrib;
$('#datf').html(Farda);
document.getElementById("datf").value = Farda;
$('#datt').html(Tamanho);
document.getElementById("datt").value = Tamanho;
$('#datq').html(Qtdfar);
document.getElementById("datq").value = Qtdfar;
$('#datn').html(nome);
document.getElementById("datn").value = nome;
$('#datfu').html(Funcao);
document.getElementById("datfu").value = Funcao;
});
});
});
Table:
<table class="table table-responsive" id="emp_fa">
<thead>
<tr>
<th>Atribuíção</th>
<th>Farda</th>
<th>Tamanho</th>
<th>Quantidade</th>
<th>Colaborador</th>
<th>Função</th>
</tr>
</thead>
<tbody>
<tr>
<td id="daat"></td>
<td id="datf"></td>
<td id="datt"></td>
<td id="datq"></td>
<td id="datn"></td>
<td id="datfu"></td>
</tr>
</tbody>
</table>
Everything works fine when you return only one row from the database. When, for example, two or three rows from the database are returned, only the last row returned is shown in the table, but I intend to show the three rows returned from the database in different rows in the table.
editarfa:
$colb = $_GET["col"];
$stmt = $conn->prepare("SELECT DataAtrib, raddb.Fardas.Farda, Tamanho, Qtdfar, nome, Funcao
FROM raddb.SaidaFarda LEFT OUTER JOIN raddb.Fardas ON raddb.Fardas.Id = raddb.SaidaFarda.Farda
LEFT OUTER JOIN raddb.TamanhoLuvas ON raddb.TamanhoLuvas.Id = raddb.SaidaFarda.Tama
LEFT OUTER JOIN raddb.usuarios ON raddb.usuarios.id = raddb.SaidaFarda.Colabo
LEFT OUTER JOIN raddb.FuncaoCol ON raddb.FuncaoCol.Id = raddb.SaidaFarda.Func
WHERE SaidaFarda.Colabo = '$colb' ORDER BY DataAtrib DESC");
$stmt->execute();
$json = [];
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$json[]= [(string)$DataAtrib, (string)$Farda, (string)$Tamanho, (int)$Qtdfar, (string)$nome, (string)$Funcao];
}
echo json_encode($json);