0

So i have this query:

    select * from `sitios` INNER JOIN `imagens` ON sitios.id_sitio = imagens.id_sitio where sitios.id_sitio='1'
while ($row=mysqli_fetch_object($q)){
      $data[]=$row;
    }

    if($q)
      echo json_encode(array('status' => true, 'data' => $data));
    else
      echo json_encode(array('status' => false, 'data' => $data));

The result comes encoded in json since im using phonegap with framework7 and i can only use json.

This query will return this:

https://i.stack.imgur.com/sP0aV.jpg

As you can see, it returns the info about the "sitio" with the id of 1, but it return 3 "nome_imagem" that have different info. This is because this "place" as 3 images associated with it.

This is my ajax post to received the data from the query:

success: function(data) {
                    data = JSON.parse(data);
                    if (data['status']) {
                         $.each(data['data'], function(i, field) {
                            var id = field.id_sitio;
                            var nome = field.nome;
                            var descricao = field.descricao;
                            var img = field.img;
                            var morada = field.morada;
                            var email = field.email;
                            var telefone = field.telefone;
                            var facebook = field.facebook;
                            var website = field.website;
                            var coordenada_x = field.coordenada_x;
                            var coordenada_y = field.coordenada_y;
                            var imagens = field.nome_imagem;
                            console.log(imagens);

The problem is, the var "imagens" only contains 1 nome_imagem. How can i store all the info on variables like i have, but store on an array all the "nome_imagem" fields that come from the query?

Thanks in advance

  • Can you please [edit] your question to show a sample of the `data` JSON as text rather than an image? (Incidentally, why are you manually parsing the JSON yourself rather than setting `dataType:'json'` and letting jQuery do it for you?) – nnnnnn May 26 '17 at 02:52
  • If you want to get only one id, you can try add `GROUP BY sitios.id_sitio` for query and you should `dataType : 'json'` for jQuery as @nnnnnn's comment – Canh Nguyen May 26 '17 at 02:59
  • I want to get all the info related to 1 id, but that id innerjoins another table that has multiple images associated to it. So i want to store in vars all the info from the row id, like i have, and then have an array with all the info from the other table associated with the same id – Emerenciana May 26 '17 at 03:15

1 Answers1

1

This answer provides an example of how to pull data out of an array of objects and produce another array of values. Basically, send your data['data'] to the $.map() function, and return only the nome_imagem value. Something like this:

var imagens = $.map(data['data'], function(o) {
    return o.nome_imagem;
})
pacifier21
  • 813
  • 1
  • 5
  • 13
  • Thanks, this works! Now i have one variable that contains an array with all the names that i want, separeted by commas. How can i select the value from the position 2 of the array for example? because now i want to display the 3 images by using getElementById and replacing each div with one image – Emerenciana May 26 '17 at 03:20
  • `imagens` is now an array, so you can access it like any other array. Use `imagens[0]` for the first element, `imagens[1]` for the seconds, and so on... – pacifier21 May 26 '17 at 03:26