Running ajax within a table

Asked

Viewed 54 times

0

Good afternoon everyone, I’m trying to make a code where it shows how long a request has been opened and I have an ajax that keeps updating this counter every second, my problem is that this request comes in a table where there may be several requests, but since ajax is only running after the page loads it takes the base date only of the last request and replication at all.

Javascript from ajax:

$(document).ready(function() {

      // Requisicao AJAX
      var requisicao = function() {
        var data_abertura = "<?php echo $data_base; ?>";
        $.ajax({
          url: "contador.php",

          type: "post",
          data: {
            data_abertura: data_abertura
          },
        }).done(function(resultado) {
          // Exibe o resultado no elemento com ID contador
          $(".contador").html(resultado);
        });
      };

      // Executa a requisicao com intervalo de 100ms
      setInterval(requisicao, 100);

    });

Javascript of the counter.php :

$date = $_POST['data_abertura'];
echo $date;
$date = new DateTime($date);
$interval = $date->diff( new DateTime( ) );
echo $interval->format( '%a Dia %H Horas %i Minutos e %s Segundos' );

Html snippet where I bring the counter (with the table), I don’t know if it helps:

    <table id="example1" class="table table-bordered table-striped">
                                <thead>
                                  <tr>
                                    <th id="CV" width="10%">Chamado</th>
                                    <th id="CV" width="10%">Problema</th>
                                    <th id="CV">Nome <br> Contato</th>
                                    <th id="CV">Departamento <br> Descrição</th>
                                    <th id="CV">Prioridade</th>
                                    <th id="CV">Data</th>
                                    <th id="CV" width="0%">Status</th>
                                  </tr>
                                </thead>
                                <tbody>
                                  <?php
                                  $query_2 =  "
                                      SELECT tb_ocorrencias.data_final, tb_ocorrencias.data_inicial, tb_ocorrencias.data_saida, tb_ocorrencias.id_prioridade,tb_ocorrencias.data_abertura,tb_ocorrencias.data_atendimento,  tb_ocorrencias.data_fechamento, tb_ocorrencias.id_ocorrencia, tb_problemas.problema, tb_usuarios.usuario, tb_usuarios.contato, tb_departamentos.departamento, tb_ocorrencias.descricao_ocorrencia, tb_status.status,tb_prioridades.prioridade, tb_ocorrencias.data_entrada
                                      FROM tb_ocorrencias
                                      INNER JOIN tb_usuarios on tb_usuarios.id_usuario = tb_ocorrencias.id_usuario
                                      INNER JOIN tb_problemas on tb_problemas.id_problema = tb_ocorrencias.id_problema
                                      INNER JOIN tb_departamentos on tb_usuarios.id_departamento = tb_departamentos.id_departamento
                                      INNER JOIN tb_prioridades on tb_prioridades.id_prioridade = tb_ocorrencias.id_prioridade
                                      INNER JOIN tb_status on  tb_status.id_status = tb_ocorrencias.id_status
                                      WHERE tb_ocorrencias.id_atendente = 0
                                      AND (tb_ocorrencias.id_departamento = '$id_departamento'
                                                      OR tb_ocorrencias.id_departamento = 184)
                                      ORDER BY tb_prioridades.prioridade DESC, tb_ocorrencias.id_ocorrencia ASC
                                  ";

                                  $resultado_2 = mysql_query($query_2, $conexao);
                                  while ($dados_2 = mysql_fetch_array($resultado_2)) {
                                    $data_base = $dados_2['data_abertura'];

                                    $id_ocorrencia =  $dados_2['id_ocorrencia'];
                                    $a = "<a href = 'chamados/atendente.php?numero=$id_ocorrencia' title = 'Visualizar' style = 'color: #000;'> $id_ocorrencia </a>";
                                      $data_abertura_1 = substr($dados_2['data_abertura'], 0, 10);
                                      $data_abertura = substr($data_abertura_1, -2, 2) . '/' . substr($data_abertura_1, -5, 2) . '/' . substr($data_abertura_1, 0, 4);
                                  ?>
                                      <tr>
                                        <td id="CC"> <?php echo "$a"; ?> </td>
                                        <td id="CV"> <?php echo $dados_2['problema']; ?> </td>
                                        <td id="CV"> <b> <?php echo $dados_2['usuario']; ?> </b> <br> <?php echo $dados_2['contato']; ?> </td>
                                        <td id="CV"> <b> <?php echo $dados_2['departamento']; ?> </b> <br> <?php echo $dados_2['descricao_ocorrencia']; ?> </td>
                                        <td id="CV"> <?php echo $dados_2['prioridade']; ?> </td>
                                        <td id="CC"> <?php echo $data_abertura; ?><p><span id="contador" class="contador" ></span> </p> </td>
                     </tbody>
</table>  
  • 1

    Good afternoon! What sense does it make for you to repeat the same id’s in various elements? You know how to differentiate id of class?

  • 1

    Good evening, I understand that in those cases the correct is unique id, but for the final objective it does not influence, correct ?

  • 2

    The correct is to observe the standards, where it says that you should not repeat the same id on the same page. Another thing is that you are using a tenth of a second setInterval calling an AJAX. This is horrible because it can not only jam the server but crash the browser. And another, this $(".contador").html(resultado); will change the HTML of all elements that have the class .contador at the same time.

  • Hello friend, your query has several JOINS and this makes there are multiple returns per record found, what you need to do is regroup the return data correctly. Some GROUP BY may help, but I would regroup the data directly in PHP on using foreach. Take the query out of the view hehe, create a class (DAO) to control the requests to DB. One more thing, if you want to do these AJAX in sequence, better use callbacks or Promisses so that the next request is made only when the previous request has finished :) Hug.

  • This variable $data_base, you use it somewhere other than the Ajax request?

2 answers

1


Good afternoon people, I managed to solve the problem, I made the request AJAX of the table every minute, this solved the problem, thank you all.

  • boooa.. with me worked out tbm

  • I didn’t need it. When I asked, and I got no answer, about the variable $data_base was to know if it was possible to turn this variable into an array containing all the data needed for the update or to create a new array with the table data.

0

your query has multiple JOINS and this makes there are multiple returns per record found, what you need to do is regroup the return data correctly. Some GROUP BY may help, but I would regroup the data directly in PHP on using foreach. Take the query out of the view hehe, create a class (DAO) to control the requests to DB. One more thing, if you want to do these AJAX in sequence, better use callbacks or Promisses so that the next request is made only when the previous request has finished :) Hug.

Browser other questions tagged

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