How to invert values that are separated by commas in mysql

5

Photo from my base:

Thedatalookslikethis:

-46.63642120299626,-23.54854965191239,0

Ineedtoselecttobeinverted:

Ex:

-23.54854965191239,0,-46.63642120299626

Myselectioninphp:

<?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");
   }
?>
    
asked by anonymous 28.09.2015 / 17:35

2 answers

5

If the data is coming directly into 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;

?>
    
28.09.2015 / 17:53
2

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

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