Uncaught Syntaxerror: Unexpected token <

Asked

Viewed 691 times

2

I’m consuming data returned from a Web Service JSON, in which I travel array columns, for compare values between Urls and if equal, by its name. We have names of teams, locations, categories and modalities, each with its identification code, ex:

Return of one of the results of URL TEAMS using the var_dump:

array(1241) { 
 [0]=> array(5) { 
  ["codigo"]=> string(1) "4"
  ["modalidade"]=> string(1) "2" 
  ["categoria"]=> string(1) "4" 
  ["nome"]=> string(8) "Arbos SA" 
  ["abreviado"]=> string(3) "ARB" 
}

To string "code" of URL Teams is comparative with the string "code" of URL results, and if the two are equal, subistitui the return by the team name, and the same rule applies to the other Urls.
I created a script using PHP to filter them and jQuery to print the tables, the script works, but sometimes returned me some errors like:

Illegal string offset

Warning: Invalid argument supplied for foreach

I was studying here in the forum and in the php documentation and found solutions like the is_array, isset and Empty, I tried to apply them in the right way, but as your layman in php I believe that is not correct, and on top of that after handling the code so much, began to return:

Uncaught Syntaxerror: Unexpected token <

on Chrome’s console, to which I have researched, some of the keys are locked in the wrong way, I have already looked for the error and not found where... I desperately ask for help, someone who can explain me the applications and syntax so that I can run the code without errors, I thank you for your time and attention!

Ex URL:

$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);

Errors occur between these lines, sometimes in array_column with array_search, sometimes in foreach:

<?php
        if(!empty($campeonato) && !is_array($campeonato) && !is_array($resultado) && !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(!is_array($camp)){

            }else{

                foreach($resultado as $result){

                    if(!is_array($result)){

                }else{

                    if(isset($result['mandante']) && isset($result['visitante']) && isset($result['local']) && isset($result['data'])) {

                $key4 = array_search($camp['categoria'], $cat);

                $categorizacao = '';

                if($key4){
                    $categorizacao = $categoria[$key4]['nome'];
                }


                $key5 = array_search($camp['modalidade'], $mod);

                $modalidadez = '';

                if($key5){
                    $modalidadez = $modalidade[$key5]['nome'];
                }

                $key1 = array_search($result['mandante'], $col);

                $mandante = '';

                if($key1){
                    $mandante = $equipe[$key1]['nome'];
                }


                $key2 = array_search($result['visitante'], $col);

                $visitante = '';

                if($key2){
                    $visitante = $equipe[$key2]['nome'];
                }


                $key3 = array_search($result['local'], $loc);

                $localizacao = '';

                if($key3){
                    $localizacao = $local[$key3]['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'],
                    );
                };
                };
            };
        };
    };
    };
    };

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

    });

I’m online all the time if you need more information, or if you want to chat, I can also pass the Urls to compare the values for the explanation, a strong hug!

  • The first error you mentioned "Warning: Invalid argument supplied for foreach" happens because the first line of the code is checked! Empty($championship) or if this value is empty it will fall in Else that is where the foreach occurs, so I believe that should be taken away the denial ! and leave only Empty($championship)

  • The second error usually happens when a wrong php tag is opened. The php code you sent is correct, if it was opened <?php at the beginning (this omitted in stackoverflow so I do not know).

  • I removed the denial and so far gave no error, the tag is open yes, sorry I forgot to include it, as is returning the error Uncaught Syntaxerror: Unexpected token <, is interfering in a shortcode below, if I remove the script, the shortcode goes back to normal.

  • page loading also takes a little while with the included script, by chance I wrote very bad?

  • The code has a lot of things that could be improved (aesthetic), in performace should not be taking long after it is simple thing that you are doing, what should take is the answer that you make to api.

  • When to the second problem of "Uncaught Syntaxerror: Unexpected token <" error you are closing the php tag at the end of the file? by including it in another file may give problem if it is not closed. The problem should not be in php but in the place where you are using it.

  • i found the error, syntax, thank you very much, your words helped a lot, I believe that this taking a long time due to the way I wrote jquery, I will add it in the post, I would like to remove the php tags with foreach and use each function of jquery, could you explain to me how to proceed?

  • added at the end of php, as it is in my code, thanks again.

  • If you want to isolate the treatment only in php da to be done +- thus, in case you really want to do only with javascript you will have to transform the array that php returned to a way that javascript can read, I believe that only a json_encode in the result of your php already right, who knows that help you. At the end of foreach in php you give a $return_results = json_encode($return_results);

Show 4 more comments
No answers

Browser other questions tagged

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