How to invert values that are comma separated in mysql

Asked

Viewed 127 times

5

Photo of my base:inserir a descrição da imagem aqui

The data is like this:

-46.63642120299626,-23.54854965191239,0

I need to select so that they stay inverted:

Ex:

-23.54854965191239,0,-46.63642120299626

My selection in php:

<?php
   $query = mysql_query("SELECT * FROM tabela")or die(mysql_error());
   while($row = mysql_fetch_array($query))
   {
      $name = $row['nome'];
      $latlng = $row['latlng'];
      $desc = $row['desc'];
      echo("addMarker($latlng, '<b>$name</b><br />');\n");
   }
?>
  • 1

    This inversion of values (LOL) has some rule?

  • So, let’s see if you can help me, I need to reverse to stay in this format:-23.54854965191239,-46.63642120299626 has no specific rule. What kind of rule do you say, so I can understand better?

2 answers

5

If the data is coming directly in the string, just do this:

<?php
// se: "-46.63642120299626,-23.54854965191239,0"
// está na variável: $row['latlng']

    $params = explode(',', $row['latlng']);
    $lat = $params[0]; //-46.63642120299626
    $lon = $params[1]; //-23.54854965191239
    $val = $params[2]; //0

    $saida = $lon.','.$val.','.$lat;

?>
  • 1

    It worked perfectly, thank you very much.

  • 1

    @Henriques.Santos. Nice that the answer helped you, if you can mark the green arrow. I thank you.

2

Use the method explode to separate by , and then just reorganize.

<?php
    $partes = explode(',', $row['latlng']);
    $latlng = $partes[0].",".$partes[2].",".$partes[1];
?>

Browser other questions tagged

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