Current location: Hot Scripts Forums » Programming Languages » PHP » advanced php function


advanced php function

Reply
  #1 (permalink)  
Old 05-08-07, 04:56 PM
soloWebDev soloWebDev is offline
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.
PHP Code:

$selectedlat $_POST['lattitude'];

$selectedlon $_POST['longitude']; 
<
td>".$selectedlat."</td>                         <td>".$selectedlon."</td
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.8043541.66916, -87.73688"m") . " miles<br>";
echo 
distance(41.740963, -87.8043541.66916, -87.73688"k") . " kilometers<br>";
echo 
distance(41.740963, -87.8043541.66916, -87.73688"n") . " nautical miles<br>"
__________________
We will see what happens next.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 05-08-07, 05:05 PM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
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:
PHP Code:

echo distance(41.740963 , -87.80435$selectedlat $selectedlon,  "m") . " miles<br>"
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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 05-08-07, 05:16 PM
soloWebDev soloWebDev is offline
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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 05-08-07, 05:55 PM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
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:
PHP Code:

ini_set('display_errors''1');
error_reporting(E_ALL); 
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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 05-08-07, 11:32 PM
job0107's Avatar
job0107 job0107 is offline
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.8043541.66916, -87.73688"m"), 5) . " miles<br>";
 echo 
round(distance(41.740963, -87.8043541.66916, -87.73688"k"), 5) . " kilometers<br>";
 echo 
round(distance(41.740963, -87.8043541.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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 05-09-07, 10:58 AM
soloWebDev soloWebDev is offline
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
Code:
<script>
function clear_form()
{
 document.getElementById('temp_state').value = '';
 document.getElementById('temp_city').value = '';
 document.getElementById('temp_lattitude2').value = '';
 document.getElementById('temp_longitude2').value = '';
 }
</script>
Please reference this thread for the other half of this puzzle.
LINK
__________________
We will see what happens next.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 05-09-07, 11:47 PM
job0107's Avatar
job0107 job0107 is offline
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:
  1. <script>
  2. function clear_form()
  3. {
  4.  document.getElementById('temp_state').value = '';
  5.  document.getElementById('temp_city').value = '';
  6.  document.getElementById('state').value = '';
  7.  document.getElementById('city').value = '';
  8.  }
  9. </script>
__________________
Jerry Broughton

Last edited by job0107; 05-10-07 at 12:01 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
execute php into javascript function Hagoromo PHP 3 05-23-07 03:19 AM
how do i call the php function inside the html tag selvam.cit PHP 6 08-24-06 09:25 PM
PHP Function Error IMAGEGIF() phppick PHP 2 03-02-05 10:30 AM
Advanced PHP Programming wizkid PHP 5 03-01-05 09:49 PM
accessing existing ISP email with a PHP webmail script. nlancaster PHP 1 01-07-04 04:28 AM


All times are GMT -5. The time now is 12:27 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.