0
I’m using jQuery to print returns from a JSON Web Service, the script works normally, but the page loading is very slow, I’m using PHP tags within my jQuery script, I would like to use only jQuery and I saw that the function each serves to go through Arrays, but I’m not sure how to apply it, and also I’m not sure if the way I wrote the code is correct, could give me tips on how to proceed?
php agenda.
<?php
$dt = date('Y') . '';
$dm = date('m') . '';
$api_campeonatos = 'https://sportsmanager.com.br/api/[email protected]&token=SXLSO8342HSDE78623GVS7234GNMSKL&ano=2018&status=A';
$campeonatos = wp_remote_get( $api_campeonatos );
$campeonato = json_decode( wp_remote_retrieve_body( $campeonatos ), true );
$api_resultados = 'https://sportsmanager.com.br/api/[email protected]&token=SXLSO8342HSDE78623GVS7234GNMSKL&inicio=2018-'. $dm .'-01&fim='. $dt .'-12-31';
$resultados = wp_remote_get( $api_resultados );
$resultado = json_decode( wp_remote_retrieve_body( $resultados), true);
$api_equipes = 'https://sportsmanager.com.br/api/[email protected]&token=SXLSO8342HSDE78623GVS7234GNMSKL';
$equipes = wp_remote_get( $api_equipes );
$equipe = json_decode( wp_remote_retrieve_body( $equipes ), true );
$api_locais = 'https://sportsmanager.com.br/api/[email protected]&token=SXLSO8342HSDE78623GVS7234GNMSKL';
$locais = wp_remote_get( $api_locais );
$local = json_decode( wp_remote_retrieve_body( $locais ), true );
$api_categorias = 'https://sportsmanager.com.br/api/[email protected]&token=SXLSO8342HSDE78623GVS7234GNMSKL';
$categorias = wp_remote_get( $api_categorias );
$categoria = json_decode( wp_remote_retrieve_body( $categorias ), true );
$api_modalidades = 'https://sportsmanager.com.br/api/[email protected]&token=SXLSO8342HSDE78623GVS7234GNMSKL';
$modalidades = wp_remote_get( $api_modalidades );
$modalidade = json_decode( wp_remote_retrieve_body( $modalidades ), true );
$retorno_resultados = array();
if(!is_array($equipe) && !is_array($local) && !is_array($categoria) && !is_array($modalidade)){
print_r('Não encontrado');
}else{
$col = array_column($equipe, 'codigo');
$loc = array_column($local, 'codigo');
$cat = array_column($categoria, 'codigo');
$mod = array_column($modalidade, 'codigo');
foreach($campeonato as $camp){
if(empty($camp) && !is_array($camp)){
}else{
foreach($resultado as $result){
if(empty($result) && !is_array($result)){
}else{
if(isset($camp['categoria']) && isset($camp['modalidade']) && isset($result['mandante']) && isset($result['visitante']) && isset($result['local']) && isset($result['data'])) {
$categorizacao = '';
$key1 = array_search($camp['categoria'], $cat);
if($key1){
$categorizacao = $categoria[$key1]['nome'];
}
$modalidadez = '';
$key2 = array_search($camp['modalidade'], $mod);
if($key2){
$modalidadez = $modalidade[$key2]['nome'];
}
$mandante = '';
$key3 = array_search($result['mandante'], $col);
if($key3){
$mandante = $equipe[$key3]['nome'];
}
$visitante = '';
$key4 = array_search($result['visitante'], $col);
if($key4){
$visitante = $equipe[$key4]['nome'];
}
$localizacao = '';
$key5 = array_search($result['local'], $loc);
if($key5){
$localizacao = $local[$key5]['nome'];
}
$data = $result['data'];
$data = date('d/m/Y', strtotime($data));
$i = 0;
if(isset($camp['codigo']) && isset($result['campeonato']) && $camp['codigo'] == $result['campeonato']){
$retorno_resultados[++$i][] = array(
'mandante' => $result['mandante'],
'visitante' => $result['visitante'],
'nm_mandante' => $mandante,
'nm_visitante' => $visitante,
'nm_cat' => $categorizacao,
'nm_mod' => $modalidadez,
'nm_local' => $localizacao,
'id' => $camp['codigo'],
'modalidade' => $camp['modalidade'],
'categoria' => $camp['categoria'],
'data' => $result['data'],
'data_certa' => $data,
'placar1n' => $result['placar1n'],
'placar2n' => $result['placar2n'],
'placar1p' => $result['placar1p'],
'placar2p' => $result['placar2p'],
'placar1s' => $result['placar1s'],
'placar2s' => $result['placar2s'],
'jogo' => $result['jogo'],
'horario' => $result['horario'],
'jogo' => $result['codigo'],
);
};
};
};
};
};
};
};
?>
jQuery
<script>
$(function() {
<?php foreach($retorno_resultados as $val){ ?>
var html3 = '';
var id='';
<?php foreach($val as $res){ ?>
html3 += '<tr><td><?php echo $res['data_certa'];?>  às  <?php echo $res['horario'];?></td><td><?php echo $res['nm_local'];?></td><td><?php echo $res['nm_mandante'];?></td><td><?php echo $res['nm_visitante'];?></td><td><?php echo $res['nm_mod']?> <?php echo $res['nm_cat']?></td></tr>';
id = <?php echo $res['id'] ?>;
<?php } ?>
$('#equipesTbody').html(html3);
<?php } ?>
});
</script>
HTML
<div class="agenda-modalidades">
<?php include_once 'agenda.php'; ?>
<table id="agendatabelas" class="table table-bordered">
<thead>
<tr>
<th>Data</th>
<th>Local</th>
<th>Mandante</th>
<th>Visitante</th>
<th>Mod / Cat</th>
</tr>
</thead>
<tbody id="equipesTbody">
</tbody>
<tfoot>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tfoot>
</table>
</div>
You can only do this by using AJAX to play a PHP and recover the values in JS.
– SachaDee
I use php to filter the data in another function, I would like to optimize jquery, it is difficult?
– Miguel Campos
Why are you using jQuery if you already have the records in PHP? You don’t have access to the element
#equipesTbody
from PHP?– Leite
it’s my first time consuming Web Service, I haven’t found anything that helps me do it in php, so I went to jquery, I’m a beginner yet.
– Miguel Campos
Can you show the HTML that generates the table that will show the teams? Where do you have the element
#equipesTbody
. You can edit that code and change that table, if yes, I would suggest that you use what you already have in PHP to show results there directly, instead of putting them on the page for jQuery to "catch" and then update the table?– Leite
I’ve added the information as you requested, I’m doing it as follows
– Miguel Campos