How to make two querys in a JSON?

Asked

Viewed 106 times

1

I’m mounting a map using the mapbox API, creating some points dynamically on the map. I have a JSON that gives me some information (getDados.php), but I wanted to perform one more query inside this file and get some more data from other tables.

   //getDados.php
  //Conectando ao banco de dados
$con = new mysqli("localhost", "root", "", "nota");
if (mysqli_connect_errno()) trigger_error(mysqli_connect_error());

//Consultando banco de dados
$qryLista = mysqli_query($con, "SELECT p.*, c.`cliente` AS nome,c.`latitude` as latitude,c.`longitude` as longitude, p.`data_inicio` as data_inicio,p.`data_termino` as data_termino FROM `triagem` AS p INNER JOIN `clientes` AS c ON p.`fk_cliente` = c.`id` ORDER BY p.`cliente` ASC");
while($resultado = mysqli_fetch_assoc($qryLista)){
    $fk_triagem = $resultado["id"];
    $vetor[] = array_map('utf8_encode', $resultado);
  }

//Passando vetor em forma de json
echo json_encode($vetor2);
die();

After that I take this data and work with them in a JS file

//index.js
$(document).ready(function(){
    $.ajax({
        type:'post',        //Definimos o método HTTP usado
        dataType: 'json',   //Definimos o tipo de retorno
        url: '../mapa/getDados.php',//Definindo o arquivo onde serão buscados os dados
    })
    .done(function(response) {

        for(var i=0;response.length>i;i++) {
            var triagem = [response[i].longitude,response[i].latitude];
            var popup = new mapboxgl.Popup()
              .setText('Construction on the Washington Monument began in 1848.')
              .setHTML('<div class="panel panel-default"><div class="panel-body"><p class="lead">Triagem</p><p><strong>Cliente: </strong>'+ response[i].nome +' <\/p><strong>Data de ínicio: </strong>' + response[i].data_inicio + '<\/p><strong>Data de término:</strong> ' + response[i].data_termino + '<p><strong>Técnicos:</strong> xxxx<p><strong>Quantidade de peças previstas:</strong> ' + response[i].total + '<\/p><strong>Quantidade de peças triadas:</strong> xxx</div></div>')
              .addTo(map);
              // create DOM element for the marker
              var el = document.createElement('div');
              el.id = 'marker';
              new mapboxgl.Marker(el)
              .setLngLat(triagem)
              .setPopup(popup) // sets a popup on this marker
              .addTo(map);
        }
    })
});

There’s a way in this same getDados.php file to return some more information ? Maybe create a $vector 2 or something like that?

  • You can return an array of vectors, so the vector can contain the return of two tables, another thing that in your code you are returning $vetor2 could show us where it is set?

  • Don’t just send a new request via Ajax? In Ajax you send variables to the file getDados.php and in this file you assemble the query you want according to the variables sent.

  • From what I understand you just want to execute more than one query, you can do as you said in an array but then you will have to change the way you treat in JS, in case you can give a array_push($result, $query1) and array_push($result, $query2) and give echo json_encode($result)

No answers

Browser other questions tagged

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