Doubt how to separate returned items from a web service

Asked

Viewed 59 times

1

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, 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 consume a Web Service, I wonder if it would have how to display a championship per table line, so I can separate them in each "<a>" using the jQuery script I created, thanks for the help!

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"
},
  • Miguel, since it’s an api you should consult the api documentation to see how you can return the data differently, there’s nothing we can help with that. If you want to manipulate the result, put in the answer an example of the return json and what you would like to change that we can help you

  • I understand, I’m sorry, it’s my first time with this kind of work, I’ll post the return of Json, because they didn’t give me any documentation, I’m very lost! If you can help me, I’d be very grateful!

  • Tranquil Miguel. Well the json is simple, now you need to explain it better: " would like to know if it would have how to display a championship per table line". I don’t know what parameters you used, but it didn’t seem to filter by those criteria you mentioned in the question, see that there are different modalities and categories in the result

  • on that line, I make him compare three values, if($Row['sex'] == ’M' && $Row['mode'] == 2 && $Row['category'] == 4) {$return .= '<td>'. $Row['name']. '</td>'; and return me the name of the championship, but there is more than one championship that meets these criteria, so it returns the name of two of them in the same row, I would like each championship to have its own generated line within this html tag <a class="list-group-item list-group-item-action active" data-toggle="list" href="#home" role="tab"><? php echo $return;? ></a>

  • the result is general, it would filter differently according to the page, but then I would change the values as needed.

  • i just need it to divide the filtered names and display each one in its <a class="list-group-item">

Show 1 more comment

1 answer

1


Since you are using php right in javascript, it is easier to resolve. First you transform the variable $retorno array. Then you insert the values found within this variable. As shown in the code below:

 $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 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);
});
  • Dude, you’re awesome, it worked out! One last question, if I wanted to call the <a> championship id, would that be about right? href="+$return[i]. ['code']+"

  • because in this case, I use the bootstrap, with the data-toggle function, to exchange content without redirecting the page, so I would need to replicate the id co campeonato both in the href of <a>, and in the id of the <div class="tab-pane active" id="home" role="tabpanel">...</div>

  • @Miguelcampos I’ll edit the answer for you

  • Thank you very much @Andrei Coelho, is being of great help!!

  • @Miguelcampos quiet. Take a look.

  • The $valor['id'] you can use the event code wherever you want.

  • got it, it returned me the following error Uncaught Syntaxerror: Invalid or Unexpected token, will we need to concatenate?

  • @Miguelcampos Sorry, I had made a wrong edition. now it’s right.

  • worked perfectly now! I don’t even know how to thank you guy, thank you even!

  • Tranquil @Miguelcampos . Thanks for the acceptance. Good luck!

Show 6 more comments

Browser other questions tagged

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