How I take this amount

Asked

Viewed 93 times

2

Good evening, I need a help with the code below.

This code already works for what I need. Geolocation to display the nearest users, but how I get the distance value???

$user = $con->query("SELECT user_nome, user_user, user_idade, user_img, 
(6371 * acos(
cos( radians($lat) )
* cos( radians( user_latitude ) )
* cos( radians( user_longitude ) - radians($long) )
+ sin( radians($lat) )
* sin( radians( user_latitude ) ) 
)
) AS distancia
FROM usuarios
HAVING distancia < 400
ORDER BY distancia ASC LIMIT 50", PDO::FETCH_ASSOC);

That distance is neither in my bank nor in variable. I wanted to show for example in a table: user_name and distance

Thank you

  • By doing what you’re doing, the field distancia is automatically created in the associative array that has the results of your query. Just iterate using a foreach, for example: foreach ($user as $row) { echo $row['distancia'] } .

3 answers

0

This code I put up had worked and now it doesn’t work at all.. I don’t know what I had done or if he just read some previous information.. Anyway.. I did it and nothing comes back.. What am I doing wrong? I created a table just to test and put this information that would come from the comic..

<table>
<tr>
<td>Lat</td>
<td>Long</td>
<td>Nome</td>
</tr>
<tr>
<td>-25.4921949</td>
<td>-49.2676986</td>
<td>Hauer 001</td>
</tr>

<tr>
<td>-25.53517</td>
<td>-49.2911306</td>
<td>Pinheirinho 001</td>
</tr>

<tr>
<td>-25.5015146</td>
<td>-49.2437735</td>
<td>Boqueirao 001</td>
</tr>
</table>

  require "config/conexao.php";
//estes números seriam do ponto inicial
$lat = "-25.4921949";
$long = "-49.2676986";
$user = $con->query("SELECT      *
FROM        (
                SELECT      user_nome
                        ,   user_user
                        ,   user_idade
                        ,   user_img
                        ,   (6371 * acos(cos(radians($lat)) * cos(radians(user_latitude)) * cos(radians(user_longitude) - radians($long)) + sin(radians($lat)) * sin(radians(user_latitude)))) AS distancia
                FROM        usuarios
            ) U
WHERE       U.distancia < 1000
ORDER BY    U.distancia
LIMIT       50", PDO::FETCH_ASSOC);
foreach ($user as $row) {
  echo $row['distancia'];
}

0

Sorry guys.. I posted a problem just now but I found the q needed in another forum.. it worked for what I wanted and even better.. I’ll post it here in case anyone needs... Geolocation.. will take the user’s city, state and country.. dai vc do the rest.. I include in the registration form... Thank you for the answers here... NOTE: I could not format the code here separating java from html.. error and I am Noob here

<!DOCTYPE html>
<html>
    <head>

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
        <title>Geolocation</title>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
        <style>
            html, body {
                height: 100%;
                margin: 0;
                padding: 0;
            }
            #map {
                height: 100%;
            }
        </style>
    </head>
    <body>
        <form id="localizacao" >

            <input id="cidade" value="">
            <input id="estado" value="">
            <input id="pais" value="">

            <button   type="submit" class="btn btn-default ">Salvar</button>
        </form>

        <script type='text/javascript'>

            var lati = '';
            var long = '';
            var cidade = '';
            var estado = '';
            var pais = '';
            var dadosajax = '';

            navigator.geolocation.getCurrentPosition(function (posicao) {
                var url = "http://nominatim.openstreetmap.org/reverse?lat=" + posicao.coords.latitude + "&lon=" + posicao.coords.longitude + "&format=json&json_callback=preencherDados";

                var script = document.createElement('script');
                script.src = url;
                document.body.appendChild(script);
                lati = posicao.coords.latitude;
                long = posicao.coords.longitude;

            });

            function preencherDados(dados) {
                cidade = dados.address.city;
                estado = dados.address.state;
                pais = dados.address.country;



                //alert('cordenadas:' + lati + ',' + long + '    Lugar:' + cidade + ',' + estado + ',' + pais);
                

                document.getElementById('cidade').value = cidade;
                document.getElementById('estado').value = estado;
                document.getElementById('pais').value = pais;


                dadosajax = {'cidade':cidade, 'estado':estado, 'pais':pais};
                
                $("#localizacao").submit(function (e) {
                    e.preventDefault();
                    alert(dadosajax);
                    $.ajax({
                        type: "POST",
                        url: "teste6.php",
                        data: dadosajax

                    });
                });




            }
            ;
        </script>
        </body>
            </html>

0

If you want to change the SQL query it is easy to get the columns you want:

SELECT      *
FROM        (
                SELECT      user_nome
                        ,   user_user
                        ,   user_idade
                        ,   user_img
                        ,   (6371 * acos(cos(radians($lat)) * cos(radians(user_latitude)) * cos(radians(user_longitude) - radians($long)) + sin(radians($lat)) * sin(radians(user_latitude)))) AS distancia
                FROM        usuarios
            ) U
WHERE       U.distancia < 400
ORDER BY    U.distancia
LIMIT       50
  • Thank you very much...

  • You are welcome! If the answer was helpful give an UP, and if it is correct mark it as such!

Browser other questions tagged

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