Change Preposition According to State

Asked

Viewed 88 times

1

I have the following situation:

I’m using an IP-based location API http://ip-api.com/

Everything is working perfectly. What I can’t do is change the following:

Hello visitor "from" São Paulo Hello Visitor "from" Rio de Janeiro

You can change this preposition automatically according to the state?

2 answers

4


Although somewhat archaic, it would work with array:

<?php
$uf = "São Paulo";
$preposicoes = array(
    "Rio de Janeiro" => "do",
    "São Paulo" => "de"); // complete com os demais estados

echo "Olá visitante " . $preposicoes[$uf] . " " . $uf . "!";
?>
  • Straight to the point.. Super correct! VLW man, that archaic nothing rs!

0

An alternative route json worked smoothly here:

            $(function(){
                $.ajax({
                    url: "http://ip-api.com/json/208.80.152.201",
                    context: document.body
                }).done(function(response) {
                    console.log(response);
                    $("#local-visitante").html(response.city);
                });
            });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<p>Olá visitante de <span id="local-visitante"></span></p>

Via PHP also worked ok following the documentation specifications:

<?php
    $ip = "177.33.22.36"; // the IP address to query
    $query = @unserialize(file_get_contents('http://ip-api.com/php/'.$ip));
    echo 'Olá visitante de: ' . $query['city'];
?>

I hope I’ve helped.

  • I wasn’t the one who said no, but notice that the user is asking something else, not what you answered...

  • I thought he wanted to exhibit the city dynamically.

Browser other questions tagged

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