Replace foreach php with each jquery

0

I'm using jQuery to print returns from a JSON Web service, the script works normally, but loading the page is very slow, I'm using PHP tags inside my jQuery script, I'd like to use only jQuery and I saw that the each function is for going through Arrays, but I'm not sure how to apply it, and I'm not sure if the form I wrote the code is correct, could you give me hints on how to proceed?

agenda.php

<?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'];?>&nbsp às &nbsp<?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']?>&nbsp<?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>
    
asked by anonymous 30.08.2018 / 18:50

2 answers

1

PHP and include_once

In PHP when you include other files in a file ( include , include_once ), when processing this file, PHP will manage this code as if it were an entire file (such as include ) , so variables defined within a file that is in an include are accessible in parent .

Showing information

With the information you posted, and without being able to test, what I would do was in agenda.php remove the <script> tag where you have jQuery, everything inside it, up to the tag that closes PHP ?> , removed the included tag, but stopped there, without closing PHP.

It is good practice when the file only has PHP

  

If a file is pure PHP code, it is preferable to omit the closing tag at the end of the file. Preventing the existence of spaces or blank lines after the tag, which may cause undesirable effects, why PHP will start the output buffer when there is no intention of the programmer to send any output at this point in the script.

Next, in HTML, in the table where you want to list the results, it updated the equipesTbody part to be

<tbody id="equipesTbody">
<?php foreach($retorno_resultados as $val): ?>
    <?php foreach($val as $res): ?>
        <tr>
            <td><?php echo $res['data_certa'];?>&nbsp; às &nbsp;<?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']?>&nbsp;<?php echo $res['nm_cat']?></td>
        </tr>
    <?php endforeach; ?>
<?php endforeach; ?>
</tbody>

Performance

In fact, your performance problem seems to be in the number of external API calls you make. There are several ways to improve this aspect, but I think for now, you can get it to work is the most important, improving performance can ask another question later.

    
30.08.2018 / 19:36
0

Try using something like:

$(document).ready(function(){
    $.get( "http://meusite.com/", function(data) {
         var obj = JSON.parse(data);

        obj.forEach(function(o, index){
            console.log(o);
        });
    });
});

Try this and put the console.log result ai

    
30.08.2018 / 18:56