I'm having doubts about how to structure a JQuery
that should append an HTML code to each of the items a JSON returns me. My question is how to retrieve each of the JSON values and do a repeat structure to create the HTML that will eventually be printed on the page. It's all very simple because I'm learning to work with Phonegap
. Thanks in advance for your help.
For example, I want a Panel with the country and championship name as the hardcoded example of the image to be created for each item returned in JSON:
webservice.js
functionlistaCampeonatos(){$.ajax({url:'http://localhost/projetos/cjwebservice/listagem.php',type:'GET',dataType:'json',data:{type:'listaCampeonatos'},ContentType:'application/json',success:function(response){alert('Listagembemsucedida!');$('#resultado').html(JSON.stringify(response));},error:function(err){alert('Ocorreuumerroaosecomunicarcomoservidor!Porfavor,entreemcontatocomoadministradoroutentenovamentemaistarde.');console.log(err);}});
}
listing.php
include'./conexao.php';header("Access-Control-Allow-Origin: *");
$link = conectar();
if ($_GET['type'] == "listaCampeonatos") {
//echo 'Tipo de operação: ' . $_GET['type'] . '<br>';
$query = "SELECT c.id AS id_campeonato, c.nome_camp AS nome_campeonato, p.nome_pais AS nome_pais
FROM tb_campeonato c
LEFT JOIN tb_pais p ON p.id = c.tb_pais_id
LEFT JOIN tb_partida pt ON pt.tb_campeonato_id = c.id
WHERE pt.flag_ativo = 1
GROUP BY p.nome_pais";
$result = mysqli_query($link, $query);
$registros = array();
while ($reg = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$registros[] = array('Campeonatos' => $reg);
}
$saida = json_encode(array('Lista' => $registros));
echo $saida;}
games.html
<!-- COTAÇÕES INÍCIO -->
<div id="painel_partidas" class="panel panel-primary panel-heading-margin">
<div class="panel-heading">
<center>
<b>Brasil » Série A - 19/10/2016</b>
<button data-toggle="collapse" data-target="#partida" class="btn btn-default btn-xs pull-right"><i class="fa fa-compress"></i></button>
</center>
</div>
<div id="partida">
<div class="w3-border">
<center>
<button class="btn btn-xs btn-danger" onclick="listaCampeonatos()"><i class="fa fa-search"></i>
Testar JSON
</button>
</center>
<div id="resultado"></div>
<!--COTAÇÕES AQUI-->
</div>
</div>
</div>
<!-- COTAÇÕES FIM -->