Jump to content

return 5 nearest centres


jarvis

Recommended Posts

Afternoon All,

 

Following on from a previous post, I've now managed to get my postcode script to work, i.e. I can enter a postcode of a start & finish and it returns the distance.

 

To extend on that, I've set up a centres table, a courses table and an association table.

 

When you add a centre, you select (multiple) courses that the place will hold. This all works well.

 

I then have a script (below) which you enter your postcode and select the course you want to do, it should then return the 5 nearest centres. At the mo it only returns 1. Can someone point me in the right direction as I've now looked at this way too long.

 

Thanks

 

<?php 
$page_title = 'Locate A Centre';
include ('includes/header.html');
require_once('mysql_connect.php');// Connect to the db 

if (isset($_POST['submitted'])) { // Handle the form.

if (!empty($_POST['start'])) {
	$start = escape_data($_POST['start']);
} else {
	$start = FALSE;
	echo '<p>Please enter a valid postcode!</p>';
}	

$get_destination = escape_data($_POST['finish']);

$query = "SELECT centre_id, centre_name, postcode FROM centres LEFT JOIN centre_associations USING (centre_id) WHERE category_id=$get_destination";		

$result = mysql_query ($query);

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
	$finish = $row[2];
	$centre_name = $row[1];
}
#echo $finish;	
echo 'your nearest centre is '.$centre_name;

#Convert the post code to upper case and trim the variable
$start = strtoupper(trim($start));
$finish = strtoupper(trim($finish));

#Remove any spaces
$start = str_replace(" ","",$start);
$finish = str_replace(" ","",$finish);

#Trim the last 3 characters off the end
$start = substr($start,0,strlen($start)-3);
$finish = substr($finish,0,strlen($finish)-3);

#query the db
$query_start = "SELECT latitude, longitude FROM postcodes WHERE postcode = '$start' LIMIT 1";

$result_start = mysql_query ($query_start);

$num = mysql_num_rows ($result_start); // How many users are there?

if ($num > 0) { // If it ran OK, display the records.

	while ($row = mysql_fetch_array ($result_start, MYSQL_ASSOC)) {
		#Assign variables
		$lat1 = $row["latitude"];
		$long1 = $row["longitude"];
	}

} else { 
	echo '<p>post code not found</p>';
}	

$query_finish = "SELECT latitude, longitude FROM postcodes WHERE postcode = '$finish' LIMIT 1";

$result_finish = mysql_query ($query_finish);

$num = mysql_num_rows ($result_finish); // How many users are there?

if ($num > 0) { // If it ran OK, display the records.

	while ($row = mysql_fetch_array ($result_finish, MYSQL_ASSOC)) {
		#Assign variables
		$lat2 = $row["latitude"];
		$long2 = $row["longitude"];
	} 

} else { 
	echo '<p>post code not found</p>';
}		

function getDistance($lat1, $long1, $lat2, $long2){

	#$earth = 6371; #km change accordingly
	$earth = 3960; #miles

	#Point 1 cords
	$lat1 = deg2rad($lat1);
	$long1= deg2rad($long1);

	#Point 2 cords
	$lat2 = deg2rad($lat2);
	$long2= deg2rad($long2);

	#Haversine Formula
	$dlong=$long2-$long1;
	$dlat=$lat2-$lat1;

	$sinlat=sin($dlat/2);
	$sinlong=sin($dlong/2);

	$a=($sinlat*$sinlat)+cos($lat1)*cos($lat2)*($sinlong*$sinlong);

	$c=2*asin(min(1,sqrt($a)));

	$d=round($earth*$c);

	return $d;

}

#Returns the distance in miles
$distance = getDistance($lat1, $long1, $lat2, $long2);

}
echo '<p>That\'s a distance of '.$distance.' miles</p>';

?>
<form method="post" action="locate.php">
start: <input type="text" name="start" value="<?php if (isset($_POST['start'])) echo $_POST['start']; ?>" />
course:
            <select name="finish"  size="5">
            <?php // Create the pull-down menu information.
            $query = "SELECT category_id, category FROM categories ORDER BY category ASC";		
            $result = @mysql_query ($query);
            while ($row = mysql_fetch_array ($result, MYSQL_NUM)) {
                echo "<option value=\"$row[0]\"";

                echo ">$row[1]</option>\n";
            }
             ?>
            </select>  

<input type="submit" name="submit" value="Go" />
<input type="hidden" name="submitted" value="TRUE" />
</form>

<?php
include ('includes/footer.html');
?>

 

Hope that makes sense and thanks in advanced!

Link to comment
Share on other sites

try this with you first while loop:

$i=0;
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$finish[$i] = $row[2];
$centre_name[$i] = $row[1];
$i++;
}

echo 'your nearest 5 centre are '.$centre_name['0'] . ", " .$centre_name['1'] . ", " .$centre_name['2'] . ", " .$centre_name['3'] . ", " .$centre_name['4'];

Link to comment
Share on other sites

Thanks ldb358, I've replaced my code with the loop, I think it's working. It shows my centres but says postcode not found

 

your nearest 5 centre are Combined Heating Services Ltd, Gas Train Ltd, SWAAT, East Cheshire Training & Assessment, Steve Johnstonyour nearest centre is Array

post code not found

 

That's a distance of 3516 miles

Hmmm...

 

Also, what if only one centre runs that course? If I try that, I get:

 

your nearest 5 centre are UK Energy Training Ltd, , , , your nearest centre is Array

post code not found

 

That's a distance of 3516 miles

 

The distance of 3516 returns if a postcode isn't found i've discovered!

 

I figured a loop was needed somewhere but wasn't sure if that would just show the same result 5 times or not (sorry if thats a daft thing to say!)

 

Thanks

Link to comment
Share on other sites

I've added this bit

echo 'your nearest 5 centre are '.$finish['0'] . ", " .$finish['1'] . ", " .$finish['2'] . ", " .$finish['3'] . 

", " .$finish['4'];

This is needed to show the postcodes for each centre. I guess I now need to loop the whole thing passing in the postcodes? That should do it I guess, if I then sort the distance by ascending??

Link to comment
Share on other sites

okay this should fix the results if there is only 1(or less than 5):

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$finish[$i] = $row[2];
$centre_name[$i] = $row[1];
$i++;
}
switch(mysql_num_rows($results)){
   case "4":
echo 'your nearest 4 centre are '.$centre_name['0'] . ", " .$centre_name['1'] . ", " .$centre_name['2'] . ", and " .$centre_name['3'] ;
   break;
  case "3":
echo 'your nearest 3 centre are '.$centre_name['0'] . ", " .$centre_name['1'] . ", and " .$centre_name['2'];
   break;
  case "2":
echo 'your nearest 2 centre are '.$centre_name['0'] . ", and " .$centre_name['1'];
  break;
  case "1":
echo 'your nearest centre is '.$centre_name['0'];
  break;
  case "0":
echo "no records found";
  break;
case "5":
default:
echo 'your nearest 5 centre are '.$centre_name['0'] . ", " .$centre_name['1'] . ", " .$centre_name['2'] . ", " .$centre_name['3'] . ", and " .$centre_name['4'];
}

Link to comment
Share on other sites

i don't get what you are asking can you clarify

 

Edit: do you want to do somthing like this?:

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$finish[$i] = $row[2];
$centre_name[$i] = $row[1];
$i++;
}
switch(mysql_num_rows($results)){
   case "4":
echo 'your nearest 4 centre are '.$centre_name['0']."(".$finish['0'].")" . ", " .$centre_name['2']."(".$finish['1'].")" . ", " .$centre_name['2']."(".$finish['2'].")" . ", and " .$centre_name['3']."(".$finish['3'].")";
   break;
  case "3":
echo 'your nearest 3 centre are '.$centre_name['0'] ."(".$finish['0'].")" . ", " .$centre_name['1'] . "(".$finish['1'].")" .  ", and " .$centre_name['2']."(".$finish['2'].")" ;
   break;
  case "2":
echo 'your nearest 2 centre are '.$centre_name['0']. "(".$finish['0'].")" . ", and " .$centre_name['1']."(".$finish['1'].")";
  break;
  case "1":
echo 'your nearest centre is '.$centre_name['0']."(".$finish['0'].")";
  break;
  case "0":
echo "no records found";
  break;
case "5":
default:
echo 'your nearest 5 centre are '.$centre_name['0'] . "(".$finish['0'].")" . ", " .$centre_name['1'] . "(".$finish['1'].")" . ", " .$centre_name['2'] . "(".$finish['2'].")" . ", " .$centre_name['3'] . "(".$finish['3'].")" . ", and " .$centre_name['4']."(".$finish['4'].")" . ;
}

Link to comment
Share on other sites

Hi ldb358

 

Thanks again for the reply. I think that will sort the issue if there is only 1 postcode. The other part was me trying to say, once I loop through 5 times to produce 5 postcodes, I need to pass these in to the code so it says

location 1 - 25 miles

location 2 - 50 miles

etc

 

So once the loop generates the postcode and centre name,rather than list them out, I need to pass them into the function so it will then say the above

 

Does that make more sense?

thanks again!

Link to comment
Share on other sites

this is somewhat pointless and long, and can be avoided with a few loops.

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
   $finish[$i] = $row[2];
   $centre_name[$i] = $row[1];
$i++;
}
switch(mysql_num_rows($results)){
   case "4":
echo 'your nearest 4 centre are '.$centre_name['0'] . ", " .$centre_name['1'] . ", " .$centre_name['2'] . ", and " .$centre_name['3'] ;
   break;
  case "3":
echo 'your nearest 3 centre are '.$centre_name['0'] . ", " .$centre_name['1'] . ", and " .$centre_name['2'];
   break;
  case "2":
echo 'your nearest 2 centre are '.$centre_name['0'] . ", and " .$centre_name['1'];
  break;
  case "1":
echo 'your nearest centre is '.$centre_name['0'];
  break;
  case "0":
echo "no records found";
  break;
case "5":
default:
echo 'your nearest 5 centre are '.$centre_name['0'] . ", " .$centre_name['1'] . ", " .$centre_name['2'] . ", " .$centre_name['3'] . ", and " .$centre_name['4'];
}

You can make your loop like this

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
   $finish[] = $row[2];
   $centre_name[] = $row[1];
}

which saves a few lines of code, but when building the return string, do something like

 

$string = "Your nearest ".count($centre_name);
string .= (count($centre_name) > 1) ? " events are " : " event is ";//will put an is if only 1 and an are if more than 1
//now go through the array, and add the values to string 
foreach($centre_name as $key => $name){
$string .= $name;
$string .= ($key != (count($centre_name) - 1)) ? ", " : " ";//will add a comma if we aren't at the end. if not just add a space
}
echo $string

 

with the following test data

$centre_name = array("dog", "cat", "mouse");

$string = "Your nearest ".count($centre_name);
$string .= (count($centre_name) > 1) ? " events are " : " event is ";//will put an is if only 1 and an are if more than 1
//now go through the array, and add the values to string 
foreach($centre_name as $key => $name){
$string .= $name;
$string .= ($key != (count($centre_name) - 1)) ? ", " : " ";//will add a comma if we aren't at the end. if not just add a space
}
echo $string

 

the script returned

Your nearest 3 events are dog, cat, mouse 

 

 

Link to comment
Share on other sites

Thank mikesta707

 

Why do I not need the i++ in the loop

[php
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
  $finish[] = $row[2];
  $centre_name[] = $row[1];
}
[/code]

Like it was here

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$finish[$i] = $row[2];
$centre_name[$i] = $row[1];
$i++;
}

 

I also get the error:

Parse error: syntax error, unexpected T_CONCAT_EQUAL in C:\Program Files\Apache\Apache2\htdocs\logic\web\locate.php on line 43

When I use

[php
$string = "Your nearest ".count($centre_name);
string .= (count($centre_name) > 1) ? " events are " : " event is ";//will put an is if only 1 and an are if more than 1
//now go through the array, and add the values to string
foreach($centre_name as $key => $name){
$string .= $name;
$string .= ($key != (count($centre_name) - 1)) ? ", " : " ";//will add a comma if we aren't at the end. if not just add a space
}
echo $string
[/code]
line 43 is
string .= (count($centre_name) > 1) ? " events are " : " event is "; //will put an is if only 1 and an are if more than 1

 

Finally, do I need this line:

$centre_name = array("dog", "cat", "mouse");

Or is it just to illustrate what the array would like like?

 

Thanks & apologies!

Link to comment
Share on other sites

Ah ok, I'm getting somewhere (have had a very strong coffee now!)

 

My code

$i=0;
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
   $finish[] = $row[2];
   $centre_name[] = $row[1];
   
}

$location = "Your nearest ".count($centre_name);
$location .= (count($centre_name) > 1) ? " centres are " : " centre is "; //will put an is if only 1 and an are if more than 1
//now go through the array, and add the values to string 
foreach($centre_name as $key => $name){
$location .= $name;
$location .= ($key != (count($centre_name) - 1)) ? "<br/> " : " "; //will add a comma if we aren't at the end. if not just add a space
}
echo $location;	

This now works without error. However, I need to add the postcode into the loop and it needs to show the distance too. For example:

Your nearest 6 centres are:

location 1 - 25 miles

location 2 - 125 miles

location 3 - 205 miles

The distance is calculated by the function, which is why I wondered if you need to loop through to get the postcodes for each centre, than pass that into the function, looping that to provide the above info?

 

Thanks

Link to comment
Share on other sites

Hi,

 

Sorry, am struggling with this. Right, thanks to those above who've helped, i'm now getting somewhere!

The following code returns

Your nearest 6 centres are:

Combined Heating Services Ltd

Gas Train Ltd

SWAAT

East Cheshire Training & Assessment

Steve Johnston

UK Energy Training Ltd ArrayNR4 6DG ME5 8RF

PL14 3US

SK4 1RR

FY4 5PN

CH5 2QJ

Am struggling with 2 parts:

1) Getting the postcode next to the company name

2) Getting the distance of each location

 

At the mo it says 'post code not found'. And won't show any distances either.

<?php 
$page_title = 'Locate A Centre';
include ('includes/header.html');
require_once('mysql_connect.php');// Connect to the db 

if (isset($_POST['submitted'])) { // Handle the form.

if (!empty($_POST['start'])) {
	$start = escape_data($_POST['start']);
} else {
	$start = FALSE;
	echo '<p>Please enter a valid postcode!</p>';
}	

$get_destination = escape_data($_POST['finish']);

$query = "SELECT centre_id, centre_name, postcode FROM centres LEFT JOIN centre_associations USING (centre_id) WHERE category_id=$get_destination";		

$result = mysql_query ($query);

$i=0;
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
   $finish[] = $row[2];
   $centre_name[] = $row[1];
   
}

#print_r ($finish);

$location = "Your nearest ".count($centre_name);
# will put an is if only 1 and an are if more than 1
$location .= (count($centre_name) > 1) ? " centres are:<br/> " : " centre is:<br/> "; 
# now go through the array, and add the values to string 
foreach($centre_name as $key => $name){
$location .= $name;
# will add a comma if we aren't at the end. if not just add a space
$location .= ($key != (count($centre_name) - 1)) ? "<br/> " : " "; 
}
echo $location;	

foreach($finish as $key => $postcode){
$finish .= $postcode;
# will add a comma if we aren't at the end. if not just add a space
$finish .= ($key != (count($postcode) - 1)) ? "<br/> " : " "; 
}
echo $finish;		

#Convert the post code to upper case and trim the variable
$start = strtoupper(trim($start));
$finish = strtoupper(trim($finish));

#Remove any spaces
$start = str_replace(" ","",$start);
$finish = str_replace(" ","",$finish);

#Trim the last 3 characters off the end
$start = substr($start,0,strlen($start)-3);
$finish = substr($finish,0,strlen($finish)-3);

#query the db
$query_start = "SELECT latitude, longitude FROM postcodes WHERE postcode = '$start' LIMIT 1";

$result_start = mysql_query ($query_start);

$num = mysql_num_rows ($result_start); // How many users are there?

if ($num > 0) { // If it ran OK, display the records.

	while ($row = mysql_fetch_array ($result_start, MYSQL_ASSOC)) {
		#Assign variables
		$lat1 = $row["latitude"];
		$long1 = $row["longitude"];
	}

} else { 
	echo '<p>post code not found</p>';
}	

$query_finish = "SELECT latitude, longitude FROM postcodes WHERE postcode = '$finish' LIMIT 1";

$result_finish = mysql_query ($query_finish);

$num = mysql_num_rows ($result_finish); // How many users are there?

if ($num > 0) { // If it ran OK, display the records.

	while ($row = mysql_fetch_array ($result_finish, MYSQL_ASSOC)) {
		#Assign variables
		$lat2 = $row["latitude"];
		$long2 = $row["longitude"];
	} 

} else { 
	echo '<p>post code not found</p>';
}		

function getDistance($lat1, $long1, $lat2, $long2){

	#$earth = 6371; #km change accordingly
	$earth = 3960; #miles

	#Point 1 cords
	$lat1 = deg2rad($lat1);
	$long1= deg2rad($long1);

	#Point 2 cords
	$lat2 = deg2rad($lat2);
	$long2= deg2rad($long2);

	#Haversine Formula
	$dlong=$long2-$long1;
	$dlat=$lat2-$lat1;

	$sinlat=sin($dlat/2);
	$sinlong=sin($dlong/2);

	$a=($sinlat*$sinlat)+cos($lat1)*cos($lat2)*($sinlong*$sinlong);

	$c=2*asin(min(1,sqrt($a)));

	$d=round($earth*$c);

	return $d;

}

#Returns the distance in miles
$distance = getDistance($lat1, $long1, $lat2, $long2);
#Show only the message when a forms submitted
echo '<p>That\'s a distance of '.$distance.' miles</p>';		
}

?>
<form method="post" action="locate.php">
start: <input type="text" name="start" value="<?php if (isset($_POST['start'])) echo $_POST['start']; ?>" />
course:
            <select name="finish"  size="5">
            <?php // Create the pull-down menu information.
            $query = "SELECT category_id, category FROM categories ORDER BY category ASC";		
            $result = @mysql_query ($query);
            while ($row = mysql_fetch_array ($result, MYSQL_NUM)) {
                echo "<option value=\"$row[0]\"";

                echo ">$row[1]</option>\n";
            }
             ?>
            </select>  

<input type="submit" name="submit" value="Go" />
<input type="hidden" name="submitted" value="TRUE" />
</form>

<?php
include ('includes/footer.html');
?>

 

Sorry to keep posting but am struggling a bit with this one!

Thanks

Link to comment
Share on other sites

Right, I've got this working how I want but need to 'tidy' the code up as it's not ideal. I think this can be solved by means of a loop or loops. Any help is very much appreciated!

 

<?php 
$page_title = 'Locate A Centre';
include ('includes/header.html');
require_once('mysql_connect.php');// Connect to the db 

if (isset($_POST['submitted'])) { // Handle the form.

if (!empty($_POST['start'])) {
	$start = escape_data($_POST['start']);
} else {
	$start = FALSE;
	echo '<p>Please enter a valid postcode!</p>';
}	

$get_destination = escape_data($_POST['finish']);

$query = "SELECT centre_id, centre_name, postcode FROM centres LEFT JOIN centre_associations USING (centre_id) WHERE category_id=$get_destination LIMIT 5";		

$result = mysql_query ($query);


$i=0;
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
	$finish[$i] = $row[2];
	$centre_name[$i] = $row[1];	
$i++;
}

echo 'your nearest centre are <br/>';
echo $centre_name['0'].' '.$finish['0'].'<br/>';
echo $centre_name['1'].' '.$finish['1'].'<br/>';
echo $centre_name['2'].' '.$finish['2'].'<br/>';
echo $centre_name['3'].' '.$finish['3'].'<br/>';
echo $centre_name['4'].' '.$finish['4'].'<br/>';

#Assign values
$finish0 = $finish['0'];
$finish1 = $finish['1'];
$finish2 = $finish['2'];
$finish3 = $finish['3'];
$finish4 = $finish['4'];



#Convert the post code to upper case and trim the variable
$start = strtoupper(trim($start));
$finish0 = strtoupper(trim($finish0));
$finish1 = strtoupper(trim($finish1));
$finish2 = strtoupper(trim($finish2));
$finish3 = strtoupper(trim($finish3));
$finish4 = strtoupper(trim($finish4));

#Remove any spaces
$start = str_replace(" ","",$start);
$finish0 = str_replace(" ","",$finish0);
$finish1 = str_replace(" ","",$finish1);
$finish2 = str_replace(" ","",$finish2);
$finish3 = str_replace(" ","",$finish3);
$finish4 = str_replace(" ","",$finish4);

#Trim the last 3 characters off the end
$start = substr($start,0,strlen($start)-3);
$finish0 = substr($finish0,0,strlen($finish0)-3);
$finish1 = substr($finish1,0,strlen($finish1)-3);
$finish2 = substr($finish2,0,strlen($finish2)-3);
$finish3 = substr($finish3,0,strlen($finish3)-3);
$finish4 = substr($finish4,0,strlen($finish4)-3);

#query the db
$query_start = "SELECT latitude, longitude FROM postcodes WHERE postcode = '$start' LIMIT 1";

$result_start = mysql_query ($query_start);

$num = mysql_num_rows ($result_start); // How many users are there?

if ($num > 0) { // If it ran OK, display the records.

	while ($row = mysql_fetch_array ($result_start, MYSQL_ASSOC)) {
		#Assign variables
		$lat1 = $row["latitude"];
		$long1 = $row["longitude"];
	}

} else { 
	echo '<p>post code not found</p>';
}	

$query_finish = "SELECT latitude, longitude FROM postcodes WHERE postcode = '$finish0' LIMIT 1";
$result_finish = mysql_query ($query_finish);
$num = mysql_num_rows ($result_finish); // How many users are there?
if ($num > 0) { // If it ran OK, display the records.	
	while ($row = mysql_fetch_array ($result_finish, MYSQL_ASSOC)) {
		#Assign variables
		$lat0 = $row["latitude"];
		$long0 = $row["longitude"];
	} 
} else { 
	echo '<p>post code not found</p>';
}		

$query_finish = "SELECT latitude, longitude FROM postcodes WHERE postcode = '$finish1' LIMIT 1";
$result_finish = mysql_query ($query_finish);
$num = mysql_num_rows ($result_finish); // How many users are there?
if ($num > 0) { // If it ran OK, display the records.	
	while ($row = mysql_fetch_array ($result_finish, MYSQL_ASSOC)) {
		#Assign variables
		$lat11 = $row["latitude"];
		$long11 = $row["longitude"];
	} 
} else { 
	echo '<p>post code not found</p>';
}	

$query_finish = "SELECT latitude, longitude FROM postcodes WHERE postcode = '$finish2' LIMIT 1";
$result_finish = mysql_query ($query_finish);
$num = mysql_num_rows ($result_finish); // How many users are there?
if ($num > 0) { // If it ran OK, display the records.	
	while ($row = mysql_fetch_array ($result_finish, MYSQL_ASSOC)) {
		#Assign variables
		$lat2 = $row["latitude"];
		$long2 = $row["longitude"];
	} 
} else { 
	echo '<p>post code not found</p>';
}

$query_finish = "SELECT latitude, longitude FROM postcodes WHERE postcode = '$finish3' LIMIT 1";
$result_finish = mysql_query ($query_finish);
$num = mysql_num_rows ($result_finish); // How many users are there?
if ($num > 0) { // If it ran OK, display the records.	
	while ($row = mysql_fetch_array ($result_finish, MYSQL_ASSOC)) {
		#Assign variables
		$lat3 = $row["latitude"];
		$long3 = $row["longitude"];
	} 
} else { 
	echo '<p>post code not found</p>';
}

$query_finish = "SELECT latitude, longitude FROM postcodes WHERE postcode = '$finish4' LIMIT 1";
$result_finish = mysql_query ($query_finish);
$num = mysql_num_rows ($result_finish); // How many users are there?
if ($num > 0) { // If it ran OK, display the records.	
	while ($row = mysql_fetch_array ($result_finish, MYSQL_ASSOC)) {
		#Assign variables
		$lat4 = $row["latitude"];
		$long4 = $row["longitude"];
	} 
} else { 
	echo '<p>post code not found</p>';
}	

function getDistance0($lat1, $long1, $lat0, $long0){	
	#$earth = 6371; #km change accordingly
	$earth = 3960; #miles
	#Point 1 cords
	$lat1 = deg2rad($lat1);
	$long1= deg2rad($long1);
	#Point 2 cords
	$lat0 = deg2rad($lat0);
	$long0= deg2rad($long0);
	#Haversine Formula
	$dlong=$long0-$long1;
	$dlat=$lat0-$lat1;
	$sinlat=sin($dlat/2);
	$sinlong=sin($dlong/2);
	$a=($sinlat*$sinlat)+cos($lat1)*cos($lat0)*($sinlong*$sinlong);
	$c=2*asin(min(1,sqrt($a)));
	$d=round($earth*$c);
	return $d;
}

function getDistance1($lat1, $long1, $lat11, $long11){
	#$earth = 6371; #km change accordingly
	$earth = 3960; #miles
	#Point 1 cords
	$lat1 = deg2rad($lat1);
	$long1= deg2rad($long1);
	#Point 2 cords
	$lat11 = deg2rad($lat11);
	$long11= deg2rad($long11);
	#Haversine Formula
	$dlong=$long1-$long1;
	$dlat=$lat11-$lat1;
	$sinlat=sin($dlat/2);
	$sinlong=sin($dlong/2);
	$a=($sinlat*$sinlat)+cos($lat1)*cos($lat11)*($sinlong*$sinlong);
	$c=2*asin(min(1,sqrt($a)));
	$d=round($earth*$c);
	return $d;
}

function getDistance2($lat1, $long1, $lat2, $long2){
	#$earth = 6371; #km change accordingly
	$earth = 3960; #miles
	#Point 1 cords
	$lat1 = deg2rad($lat1);
	$long1= deg2rad($long1);
	#Point 2 cords
	$lat2 = deg2rad($lat2);
	$long2= deg2rad($long2);
	#Haversine Formula
	$dlong=$long2-$long1;
	$dlat=$lat2-$lat1;
	$sinlat=sin($dlat/2);
	$sinlong=sin($dlong/2);
	$a=($sinlat*$sinlat)+cos($lat1)*cos($lat2)*($sinlong*$sinlong);
	$c=2*asin(min(1,sqrt($a)));
	$d=round($earth*$c);
	return $d;
}	

function getDistance3($lat1, $long1, $lat3, $long3){
	#$earth = 6371; #km change accordingly
	$earth = 3960; #miles
	#Point 1 cords
	$lat1 = deg2rad($lat1);
	$long1= deg2rad($long1);
	#Point 2 cords
	$lat3 = deg2rad($lat3);
	$long3= deg2rad($long3);
	#Haversine Formula
	$dlong=$long3-$long1;
	$dlat=$lat3-$lat1;
	$sinlat=sin($dlat/2);
	$sinlong=sin($dlong/2);
	$a=($sinlat*$sinlat)+cos($lat1)*cos($lat3)*($sinlong*$sinlong);
	$c=2*asin(min(1,sqrt($a)));
	$d=round($earth*$c);
	return $d;
}	

function getDistance4($lat1, $long1, $lat4, $long4){
	#$earth = 6371; #km change accordingly
	$earth = 3960; #miles
	#Point 1 cords
	$lat1 = deg2rad($lat1);
	$long1= deg2rad($long1);
	#Point 2 cords
	$lat4 = deg2rad($lat4);
	$long4= deg2rad($long4);
	#Haversine Formula
	$dlong=$long4-$long1;
	$dlat=$lat4-$lat1;
	$sinlat=sin($dlat/2);
	$sinlong=sin($dlong/2);
	$a=($sinlat*$sinlat)+cos($lat1)*cos($lat4)*($sinlong*$sinlong);
	$c=2*asin(min(1,sqrt($a)));
	$d=round($earth*$c);
	return $d;
}		

#Returns the distance in miles
$distance0 = getDistance0($lat1, $long1, $lat0, $long0);
#Moved inside isset so wont show when page loads
echo '<p>That\'s a distance of '.$distance0.' miles</p>';	

#Returns the distance in miles
$distance1 = getDistance1($lat1, $long1, $lat11, $long11);
echo '<p>That\'s a distance of '.$distance1.' miles</p>';		

#Returns the distance in miles
$distance2 = getDistance2($lat1, $long1, $lat2, $long2);
echo '<p>That\'s a distance of '.$distance2.' miles</p>';	

#Returns the distance in miles
$distance3 = getDistance3($lat1, $long1, $lat3, $long3);
echo '<p>That\'s a distance of '.$distance3.' miles</p>';	

#Returns the distance in miles
$distance4 = getDistance4($lat1, $long1, $lat4, $long4);
echo '<p>That\'s a distance of '.$distance4.' miles</p>';		
}

?>
<form method="post" action="locate.php">
start: <input type="text" name="start" value="<?php if (isset($_POST['start'])) echo $_POST['start']; ?>" />
course:
            <select name="finish"  size="5">
            <?php // Create the pull-down menu information.
            $query = "SELECT category_id, category FROM categories ORDER BY category ASC";		
            $result = @mysql_query ($query);
            while ($row = mysql_fetch_array ($result, MYSQL_NUM)) {
                echo "<option value=\"$row[0]\"";

                echo ">$row[1]</option>\n";
            }
             ?>
            </select>  

<input type="submit" name="submit" value="Go" />
<input type="hidden" name="submitted" value="TRUE" />
</form>

<?php
include ('includes/footer.html');
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.