How to generate query and add new input?

0

I have a field input of type date I want every time I click and view it, it generates a select with the query of the date that I set and generate a new input to add another date and so on. >

My code so far:

HTML FORM

<form class="form-inline" role="form" style="margin-left: 10px;" action="#">
 <div class="form-group">
 <label for="relatorio">Visualizar Relatorio: </label>
 <input type="date" class="form-control" id="relatorio" name="relatorio">
 </div>
 <div class="btn btn-success m-l-10" onclick="consulta()">Adicionar</div>
 <div class="btn btn-success m-l-10" onclick="">Remover</div>
</form>

HTML RESULTS

<table>
        <thead>
          <tr>
            <th>Cod.</th>
            <th>Descricao</th>
            <th>Status</th>
            <th>Valor</th>
        </tr>
    </thead>
    <tbody>
        <?php 
        foreach ($dia as $b) {?>
        <tr>
            <td><?php echo $b->id; ?></td>
            <td><?php echo $b->descricao; ?></td>
            <td><?php echo $b->status; ?></td>
            <td><?php echo $b->valor; ?></td>
        </tr>
        <?php } ?>
    </tbody>

MYSQL:

"SELECT *, SUM(valor) as valo FROM pedidos WHERE data LIKE :data GROUP BY 'data' ORDER BY id DESC"

My biggest problem is that I do not know how to generate foreach and querys because every time I add a new date with the reports it will generate a new foreach.

How to do it?

    
asked by anonymous 29.01.2018 / 21:28

1 answer

1

With PHP you will not be able to do this "dynamically" you will have to launch the data through an AJAX request and each time that request returns some value, that is, return the HTML, you insert it with document.querySelector(".classe"); .

Edit
When the user clicks (set) the date, it will throw the connect() function, this function could be something like:

function connect()
{
    var relatorio = document.getElementById("relatorio").value;
    $.post("query.php", {data: relatorio}, function(response){
        // response é os dados do HTML lá do PHP
        document.getElementById("resultado").innerHTML += response;
    });
}
query.php
//VOCE PRECISA FAZER O FOREACH NO PHP
//Esse script vai retornar HTML do FOREACH
header("Content-Type", "text/html");

$data = $_POST['data']; //recebendo de $.post (javascript)

$query = "SELECT * FROM table WHERE {$data}"; //faça sua query, porém insira a variável $data

//mande para o banco de dados

//FAÇA O FOREACH AQUI DO HTML E ARMAZENE EM UMA VARIAVEL

//echo $VARIAVEL_COM_O_HTML DO FOREACH

I do not know what your final result should look like, I'm not psychic. But there's the notion of what you need to do there. Until.

    
29.01.2018 / 21:51