Note that there are some explanatory texts on larger screens.

plurals
  1. POcalculate distance between 2 places & insert the results to new tables
    text
    copied!<p>I have a form that required user to input their name, address, &amp; education background to analyze it with fuzzy. when they press "Mark Position" button, it'll show on the screen their exact latitude &amp; longitude. after that, I want to use the lat &amp; long that they got to count their distance with some places that I already have in mysql (including the latitude &amp; longitude of the places).</p> <p>the "Mark Position" works fine, but the "Save" button seems to not calculate the distance &amp; not save it to 2 new tables I already had (tj_masjid &amp; tj_univ). here's my code :</p> <pre><code>&lt;?php include 'connect.php'; $conn = mysql_connect($dbhost,$dbuser,$dbpass); $db = mysql_select_db($dbname); if($_POST["button"]=="Save") { $nama=$_POST["nama"]; //name $alamat =$_POST["alamat"]; //address $pendidikan =$_POST["pendidikan"]; //educational background (years) $latitude=$_POST["latitude"]; $longitude=$_POST["longitude"]; $query="INSERT INTO lokasi(nama, alamat, pendidikan, latitude, longitude) VALUES('$nama', '$alamat', '$pendidikan', '$latitude', '$longitude')"; //insert to table location $result=mysql_query($query); if($result){ echo "data berhasil disimpan"; //saving success $id_lokasi=mysql_query('SELECT id from lokasi where nama=nama, alamat=alamat, pendidikan=pendidikan, latitude=latitude, longitude=longitude'); }else{ echo "data gagal disimpan"; //saving failed } //calculate distance in kilometers function getDistanceBetween($latitude1, $longitude1, $latitude2, $longitude2, $unit = 'Mi') { $theta = $longitude1 - $longitude2; $distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta))); $distance = acos($distance); $distance = rad2deg($distance); $distance = $distance * 60 * 1.1515; switch($unit) { case 'Mi': break; case 'Km' : $distance = $distance * 1.609344; } return (round($distance,2)); } //calculate the distance between users location &amp; masjid location (2 masjid) $sql_masjid=mysql_query("SELECT * from masjid"); while($rs = mysql_fetch_array($sql_masjid, MYSQL_ASSOC)) { $masjid[$rs['id']] = $rs; $j_masjid[$rs['id']] = getDistanceBetween($latitude,$longitude,$rs['latitude'],$rs['longitude'],'Km'); $t_jarak = $j_masjid[$rs['id']]; $t_idmasjid = $rs['id']; $tj_masjid=mysql_query("INSERT INTO tj_masjid(id_lokasi, id_masjid, jarak) VALUES($id_lokasi,$t_idmasjid,$t_jarak)"); } //calculate the distance between users location &amp; university location (2 univ) $univ=mysql_query("SELECT * from universitas"); while($rs = mysql_fetch_array($univ, MYSQL_ASSOC)) { $univ[$rs['id']] = $rs; $j_univ[$rs['id']] = getDistanceBetween($latitude,$longitude,$rs['latitude'],$rs['longitude'],'Km'); $t_jarak = $j_univ[$rs['id']]; $t_iduniv = $rs['id']; $tj_univ=mysql_query("INSERT INTO tj_univ(id_lokasi, id_univ, jarak) VALUES($id_lokasi,$t_iduniv,$t_jarak)"); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload