Divergent Results When Filtering Response in API

Asked

Viewed 36 times

0

Edit: I managed to get him to create a single tab-crash, but now he’s not filtering the games according to the championship as you can see in the image below, he’s printing them all! I edited the code...

inserir a descrição da imagem aqui

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 they are all true, it returns the name of the respective championship and prints on screen within a <div> data-toggle of bootstrap.

Within this toggle, another function compares the values below into two different Urls.

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

If these values are equal, he prints the league matches with the information below:

Principal x Visiting

The function works, the big problem is, it is not only pulling the information from the championships that were compared above, but from all of them, and in the <div> content "tab-pane" he is creating one for each game, but the correct thing would be for him to create only one tab crashes with the id of the championship, and all games with this id were inserted in table format within it.

i tried to use the foreach after opening the div and also an array_push, but as it is my first time with Web Service consumption I am not succeeding, someone could help me?

Returns the values and compares SEX, MODALITY AND CATEGORY:

<?php 
    $api_campeonatos = 'https://sportsmanager.com.br/api/[email protected]&token=SXLSO8342HSDE78623GVS7234GNMSKL&ano=2018&status=A';
    $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
                }   
            }   
        }
    }
?>

Print the championship name on a list-group:

<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="cont-<?php echo $valor['id'];?>"></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>

Compares the values $api_results['campeonato] : $api_campeonatos['codigo'] in two different Urls and returns the championship games:

<?php
// pega os valores da primeira URL
$api_campeonatos = 'https://sportsmanager.com.br/api/[email protected]&token=SXLSO8342HSDE78623GVS7234GNMSKL&ano=2018&status=A';
$campeonato = wp_remote_get( $api_campeonatos );
$campeonato = json_decode( wp_remote_retrieve_body( $campeonato ), true );

// pega os valores da segunda URL
$api_resultados = 'https://sportsmanager.com.br/api/[email protected]&token=SXLSO8342HSDE78623GVS7234GNMSKL&ano=2018&status=A';
$resultado = wp_remote_get( $api_resultados );
$resultado = json_decode( wp_remote_retrieve_body( $resultado ), true );

$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
        }
    }
}
?>

Prints game values inside a <div> tab-pane content:

<script>

$(function(){

    var html3 = '';

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

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

    <?php } ?>

    <?php foreach($retorno_resultados as $id_camp){ ?>
    $('#cont-<?php echo $id_camp['id'];?>').html(html3);
    <?php } ?>
});

</script>

Return JSON URL:championships:

 {
"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"
},
  {
"codigo": "36",
"nome": "CIRCUITO ESCOLAR - SÉRIE PRATA",
"modalidade": "2",
"categoria": "8",
"sexo": "F",
"data": "2018-08-13 00:00:00",
"atualizacao": null,
"status": "N"
},
  {
"codigo": "20",
"nome": "CIRCUITO ESCOLAR - SÉRIE PRATA",
"modalidade": "2",
"categoria": "6",
"sexo": "M",
"data": "2018-08-13 00:00:00",
"atualizacao": null,
"status": "N"
},

JSON return URL:results:

{
"codigo": "31",
"campeonato": "18",
"data": "2018-09-11 00:00:00",
"horario": "19H",
"local": "2",
"realizada": "N",
"jogo": "0",
"fase": "2",
"rodada": "0",
"mandante": "5",
"visitante": "3",
"placar1n": null,
"placar2n": null,
"placar1p": null,
"placar2p": null,
"placar1s": null,
"placar2s": null,
"grupo1": "",
"grupo2": "",
"quadra": "2",
"obs": null
},
  {
"codigo": "32",
"campeonato": "18",
"data": "2018-09-25 00:00:00",
"horario": "19H",
"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": "19h",
"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
},

NOTE: THERE ARE MORE RECORDS ON RETURN.

inserir a descrição da imagem aqui

  • What exactly is your doubt?

  • my question is, how can I print all returns from the results URL with the comparison of these values $camp['code']) && isset($result['championship'] in different urls, within the tab-pane, do not create a tab pane per record, which in this case is what is happening.

  • http://circuitoescolar.com.br/new/test/ this url is where I am running the tests.

  • added an image of what is happening at the end of the question, as you can see, the script is creating a "tab-pane" for each game that has the value "Championship":"18", I need him to create just one tab-pane, with all games that have the value "Championship":"18" inside, in table format.

  • @Leandroangelo managed to display only one tab-pane, but the feedback is wrong, you could not explain me please?

No answers

Browser other questions tagged

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