05-08-07, 04:56 PM
Wannabe Coder
Join Date: Apr 2007
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
advanced php function
This is the line i am having trouble with.
echo distance("41.740963" , "-87.80435", "'.$selectedlat.'" , "'.$selectedlon.'" , "m") . " miles<br>";
I am wondering if i am calling these wrong, in this which is above this function, the lat and lon are being displayed correctly.
but when i try to plug them into the function, they do not work.
PHP Code:
function distance ( $lat1 , $lon1 , $lat2 , $lon2 , $unit ) {
$theta = $lon1 - $lon2 ;
$dist = sin ( deg2rad ( $lat1 )) * sin ( deg2rad ( $lat2 )) + cos ( deg2rad ( $lat1 )) * cos ( deg2rad ( $lat2 )) * cos ( deg2rad ( $theta ));
$dist = acos ( $dist );
$dist = rad2deg ( $dist );
$miles = $dist * 60 * 1.1515 ;
$unit = strtoupper ( $unit );
if ( $unit == "K" ) {
return ( $miles * 1.609344 );
} else if ( $unit == "N" ) {
return ( $miles * 0.8684 );
} else {
return $miles ;
}
}
echo distance ( "41.740963" , "-87.80435" , "'. $selectedlat .'" , "'. $selectedlon .'" , "m" ) . " miles<br>" ;
echo distance ( 41.740963 , - 87.80435 , 41.66916 , - 87.73688 , "m" ) . " miles<br>" ;
echo distance ( 41.740963 , - 87.80435 , 41.66916 , - 87.73688 , "k" ) . " kilometers<br>" ;
echo distance ( 41.740963 , - 87.80435 , 41.66916 , - 87.73688 , "n" ) . " nautical miles<br>" ;
__________________
We will see what happens next.
05-08-07, 05:05 PM
Community Liaison
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
this is how you should call the function:
you don't need to convert the variables to string first, you can pass them to the function right away
__________________
"Good judgement comes from experience, and experience comes from bad judgement." -
Fred Brooks
05-08-07, 05:16 PM
Wannabe Coder
Join Date: Apr 2007
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
UnrealEd, Thanks for responding so quickly.
Any thoughts why it still might not be working?
Any thing you might suggest in order for this to work?
__________________
We will see what happens next.
05-08-07, 05:55 PM
Community Liaison
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
are you recieving any errors? if not, place this on top of your script:
this will cause php to display all errors
i don't know the formula to calculate the distance across a sphere's surface by hard, so i don't know if you have an error in there, but i think not
__________________
"Good judgement comes from experience, and experience comes from bad judgement." -
Fred Brooks
05-08-07, 11:32 PM
Community Liaison
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I don't know if your math is correct or if your answers are correct. But for whatever it is, this does calculate the values submitted and gives you the results. I don't know what precision you need so I set it to 5 decimals.
PHP Code:
<?php $selectedlat = $_POST [ "lattitude" ]; $selectedlon = $_POST [ "longitude" ]; echo "<table><tr><td>" . $selectedlat . "</td><td>" . $selectedlon . "</td></tr></table>" ; function distance ( $lat1 , $lon1 , $lat2 , $lon2 , $unit ) { $theta = $lon1 - $lon2 ; $dist = sin ( deg2rad ( $lat1 )) * sin ( deg2rad ( $lat2 )) + cos ( deg2rad ( $lat1 )) * cos ( deg2rad ( $lat2 )) * cos ( deg2rad ( $theta )); $dist = acos ( $dist ); $dist = rad2deg ( $dist ); $miles = $dist * 60 * 1.1515 ; $unit = strtoupper ( $unit ); if ( $unit == "K" ) { return ( $miles * 1.609344 ); } else if ( $unit == "N" ) { return ( $miles * 0.8684 ); } else { return $miles ; } } if(! $selectedlat || ! $selectedlon ) { echo "<form action='#' method='post'>" ; echo "Enter Lattitude : <input type='text' name='lattitude'><br>" ; echo "Enter Longitude : <input type='text' name='longitude'><br>" ; echo "<input type='submit' value='Submit'>" ; echo "</form>" ; } else { echo round ( distance ( 41.740963 , - 87.80435 , $selectedlat , $selectedlon , "m" ), 5 ) . " miles<br>" ; echo round ( distance ( 41.740963 , - 87.80435 , 41.66916 , - 87.73688 , "m" ), 5 ) . " miles<br>" ; echo round ( distance ( 41.740963 , - 87.80435 , 41.66916 , - 87.73688 , "k" ), 5 ) . " kilometers<br>" ; echo round ( distance ( 41.740963 , - 87.80435 , 41.66916 , - 87.73688 , "n" ), 5 ) . " nautical miles<br>" ; } ?>
Obviously your form must be functioning properly if you are able to display the posted values.
__________________
Jerry Broughton
Last edited by job0107; 05-08-07 at 11:59 PM .
05-09-07, 10:58 AM
Wannabe Coder
Join Date: Apr 2007
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you to everyone who helped out, here is the completed working code. Thanks again, really great team work by everyone. Which I have noticed is how this forum, stands out beyond the others. Everyuone works well with everyone else, and the forum is filled with inteligent, nice, coders. Thanks again.
PHP Code:
<?php
$selectedState = $_POST [ 'state' ];
$selectedCity = $_POST [ 'city' ];
// Lattitude, Longitude Selection
$qL = "SELECT lattitude, longitude FROM table WHERE city = ' $selectedCity '" ;
$rL = mysql_query ( $qL ) or die( "Invalid query: $qL " );
while( $data = mysql_fetch_array ( $rL ))
{
$selectedlat2 = $data [ 'lattitude' ];
$selectedlon2 = $data [ 'longitude' ];
}
//State Selection
if(! $selectedCity )
{
echo "<div>" ;
echo "<form name='theForm' action='#' method='post'>" ;
$qS = "SELECT DISTINCT state FROM table ORDER BY state" ;
$ $rS = mysql_query ( $qS ) or die( "Invalid query: $qS " );
echo "<select name='state' onchange='theForm.submit()'>" ;
echo "<option value=''>Select a state</option>" ;
while( $data = mysql_fetch_array ($ $rS ))
{
if( $data [ 'state' ] == $selectedState )
{
echo "<option value='" . $data [ 'state' ]. "' SELECTED>" . $data [ 'state' ]. "</option>" ;
}
else
{
echo "<option value='" . $data [ 'state' ]. "'>" . $data [ 'state' ]. "</option>" ;
}
}
echo "</select>" ;
//City & ZIP Selection
if( $selectedState )
{
$qC = "SELECT city, lattitude, longitude FROM table WHERE state = ' $selectedState ' ORDER BY city" ;
$ $rC = mysql_query ( $qC ) or die( "Invalid query: $qC " );
echo "<select name='city' onchange='theForm.submit()'>" ;
echo "<option value=''>Select a city</option>" ;
while( $data = mysql_fetch_array ($ $rC ))
{
echo "<option value='" . $data [ 'city' ]. "'>" . $data [ 'city' ]. "</option>" ;
//echo "<input type='hidden' value='".data['zip_Code']."'>";
}
echo "</select>" ;
echo "<input id='temp_state' name='temp_state' type='hidden' value=' $selectedState '>" ;
echo "<input id='temp_city' name='temp_city' type='hidden' value=' $selectedCity '>" ;
echo "<input id='temp_lattitude' name='temp_lattitude2' type='hidden' value=' $selectedlat2 '>" ;
echo "<input id='temp_longitude' name='temp_longitude2' type='hidden' value=' $selectedlon2 '>" ;
}
echo "</form>" ;
echo "</div>" ;
}
else
{
echo "<table>
<tr>
<td>State</td>
<td>City</td>
<td>lat</td>
<td>lon</td>
</tr>
<tr>
<td>" . $selectedState . "</td>
<td>" . $selectedCity . "</td>
<td>" . $selectedlat2 . "</td>
<td>" . $selectedlon2 . "</td>
</tr>
</table>" ;
echo "<table><tr><td>" . $selectedCity . "</td><td>" . $selectedlat2 . "</td><td>" . $selectedlon2 . "</td></tr></table>" ;
function distance ( $lat1 , $lon1 , $lat2 , $lon2 , $unit )
{
$theta = $lon1 - $lon2 ;
$dist = sin ( deg2rad ( $lat1 )) * sin ( deg2rad ( $lat2 )) + cos ( deg2rad ( $lat1 )) * cos ( deg2rad ( $lat2 )) * cos ( deg2rad ( $theta ));
$dist = acos ( $dist );
$dist = rad2deg ( $dist );
$miles = $dist * 60 * 1.1515 ;
$unit = strtoupper ( $unit );
if ( $unit == "K" )
{
return ( $miles * 1.609344 );
}
else if ( $unit == "N" )
{
return ( $miles * 0.8684 );
}
else
{
return $miles ;
}
}
echo round ( distance ( 41.740963 , - 87.80435 , $selectedlat2 , $selectedlon2 , "m" ), 1 ) . " miles<br>" ;
if( $selectedState && $selectedCity && $selectedlat2 && $selectedlon2 )
{
echo "<script>clear_form()</script>" ;
}
}
?>
The js
Please reference this thread for the other half of this puzzle.
LINK
__________________
We will see what happens next.
05-09-07, 11:47 PM
Community Liaison
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
If I might make a few suggestions, The program as you have it at the moment, will not let you select more than one state. I have made a few changes that should fix the problems.
PHP Code:
<?php function distance ( $lat1 , $lon1 , $lat2 , $lon2 , $unit ) { $dist = rad2deg ( acos ( sin ( deg2rad ( $lat1 )) * sin ( deg2rad ( $lat2 )) + cos ( deg2rad ( $lat1 )) * cos ( deg2rad ( $lat2 )) * cos ( deg2rad ( $lon1 - $lon2 )))); $miles = $dist * 69.09 ; $unit = strtoupper ( $unit ); return $miles = ( $unit == "K" ) ? ( $miles *= 1.609344 ) : ( $unit == "N" ) ? ( $miles *= .8684 ) : $miles ; } $selectedState = $_POST [ "temp_state" ];if(! $selectedState ){ $selectedState = $_POST [ "state" ];} $selectedCity = $_POST [ "temp_city" ];if(! $selectedCity ){ $selectedCity = $_POST [ "city" ];} //State Selection if(! $selectedCity ) { echo "<div>" ; echo "<form name='theForm' action='#' method='post'>" ; $qS = "SELECT DISTINCT state FROM table ORDER BY state" ; $rS = mysql_query ( $qS ) or die( "Invalid query: $qS " ); echo "<select id='state' name='state' onchange='theForm.submit()'>" ; echo "<option value=''>Select a state</option>" ; while( $data = mysql_fetch_array ( $rS )) { if( $data [ 'state' ] == $selectedState ) { echo "<option value='" . $data [ 'state' ]. "' SELECTED>" . $data [ 'state' ]. "</option>" ; } else { echo "<option value='" . $data [ 'state' ]. "'>" . $data [ 'state' ]. "</option>" ; } } echo "</select>" ; //City & ZIP Selection if( $selectedState ) { $qC = "SELECT city, lattitude, longitude FROM table WHERE state = ' $selectedState ' ORDER BY city" ; $rC = mysql_query ( $qC ) or die( "Invalid query: $qC " ); echo "<select id='city' name='city' onchange='theForm.submit()'>" ; echo "<option value=''>Select a city</option>" ; while( $data = mysql_fetch_array ( $rC )) { echo "<option value='" . $data [ 'city' ]. "'>" . $data [ 'city' ]. "</option>" ; } echo "</select>" ; echo "<input id='temp_state' name='temp_state' type='hidden' value=' $selectedState '>" ; echo "<input id='temp_city' name='temp_city' type='hidden' value=' $selectedCity '>" ; } echo "</form>" ; echo "</div>" ; } else { // Lattitude, Longitude Selection $qL = "SELECT lattitude, longitude FROM table WHERE city = ' $selectedCity '" ; $rL = mysql_query ( $qL ) or die( "Invalid query: $qL " ); while( $data = mysql_fetch_array ( $rL )) { $selectedlat2 = $data [ 'lattitude' ]; $selectedlon2 = $data [ 'longitude' ]; } echo "<table> <tr> <td>State</td> <td>City</td> <td>lat</td> <td>lon</td> </tr> <tr> <td>" . $selectedState . "</td> <td>" . $selectedCity . "</td> <td>" . $selectedlat2 . "</td> <td>" . $selectedlon2 . "</td> </tr> </table> <table> <tr> <td>" . $selectedCity . "</td> <td>" . $selectedlat2 . "</td> <td>" . $selectedlon2 . "</td> </tr> </table>" ; echo round ( distance ( 41.740963 , - 87.80435 , $selectedlat2 , $selectedlon2 , "m" ), 1 ) . " miles<br>" ; echo "<script>clear_form()</script>" ; echo "<p><button onclick='theForm.submit()'>Make another selection?</button></p>" ; } ?>
And the js
Javascript Code:
<script>
function clear_form( )
{
document.getElementById ( 'temp_state' ) .value = '' ;
document.getElementById ( 'temp_city' ) .value = '' ;
document.getElementById ( 'state' ) .value = '' ;
document.getElementById ( 'city' ) .value = '' ;
}
</script>
__________________
Jerry Broughton
Last edited by job0107; 05-10-07 at 12:01 AM .
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off