Reputation: 1059
I am trying to use the php api provided here: https://forums.digitalpoint.com/threads/uk-post-code-distance-calculator-using-php-mysql-3-super-easy-steps.1823109/. There were a few problems in the code which I had to fix but I dont know why I am getting this error.
This is the config.php file:
<?php
include('/config.php');
/*
* Created on May 20, 2010
*
* Programmed by Manoj Kumar
*/
//Connect To Database
$host_sql = $host;
$username_sql = $username;
$password_sql = $password;
$dbname_sql = $dbname;
$con = mysqli_connect($host_sql, $username_sql, $password_sql, $dbname_sql);
?>
And this is the distCalc.php file
<?php
/*
* Created on Jun 1, 2010
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
include('/api/postcodedistance/config.php');
//distCalc("NW4 4TE","ZE3 8HB");
function distCalc($pc1_full,$pc2_full) {
#Convert the post code to upper case and trim the variable
$pc1_full = strtoupper(trim($pc1_full));
#Remove any spaces
$pc1_full = str_replace(" ","",$pc1_full);
#Trim the last 3 characters off the end
$pc1 = substr($pc1_full,0,strlen($pc1_full)-3);
#Convert the post code to upper case and trim the variable
$pc2_full = strtoupper(trim($pc2_full));
#Remove any spaces
$pc2_full = str_replace(" ","",$pc2_full);
#Trim the last 3 characters off the end
$pc2 = substr($pc2_full,0,strlen($pc2_full)-3);
$sql="SELECT * FROM `hwz_postcodes` WHERE `outcode` = '$pc1'";
$result=mysqli_query($con, $sql);
$row=mysqli_fetch_array($result);
$pc1_lat=$row['latitude'];
$pc1_long=$row['longitude'];
$sql="SELECT * FROM `hwz_postcodes` WHERE `outcode` = '$pc2'";
$result=mysqli_query($con, $sql);
$row=mysqli_fetch_array($result);
$pc2_lat=$row['latitude'];
$pc2_long=$row['longitude'];
//echo "<br/>".$pc1."<br/> ".$pc1_lat."<br/> ".$pc1_long;
//echo "<br/>".$pc2."<br/> ".$pc2_lat."<br/> ".$pc2_long;
$distance = getDistance($pc1_lat, $pc1_long, $pc2_lat, $pc2_long);
//echo "<br/>Distance:".$distance;
return $distance;
}
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;
}
?>
This is the error I am getting:
Notice: Undefined variable: con in C:\Users\Kabeer\Dropbox\Projects\project\api\postcodedistance\distCalc.php on line 38
Con is defined so I am confused what the problem is. If I make con global then I get a parsing error. I do it like this:
global $con = mysqli_connect($host_sql, $username_sql, $password_sql, $dbname_sql);
Many thanks
Kabeer
Upvotes: 0
Views: 89
Reputation: 12391
Add the global keyword in your function too:
function distCalc($pc1_full,$pc2_full) {
global $con;
//...
}
And befor you create that connection also:
global $con;
$con = mysqli_connect($host_sql, $username_sql, $password_sql, $dbname_sql);
Upvotes: 1