Doubt how to separate items returned from a Web Service

1

I have a website in Wordpress and I'm consuming a Web Service, I created a function to filter the championships based on some information:
Sex: M
Mode: 2 Category: 4 Home If all are true, it returns the name of the respective championship, the problem is that if there is more than one championship with the same conditions, it displays all the names at once, as I do not have much experience with this type of function and it is the first time I use a Web Service, I would like to know if I would like to display a championship per table row, so I can separate them in every "<a>" using the jQuery script I created, thanks in advance! p>

Test.php

<?php 
    $api_request = 'https://sportsmanager.com.br/api/[email protected]&token=SLXSO8342HSDE78623GVS7234GNMSKL';
    $api_response = wp_remote_get( $api_request );
    $api_data = json_decode( wp_remote_retrieve_body( $api_response ), true );
    $retorno = '';

if($api_data){
    foreach($api_data as $row){
        if(!is_array($row)){
            //$retorno = $retorno.'<td>'.$row.'</td>';
        }else{
            if($row['sexo'] == 'M' && $row['modalidade'] == 2 && $row['categoria'] == 4){
                $retorno .='<td>'.$row['nome'].'</td>';
            }   
        }   
    }
}
?>

jQuery

$(function(){

    var html = '<a class="list-group-item list-group-item-action active" data-toggle="list" href="#home" role="tab"><?php echo $retorno;?></a>';

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

Return of the url

 {
"codigo": "1",
"nome": "1ª Fase",
"modalidade": "9",
"categoria": "10",
"sexo": "F",
"data": "2018-04-12 00:00:00",
"atualizacao": "2018-07-23 04:07:15",
"status": "N"
},
  {
"codigo": "2",
"nome": "1ª Fase",
"modalidade": "2",
"categoria": "4",
"sexo": "M",
"data": "2018-04-05 00:00:00",
"atualizacao": "2018-07-20 01:07:40",
"status": "S"
},
  {
"codigo": "14",
"nome": "50 metros Livre Nível A",
"modalidade": "304",
"categoria": "10",
"sexo": "F",
"data": "2018-05-20 00:00:00",
"atualizacao": null,
"status": "N"
},
  {
"codigo": "15",
"nome": "CIRCUITO ESCOLAR - SÉRIE OURO",
"modalidade": "2",
"categoria": "4",
"sexo": "M",
"data": "2018-08-13 00:00:00",
"atualizacao": null,
"status": "N"
},
    
asked by anonymous 31.07.2018 / 13:58

1 answer

1

As you are using php direct in javascript, it is easier to solve. First you make the variable $retorno into array. Then, you enter the values found within this variable. As the code below shows:

 $retortno = array(); // <- mudar para array

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

}

In javascript, with php, you will use a loop to generate the html correctly by linking the values one at a time. So:

$(function(){

    var html = '';

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

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

    <?php } ?>

    $('#myList').html(html);
});
    
31.07.2018 / 15:36