Xasho Posted September 3, 2007 Share Posted September 3, 2007 Im trying to make a script where all cities in the table should get their distance calculated with eachother, and afterwards should the distance be inserted into a new table. But in this case the loop finishes after it has went through the first cities distances. <?php function getDistance($lat1, $long1, $lat2, $long2) { $theta = $long1 - $long2; $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); $dist = acos($dist); $dist = rad2deg($dist); return (($dist * 60 * 1.1515)*1.609344); } function getCord($deg, $min, $sec) { return ($deg+($min + $sec/60)/60); } ?> <?php mysql_connect("localhost","user", "pass"); @mysql_select_db("my_db") or die( "Unable to select database"); $result = mysql_query( "SELECT * FROM Cities" ); if (!$result) { die('Query execution problem: ' . mysql_error()); } //$total = mysql_num_rows($result); while($rowFrom = mysql_fetch_array($result)) { $latFromArray = explode(' ', $rowFrom[2]); $longFromArray = explode(' ', $rowFrom[3]); $result2 = mysql_query( "SELECT * FROM Cities" ); while($rowTo = mysql_fetch_array($result2)) { $latToArray = explode(' ', $rowTo[2]); $longToArray = explode(' ', $rowTo[3]); $lat1 = getCord($latFromArray[0], $latFromArray[1], $latFromArray[2]); $long1 = getCord($longFromArray[0],$longFromArray[1],$longFromArray[2]); $lat2 = getCord($latToArray[0], $latToArray[1], $latToArray[2]); $long2 = getCord($longToArray[0], $longToArray[1], $longToArray[2]); if ($rowFrom[0] != $rowTo[0]) { $distance = getDistance($lat1, $long1, $lat2, $long2); $error = mysql_query("INSERT INTO Dist (fromid, toid, dist)VALUES ('$rowFrom[0]', '$rowTo[0]','$distance')") or die("Insert Error: ".mysql_error()); print "Klar<br/>"; } } mysql_close(); } ?> The Result in the new table is: 1 2 50 1 3 40 1 4 20 1 5 45 What I want is that all cities should be compared with eachother, but somehow the loop doesnt continue after the first city. The requested result should be like this. 1 2 50 1 3 40 1 4 20 1 5 45 2 1 50 2 3 10 osv... Would be gratfeul if you could help me with correcting my loop. Link to comment https://forums.phpfreaks.com/topic/67747-solved-looping-problem/ Share on other sites More sharing options...
Barand Posted September 3, 2007 Share Posted September 3, 2007 Move "mysql_close();" out of the loop, or remove it altogether. Link to comment https://forums.phpfreaks.com/topic/67747-solved-looping-problem/#findComment-340360 Share on other sites More sharing options...
Xasho Posted September 3, 2007 Author Share Posted September 3, 2007 hehe thx Barand such a noobie mistake Link to comment https://forums.phpfreaks.com/topic/67747-solved-looping-problem/#findComment-340432 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.