Doubt Compare Values Returned from JSON Webservice

Asked

Viewed 160 times

0

I have a site in Wordpress and in it I am consuming a Web Service, I created a function to filter the championships based on some information:

Sex : M
Modality : 2
Category : 4

If all are true, it returns the name of the respective championship and prints on the screen within a div bootstrap data-toggle, the function is correct, but I needed it to compare two returned values of different JSON urls within the content of that data-toggle:

$api_results['championship]
$api_championships['code']

If these values are equal, you need to print the result of the respective games within the div Tables, in table format:

Principal x Visitor, scoreboard and date

I am trying this way, but without success, if you can help me, I will be grateful!

Pull championship values and print the names if true:

<?php 
    $api_campeonatos = 'https://sportsmanager.com.br/api/[email protected]&token=SXLSO8342HSDE78623GVS7234GNMSKL';
    $campeonatos = wp_remote_get( $api_campeonatos );
    $campeonatos_data = json_decode( wp_remote_retrieve_body( $campeonatos ), true );
    $retorno_campeonatos = array(); // <- mudar para array

    if($campeonatos_data){
        foreach($campeonatos_data as $row){
            if(!is_array($row)){
                //$retorno = $retorno.'<td>'.$row.'</td>';
            }else{
                if($row['sexo'] == 'M' && $row['modalidade'] == 2 && $row['categoria'] == 4){
                    $retorno_campeonatos[] = array('valor' => '<td>'.$row['nome'].'</td>', 'id' => $row['codigo']); // <--- adiciona
                }   
            }   
        }
    }
?>


<script>

$(function(){

    var html = '';

    var html2 = '';

    <?php foreach($retorno_campeonatos as $valor){ ?>

        html += '<a class="list-group-item list-group-item-action" data-toggle="list" href="#bloco-<?php echo $valor['id'];?>" role="tab"><?php echo $valor['valor'];?></a>';

        html2 += '<div class="tab-pane" id="bloco-<?php echo $valor['id'];?>" role="tabpanel"><div id="tables></div></div>';

    <?php } ?>

    $('#myList').html(html);

    $('#tabList').html(html2);
});

</script>

<div class="container">
    <div class="row">   
        <div id="contents">
            <div class="list-group" id="myList" role="tablist">
            </div>

            <div id="tabList" class="tab-content">
            </div>
        </div>
    </div>
</div>

Pull the results of the championships and print the table:

<?php 
    $api_resultados = 'https://sportsmanager.com.br/api/[email protected]&token=SXLSO8342HSDE78623GVS7234GNMSKL&ano=2018&status=A';
    $resultados = wp_remote_get( $api_resultados );
    $resultados_data = json_decode( wp_remote_retrieve_body( $resultados ), true );
    $retorno_resultados = array(); // <- mudar para array

    if($resultados_data){
        foreach($resultados_data as $row => $campeonatos_data){
            if(!is_array($row)){
                //$retorno = $retorno.'<td>'.$row.'</td>';
            }else{
                if($row['campeonato'] == $campeonatos_data['codigo']){
                    $retorno_resultados[] = array('val' => '<td>'.$row['mandante'].'</td>'); // <--- adiciona
                }   
            }   
        }
    }
?>

<script>

$(function(){

    var html3 = '';

    <?php foreach($retorno_resultados as $val){ ?>

        html3 += '<td><?php echo $val['mandante'];?>x<?php echo $val['visitante'];?></td>';

    <?php } ?>

    $('#tables').html(html3);

});

</script>

Return Championships:

  {
"codigo": "17",
"nome": "CIRCUITO ESCOLAR - SÉRIE OURO",
"modalidade": "2",
"categoria": "6",
"sexo": "M",
"data": "2018-08-13 00:00:00",
"atualizacao": null,
"status": "N"
},
  {
"codigo": "19",
"nome": "CIRCUITO ESCOLAR - SÉRIE OURO",
"modalidade": "2",
"categoria": "4",
"sexo": "M",
"data": "2018-08-13 00:00:00",
"atualizacao": null,
"status": "N"
},
  {
"codigo": "18",
"nome": "CIRCUITO ESCOLAR - SÉRIE PRATA",
"modalidade": "2",
"categoria": "4",
"sexo": "M",
"data": "2018-08-13 00:00:00",
"atualizacao": null,
"status": "N"
},

Retorno Results

 {
"codigo": "32",
"campeonato": "18",
"data": "2018-09-25 00:00:00",
"horario": "18",
"local": "4",
"realizada": "N",
"jogo": "0",
"fase": "2",
"rodada": "0",
"mandante": "7",
"visitante": "3",
"placar1n": null,
"placar2n": null,
"placar1p": null,
"placar2p": null,
"placar1s": null,
"placar2s": null,
"grupo1": "",
"grupo2": "",
"quadra": "4",
"obs": null
},
  {
"codigo": "25",
"campeonato": "19",
"data": "2018-08-13 00:00:00",
"horario": "19",
"local": "9",
"realizada": "N",
"jogo": "0",
"fase": "2",
"rodada": "0",
"mandante": "2",
"visitante": "6",
"placar1n": null,
"placar2n": null,
"placar1p": null,
"placar2p": null,
"placar1s": null,
"placar2s": null,
"grupo1": "",
"grupo2": "",
"quadra": "9",
"obs": null
},
  • What’s the matter? I don’t understand

  • i added a new url called, which returns the results of the championship games, in the code I tried to buy the json values of the urls $api_results['championship] and $api_championships['code'], if the values are equal, print the game with "boss", _"visitor", date and scoreboard, na **div id="Tables", not from the error, but does not return any value on the screen...

1 answer

1


The problem is you’re willing to use $val['visitante'] not available in the array $retorno_resultados. Also, you are using the 'boss' key which is also not available.

Another mistake is in yours foreach where you’re trying to compare different things that don’t even exist within the array.

after chat conversations

Solution

<?php

$api_resultados = 'https://sportsmanager.com.br/api/[email protected]&token=SLSO8342HSDE78623GVS7234GNMSKL&ano=2018&status=A';
$resultado = file_get_contents($api_resultados);
$resultado = json_decode($resultado, true);

$api_campeonato = 'https://sportsmanager.com.br/api/[email protected]&token=SLSO8342HSDE78623GVS7234GNMSKL&ano=2018&status=A';
$campeonato = file_get_contents($api_campeonato);
$campeonato = json_decode($campeonato, true);

//print_r($campeonatos_data);

$retorno_resultados = array();

foreach($campeonato as $camp){
    foreach($resultado as $result){
        if(isset($camp['codigo']) && isset($result['campeonato']) && $camp['codigo'] == $result['campeonato']){
            $retorno_resultados[] = array(
                'mandante' => '<td>'.$result['mandante'].'</td>', 
                'visitante' => '<td>'.$result['visitante'].'</td>', 
                'id' => $camp['codigo'],
                'data' => '<td>'.$camp['data'],
                'placar1n' => '<td>'.$result['placar1n'].'</td>',
                'placar2n' => '<td>'.$result['placar2n'].'</td>',
                'placar1p' => '<td>'.$result['placar1p'].'</td>',
                'placar2p' => '<td>'.$result['placar2p'].'</td>',
                'placar1s' => '<td>'.$result['placar1s'].'</td>',
                'placar2s' => '<td>'.$result['placar2s'].'</td>'
            ); // <--- adiciona
            break;
        }
    }
}
?>
<script>

$(function(){

    var html2 = '';

    <?php foreach($retorno_resultados as $val){ ?>

        html2 += '<div class="tab-pane" id="bloco-<?php echo $val['id'];?>" role="tabpanel"><td><?php echo $val['mandante'];?></td></div>';

    <?php } ?>

    $('#tabList').html(html2);

});

</script>
  • ah yes, I’m starting to get the syntax, not yet returned any value :(

  • @Miguelcampos has an error in your foreach. Because you want to compare this: $row['campeonato'] == $campeonatos_data['codigo']?

  • if these two values are equal I need to display a table with returns from the url "results" for example if the championship code : 19, equals the championship results : 19, assemble a table with the values of the results, that would be the principal, visitor, score and date

  • Ah ta... Calma ae. You mixed things up. They are in different urls right?

  • that, are different urls

  • @Miguelcampos try now.

  • Nothing yet, in the function of the championship, I’m trying to print the contents as follows html2 += '<div class="tab-pane" id="block-<? php echo $value['id'];? >" role="tabpanel"><div id="Tables"></div></div></div>';, and a script in the "results" to insert the table inside this div "Tables", this is correct?

Show 3 more comments

Browser other questions tagged

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