How to take the result of select and mount HTML?

Asked

Viewed 1,803 times

2

My problem is :

I have a field initial date and a field final date and on my table a field date. The user will enter a start date and a end date, and in the database I check if you have data included between these dates. If you have it is possible that there are several data between these dates. And my real problem is .. How to build HTML for the 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 for 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';";

Down there, assemble the data of the result in HTML. I do not know if the most correct way would be to use a radio button where a while goes mounting according to the number of data found.

1 - dados do resultado

2 - dados do resultado

3 - dados do resultado
  • 1

    Yes, it is possible.

  • Like then @Renan?

  • Filter the data exactly as you said, and display just them. You even say you already have the query ready. What you tried didn’t work out?

  • Yes I have the consultation ready picking up the fields. My problem is that I need to show the user the fields to select which one he wants to "edit"! And yes, I don’t know how to show them!

  • 2

    I suggest editing the question to focus on what you need. "How do I view data from a table..." or something like that. It seems to me that what you need is to take the result of the query and build the HTML, right? I would love to be able to help but PHP is not my strong suit :(

  • This @Renan, I need to get the result and assemble the HTML! It’s okay, I will make the appropriate edits for better understanding!

  • 3

    Old that’s still half incomplete... how would you like it to work? With ajax? You have there the return of the database, what would be the environment that shows the result? ?

  • In short: translate your question into code form and description of your environment. . . . . . . . . . . . . . . . . See: http://answall.com/help/mcve

  • I suppose it should be for a loop creating radio / button / ...!

  • Missing part that calls the desired HTML in your code (or if you want to use <form>, for example). And your query is where? Put the context in which it occurs, so the answers can be better elaborated.

Show 5 more comments

3 answers

3


It is actually quite simple to assemble your HTML by the result of your search. As you mentioned, by radio would be 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 he’s gonna ride a radio of all the data from outworking, showing the fields you determine!

2

You can do this in two ways using PHP and/or JS . . . the first is riding the Query in PHP, running it and mounting on the page itself... in which case you should have a connection to the bank, I leave here a simple 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 Voce should suit according to your needs... in general the first creates only one XML pure, the second selects the value of interest and agrees that it should be displayed

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

1

I believe that’s what you want:

In this example I am 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>

Browser other questions tagged

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