Jump to content

Recommended Posts

Hi There,

 

I have been trying to implement like this  into a website.

http://www.acmultimedia.co.uk/blog/article.php?blogID=59

 

I have ran the SQL file and set the data base up but am completly lost with what to do next!

 

Do I have ALL the code in the tutorial in the page that the form connects to??

 

Completly lost!

Do you know any php or html as some knowledge will be know to help you do this.

 

First you need to set up a simple form to put in the post code you want to compare.  Then you need that to send the data to your script file using POST/GET.

 

You will need to specify, what other post code you want to compare it.

 

All the code that was provided in that blog can sit in one file.

 

Try that first, then ask if you need more help.  Good Luck

Ok, heres the form

 

<form name="locator" method="get" action="postcode.php" id="storeFinder">
                <label for="p" class="hidden">Post code</label>
                <input type="text" name="p" id="p" value="" maxlength="8" class="storeLocatorText" />
               
                <input type="submit" class="buttonStandard" value="Search" style="margin-left:5px;float:left;width:75px;" />
                </form>

 

and postcode.php is

 

<?php

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

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

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



"SELECT * FROM tblhwz_postcodes WHERE fldPostCode = '$postcode_first' LIMIT 1";

Does the post code exist?
if ($myrow["fldPostCode"] != ""){

#Assign variables
$code1_lat = $myrow["fldLatitude"];
$code1_long = $myrow["fldLongitude"];

}else{

#post code not found

}

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($code1_lat, $code1_long, $code2_lat, $code2_long);

?>


 

just returns a blank page at the moment though...

 

im not entirley sure the script is set up right for the field names in the database...?

 

database structure is

 

CREATE TABLE `hwz_postcodes` (
  `outcode` varchar(5) NOT NULL default '',
  `x` int(11) NOT NULL default '0',
  `y` int(11) NOT NULL default '0',
  `latitude` int(11) NOT NULL default '0',
  `longitude` int(11) NOT NULL default '0',
  PRIMARY KEY  (`outcode`),
  KEY `x` (`x`,`y`)
) TYPE=MyISAM COMMENT='stores hwz postcode data and coords';

Firstly you need to convert your Get Data to a $var called $postcode so in your postcodes.php page put :

$postcode = $_GET['p'];

At the top of your page.  Then you will need to actually connect to your database via mysql_connect()...  Look it up on the php.net manual.  In fact look up how to do basic queries in mysql, there are a couple more functions you will need to use to make your statement work (mysql_query() & mysql_result()).  And you statement is written wrong.  Change :

"SELECT * FROM tblhwz_postcodes WHERE fldPostCode = '$postcode_first' LIMIT 1";

to

$qry = mysql_query("SELECT * FROM tblhwz_postcodes WHERE fldPostCode = '" . $postcode_first . "' LIMIT 1");

Finally right at the bottom before ?> add something like :

echo $distance;

To present something to your page, otherwise it will remain blank as you say.

The problem with your database is that your not actually connecting to it at all!

 

I suggest going to the w3schools website to look up about GET and POST data, and see the php manual on mysql functions.  Good Luck.

  • 4 weeks later...

Ok, getting a bit further with it (I hope!)

 

search.php

 

<form name="form1" method="post" action="postcode_search.php">
  <label></label>
  <table width="400" border="0" cellspacing="3" cellpadding="3">
    <tr>
      <td><label><strong>Post Code: </strong></label></td>
      <td><input type="text" name="postcode" id="postcode"></td>
    </tr>
    <tr>
      <td> </td>
      <td><label>
        <input type="submit" name="search" id="search.php" value="search">
      </label></td>
    </tr>
  </table>
  <p> </p>
  <p>
    <label></label>
  </p>
  <p>
    <label></label>
  </p>
  <p>
    <label></label>
  </p>
</form>

 

postcode_search.php

 

<?php
$postcode = $_GET['p'];

ini_set ("display_errors", "1");
error_reporting(E_ALL);
?>



<?php
$hostname = "localhost";
$database = "viewmykitchen1";
$username = "test";
$password = "test";
$database = mysql_pconnect($hostname, $username,
$password) or die(mysql_error());
?>

<?php

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

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

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

$postcode =($_POST['postcode'])); 

$qry = mysql_query("SELECT * FROM tblhwz_postcodes WHERE fldPostCode = '" . $postcode_first . "' LIMIT 1");


?>


<?php

Does the post code exist?
if ($myrow["fldpostcode"] != ""){

#Assign variables
$code1_lat = $myrow["fldlatitude"];
$code1_long = $myrow["fldlongitude"];

}else{

#post code not found

}

?>



<?php

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;

}

?>

<?php

#Returns the distance in miles
$distance = getDistance($code1_lat, $code1_long, $code2_lat, $code2_long);

echo $distance;

?>


 

Am I getting any further with this???

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.