Functions example from a Well House Consultants training course
More on Functions
[link]
This example is described in the following article(s): • A lazy programmer is a good programmer - [link] |
If you're searching for a page where you can try this code,
select here |
Source code: near.php Module: H105
<?php
/* Distance calculations from grid reference to kms
East and North from a single fixed point, and distance
calculations between two points. */
/* function calls:
array $result = offsets($grid);
Converts an Ordnance Survey grid reference to
East and North distances from a fixed point
$grid - the 8 character grid reference such as
SU007875
$result[0] - distance (units of 100 metres) east
of a fixed point off Cornwall
$result[1] - distance (units of 100 metres) north
of a fixed point off Cornwall
float $result = dist($first, $second);
Calculates the straight distance between two points
and returns the rounded distance. Sets a compass point
direction variable (8 point compass)
$first - an array of the format returned by offsets,
being the point from which to measure
$second - an array of the format returned by offsets,
being the point to which to measure
$result - distance from point $first to point $second.
rounded to nearest 1/10 km, 1km or 10km
depending on the distance
ALSO SETS global variable $dirn to "N", "NE", "NW",
"E", "W", "SE", "SW" or "S" depending on the direction
from $first to $second
string $html = nb6($gridref,$nrows);
Returns an HTML table of the nearest places to the
given grid reference that are defined in a MySQL
database
$gridref - the grid reference of the point of interest
$nrows - the number of rows of nearby places to list
$html - an HTML table with links to nearby places
ALSO USES the global variable $dirn set by the dist
function, and the "sharedata" database from Well House
Consultant's Wiki.
The code in the offsets and dist functions can be cut and
pasted into other applications, or this file imported, and they
will work well in a stand alone mode. The nb6 function is specific
to the Well House Consultants wiki code and serves as an example
here - you'll need to alter the code if you wish to use it
elsewhere.
Code written by Graham Ellis of Well House Consultants
graham@wellho.net / +44 (0) 1225 708225
*/
function offsets($from = "ST907633") {
$gridrefs = <<< GRIDREFS
# Grid references - bottom left corner of square
# This file contains the two letter codes used by the British
# map reference systems. For example, ST914628 is:
# From the West 300 x 10 + 914 = 3914
# From the South 100 x 10 + 628 = 1628
# Distances measured in tenths of a kilometre.
0 0 SV SW SX SY SZ TV
100 100 SR SS ST SU TQ TR
100 200 SM SN SO SP TL TM
200 300 SH SJ SK TF TG
200 400 SC SD SE TA
100 500 NW NX NY NZ OV
100 600 NR NS NT NU GX
0 700 NL NM NN NO
0 800 NF NG NH NJ NK
0 900 NA NB NC ND
100 1000 HW HX HY HZ
300 1100 HT HU
400 1200 HP
GRIDREFS;
###############################################################
foreach (explode("\n",$gridrefs) as $line) {
$line = preg_replace("/#.*/","",trim($line));
$fields = explode(" ",$line);
if (count($fields) < 3) continue;
for ($k=2; $k<count($fields); $k++) {
$twolettersx[$fields[$k]] = $fields[0] + 100 * ($k-2);
$twolettersy[$fields[$k]] = $fields[1] ;
}
}
# define a point we want to highlight on the map and convert it to km from (0,0)
$highpoint = $from; #"ST914628";
preg_match("/(..)(...)(...)/",$highpoint,$parts);
$xlocn = $twolettersx[$parts[1]] * 10 + $parts[2];
$ylocn = $twolettersy[$parts[1]] * 10 + $parts[3];
return array($xlocn,$ylocn);
}
function dist($from, $to) {
global $dirn;
$dx = $from[0] - $to[0];
$dy = $from[1] - $to[1];
$di1= "E"; $di2 = "N";
if ($dx > 0) $di1 = "W";
if ($dy > 0) $di2 = "S";
$dirn = (abs($dx) > abs($dy)) ? $di1 : $di2;
if (abs(abs($dx)-abs($dy))/(abs($dx)+abs($dy)) < 0.6)
$dirn = "$di2$di1";
$d = sqrt($dx*$dx+$dy*$dy) / 10.0;
$rv = round($d,1);
if ($d > 5.0) $rv = round ($d,0);
if ($d > 50.0) $rv = round ($d,-1);
return $rv;
}
function nb6($base = "ST907633",$nln = 5) {
global $dirn;
$bxy = offsets($base);
$already = array();
# mysql_pconnect("www.wellho.net","login","password");
# mysql_select_db("wellho");
$rset = mysql_query("select * from sharedata");
while ($row = mysql_fetch_assoc($rset)) {
if (ereg('\[map=(........)\]([^\[]*)\[/map\]',$row[info],$gotten)) {
if ($gotten[1] == $base) continue;
if (ereg("area",$row[pagename])) continue;
if ($already[$gotten[1]]) continue;
$txy = offsets($gotten[1]);
$distance = dist ($bxy,$txy);
$dhash[$row[pagename]] = $distance;
$way[$row[pagename]] = $dirn;
$pnhash[$row[pagename]] = $gotten[2];
$already[$gotten[1]] = 1;
}
}
asort($dhash);
$prev = "";
$html = "<br><table width=100% cellpadding=2><tr class=generaltext>".
"<th>Pages nearby ...</th>";
$count = 0;
$perline = 3;
foreach (array_keys($dhash) as $pname) {
if ($prev == $pnhash[$pname]) continue;
$html .= "<td width=33%><a href=/share/$pname.html>$pnhash[$pname]</a><br>";
$html .= "$dhash[$pname] kms $way[$pname]</td>";
$prev = $pnhash[$pname];
$count++;
if ($count == $nln * $perline - 1) break;
if ($count%$perline == $perline-1)
$html .= "</tr><tr class=generaltext>";
}
$html .= "</tr></table>";
return $html;
}
?>
Learn about this subject
Books covering this topic
Yes. We have over 700 books in our library.
Books
covering PHP are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Functions" training module. You'll find a description of the topic and some
other closely related examples on the
"Functions" module index page.
Full description of the source code
The complete training module that describes this source code
is availble for download (for limited use) from
our download centre.
Other resources
• Our
Solutions centre provides a number of longer technical articles.
• Our
Opentalk forum archive provides a question and answer centre.
•
The Horse's mouth provides a daily tip or thought.
• Further resources are available via the
resources centre.
• All of these resources can be searched through through our
search engine
• And there's a global index
here.
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Web site author
This web site is written and maintained by
Well House Consultants.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job.
Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are
forbidden from using it for running your own training courses
without our prior written permission. See
our
page on courseware provision for more details.
Any of our images within this code may NOT be reused on a public URL without our
prior permission. For Bona Fide personal use, we will often grant you permission provided
that you provide a link back. Commercial use on a website will incur a license fee for
each image used - details on request.