How do I get the result of the select and assemble the HTML?

2

My problem is:

I have a start date field and a end date field and in my table a date field . The user is going to type a start date and a end date , and in the database I check if there are data included between those dates. If there is a possibility that there may be several data between these dates. And my real problem is .. How to mount HTML for User?

Ex:

Data Inicial : 02/05/2008 Data Final : 05/07/2012

<tr>
    <td>
        <font  size="-1">Data inicial :</font>
    </td>
    <td >
        <input name="tx_data_inic" id="id_data_inic" type="text"  maxlength="10"  size="5">
    </td>
    <td >
        <font  size="-1">Data final :</font>
    </td>
    <td >
        <input name="tx_dt_fina" id="id_data_final" type="text"  maxlength="10" size="5">
        <button type="button" onClick="f_veri_datas();"></button>                       
    </td>
</tr>   

Select checks to see if you have data in the table:

$w_querybusca="SELECT * FROM sai_cad_datas
                    INNER JOIN sai_cad_cara
                    ON sai_cad_cara.fk_seq_data = sai_cad_datas.seq_data    
    where sai_cad_datas.dt_nota between '$w_tx_dt_inic' and '$w_tx_dt_fina';";

Below, the results data is assembled in HTML. I do not know if the correct way would be to use a radio button where a while will mount according to the number of data found.

1 - dados do resultado

2 - dados do resultado

3 - dados do resultado
    
asked by anonymous 27.08.2014 / 16:27

3 answers

3

It's actually quite simple to assemble your HTML by the result of your search. As you mentioned, radio would look like this:

    $w_querybusca = "SELECT * FROM sai_cad_datas
                     INNER JOIN sai_cad_cara
                     ON sai_cad_cara.fk_seq_data = sai_cad_datas.seq_data    
                     WHERE sai_cad_datas.dt_nota
                         BETWEEN '$w_tx_dt_inic'AND '$w_tx_dt_fina';";

    $w_queryresult=f_class_conecta_bd($w_querybusca);

    while($w_registro = pg_fetch_object($w_queryresult))
    {
        print('<input type="radio" name = rb_peri </input>');
        print('campo que quer mostrar');
    }
Within the while it will mount a radio of all the result data, showing the fields you determine!

    
28.08.2014 / 14:05
2

You can do this in two ways using PHP and / or JS . . . the first one is setting Query to PHP , running it and mounting it on the page itself ... in this case you should have a connection with the bank, here is a simpler example possible:

Using PHP / BD_PDO:

        <table id="tabela" class="display">
            <thead>
                <tr>
                    <th>Dia</th>
                </tr>
            </thead>
            <tbody>
            <?php
            require_once ("cmd_sql.php"); // uma biblioteca que utilizo pra criar a conexão com o banco, utilizando BD_PDO . . . de uma pesquisada sobre

            $varConsulta = ConsultaLista();
            $i=0;
            if ($varConsulta) {
                foreach ($varConsulta as $lin) {
                    $varDia = $varConsulta[$i]['DESCRICAO'];

                    $linhas = "";

                    $linhas .= "<td>" . $varDia . "</td>";

                    echo "<tr>" . $linhas . "</tr>";
                    $i++;
                }
            }

            function ConsultaLista() {
            $sql = new cmd_SQL();   


            $bd['sql'] = "O seu select aqui";
            $rs = $sql->pesquisar($bd);

            return $rs;
            }

            ?>
            </tbody>
        </table>

Using JS / Ajax / BD_PDO:

function carregaDia() {
    var varXML = carrega_XML('SuaBibliotecaQueCarregaOsDados.php', 'filtro=CarregaDia', 'xml');
    var nfaixa = valor_XML(varXML, 'n_reg', 0);
    var tabela = "";
    if (nfaixa > 0) {
        tabela +="<table width='100%'><thead><th>DIA</th></thead><tbody>";
        var dia = "";
        for (i = 0; i < nfaixa; i++) {
            dia = valor_xml(varXML, 'DIA', i);
            tabela += "<tr><td>" + dia+ "</td></tr>";
        }
      tabela += "</tbody</table>"
     document.getElementById('seuCampoQueCarregaATabela').value = tabela;
    }
}

Detail the functions carrega_XML and valor_XML are functions that you must adapt according to your needs ... usually the first one only creates a XML pure, the second one selects the value of interest and agrees that should be displayed

At the end ... it's very relative, it depends on the way you want to work .... if the page is dynamic, I suggest JS/Ajax , if it's static, PHP

    
27.08.2014 / 17:12
1

I believe this is what you want:

In this example I'm using MySQL

script.js

<script type="text/javascript">
$(document).ready(function(){
data = $(this).serialize() + "&" + $.param(data);
$.ajax({
  type: "POST",
  dataType: "json",
  url: "exemplo.php", 
  data: data,
  success: function(data) {
    $("#tabela").append("<tr><td>"+data["dataInicio"]+"</td> <td>"+data["dataFim"]+"</td>");
  }
});
</script>

example.php

<?php
    $consultaSql = "SELECT * FROM tabela WHERE (dataInicio BETWEEN parametro_dataInicio AND dataFim BETWEEN parametro_dataFim)";
    $query = mysql_query($sql);
    //Atribuo a variavel result o retorno da consulta
    while($result = mysql_fetch_row($query)){  
        //Retorno um Json
        echo json_encode($result);      
    } 
?>

example.html

<form method="post">
    <table id="tabela">
        <tr>
            <td>Data Inicio</td>
            <td>Data Fim</td>
        </tr>
        <tr>
            <td><input type="date" name="dataInicio" /></td>
            <td><input type="date" name="dataFim" /></td>
            <td><input type="submit" name="pesquisar" value="Pesquisar" /></td>
        </tr>
    </table>    
</form>
    
27.08.2014 / 17:10