WHERE sc.`id` IN (44,48) AND sc.`published`= 1 AND sc.`deleted`= 0
WHERE sc.`parent` IN (42) AND sc.`published`= 1 AND sc.`deleted`= 0
(but you have hardcoded lat and long in your query now??)
What do you mean by this??
(((acos(sin((51.4926591*pi()/180)) * sin((lat_tvcv.value*pi()/180))
+cos((51.4926591*pi()/180)) * cos((lat_tvcv.value*pi()/180)) * cos(((-0.0542387- lng_tvcv.value)
*pi()/180))))*180/pi())*60*1.1515) (((acos(sin((".$latitude."*pi()/180)) * sin((lat_tvcv.value*pi()/180))
+cos((".$latitude."*pi()/180)) * cos((lat_tvcv.value*pi()/180)) * cos(((".$longitude."- lng_tvcv.value)
*pi()/180))))*180/pi())*60*1.1515)Perhaps it would be an idea to just get rid of everything, except the curl class and the query and start from scratch, but in a mondx context.
this idea is not so bad

&parents='209,304,418';
$parents= isset($parents) ? $parents: '0';

If you can’t see a map here, or the location of the centre is completely wrong, please let us know and we will get our finest team trained monkeys working on the bug right away. Contact us at [email protected].;)

WHERE sc.`parent` IN ('.$parents.') AND sc.`published`= 1 AND sc.`deleted`= 0 <?php
/**
* Set variables
*/
//$parents= isset($parents) ? $parents: '0'; // Where the documents containing the lat and long coordinates are located
//$tpl=isset($tpl) ? $tpl: ''; // The template chunk to use when outputting the returned documents
//$display=isset($display) ? $display: '50'; // The number of results to display. Default 50.
/**
* The curl class
*/
class curl {
/**
* Constructor
*/
function curl() {
}
function init_curl($ch,$url,$postfields=null,$follow=null,$cookie=null,$referer=null) {
// Set url
curl_setopt($ch, CURLOPT_URL, $url);
// Enable Post
if($postfields) {
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postfields);
}
if($follow) {
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1 );
}
if($referer) {
curl_setopt($ch, CURLOPT_REFERER, $referer);
}
//Enable SSL
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
//Return results as string
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
return $ch;
} // end function
/*
Grabs a page
*/
function get_page($options) {
//Set options
foreach($options AS $key=>$value) {
$$key = $value;
}
$ch = curl_init();
$ch = $this->init_curl($ch,$url,$postfields,$follow,$cookie);
$page = curl_exec($ch);
curl_close($ch);
return $page;
}
} // end class
/**
* A postcode finder class
*
*/
class postcode_finder {
/**
* Constructor
*
*/
function postcode_finder($array) {
if(is_array($array)) {
foreach($array AS $key=>$value) {
$this->$key = $value;
}
}
//DO stuff
}
/**
/**
* Returns a lat / long of a given postcode
*
*/
function get_lat_long($postcode,$domain=null) {
if(!$domain) {
$domain = "co.uk";
}
$url = "http://maps.google.co.uk/maps/geo?q=" . urlencode($postcode) . "&output=json&key=ABQIAAAAdN8m5F_H4c8QnCWINywTCRQismpbGqJLdUil0k2vBhWw9rjFrhQVLNLfqmHZNGHxY1tlASp_Wx06bQ";
$json = $this->curl->get_page(array("url"=>$url));
$store_data = json_decode(str_replace(""","\"",htmlentities($json))); //Take care of accents
$lng = $store_data->Placemark[0]->Point->coordinates[0];
$lat = $store_data->Placemark[0]->Point->coordinates[1];
//Return
if($lng && $lat) {
return array('lat'=>$lat,
'lng'=>$lng
);
} else {
return false;
}
}
/**
* Get a list of our stores, sorted by distance to this postcode
*
*/
function get_stores_list($postcode) {
//If it's a UK postcode then format correctly
$postcode = $this->checkPostcode($postcode);
$latlng = $this->get_lat_long($postcode);
if(!$latlng) { //Unrecognised postcode
return false;
}
$latitude = $latlng['lat'];
$longitude = $latlng['lng'];
//print_r($latlng);//For Debugging
// print_r($addr);//For Debugging (does not seem to work)
$parents="304";//MAKE THIS MULTIPLE PARENTS
global $modx;
$query = "SELECT distinct
sc.pagetitle,
sc.content,
sc.id,
(((acos(sin((".$latitude."*pi()/180)) * sin((lat_tvcv.value*pi()/180))
+cos((".$latitude."*pi()/180)) * cos((lat_tvcv.value*pi()/180)) * cos(((".$longitude."- lng_tvcv.value)
*pi()/180))))*180/pi())*60*1.1515)
as distance
FROM (`modx_site_content` AS sc,
`modx_site_tmplvars` AS lat_tv ,
`modx_site_tmplvars` AS lng_tv)
LEFT JOIN `modx_site_tmplvar_contentvalues` AS lat_tvcv
ON lat_tvcv.contentid = sc.id AND lat_tvcv.tmplvarid = lat_tv.id
LEFT JOIN `modx_site_tmplvar_contentvalues` AS lng_tvcv
ON lng_tvcv.contentid = sc.id AND lng_tvcv.tmplvarid = lng_tv.id
WHERE sc.`parent` IN (".$parents.") AND sc.`published`= '1' AND sc.`deleted`= '0'
AND lat_tv.name = 'latitude'
AND lng_tv.name = 'longitude'
ORDER BY distance ASC
LIMIT 0,50
";
$rs=$modx->db->query($query);
$stores=$modx->db->makeArray($rs);
//print_r($stores); //For debugging.
return $stores;
}
/**
* Checks whether supplied postcode is a valid UK postcode
*/
function checkPostcode($toCheck) {
$orig = $toCheck;
// Permitted letters depend upon their position in the postcode.
$alpha1 = "[abcdefghijklmnoprstuwyz]"; // Character 1
$alpha2 = "[abcdefghklmnopqrstuvwxy]"; // Character 2
$alpha3 = "[abcdefghjkstuw]"; // Character 3
$alpha4 = "[abehmnprvwxy]"; // Character 4
$alpha5 = "[abdefghjlnpqrstuwxyz]"; // Character 5
// Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
$pcexp[0] = '^('.$alpha1.'{1}'.$alpha2.'{0,1}[0-9]{1,2})([0-9]{1}'.$alpha5.'{2})$';
// Expression for postcodes: ANA NAA
$pcexp[1] = '^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([0-9]{1}'.$alpha5.'{2})$';
// Expression for postcodes: AANA NAA
$pcexp[2] = '^('.$alpha1.'{1}'.$alpha2.'[0-9]{1}'.$alpha4.')([0-9]{1}'.$alpha5.'{2})$';
// Exception for the special postcode GIR 0AA
$pcexp[3] = '^(gir)(0aa)$';
// Standard BFPO numbers
$pcexp[4] = '^(bfpo)([0-9]{1,4})$';
// c/o BFPO numbers
$pcexp[5] = '^(bfpo)(c\/o[0-9]{1,3})$';
// Load up the string to check, converting into lowercase and removing spaces
$postcode = strtolower($toCheck);
$postcode = str_replace (' ', '', $postcode);
// Assume we are not going to find a valid postcode
$valid = false;
// Check the string against the six types of postcodes
foreach ($pcexp as $regexp) {
if (ereg($regexp,$postcode, $matches)) {
// Load new postcode back into the form element
$toCheck = strtoupper ($matches[1] . ' ' . $matches [2]);
// Take account of the special BFPO c/o format
$toCheck = ereg_replace ('C\/O', 'c/o ', $toCheck);
// Remember that we have found that the code is valid and break from loop
$valid = true;
break;
}
}
// Return with the reformatted valid postcode in uppercase if the postcode was
// valid
if ($valid){
return $toCheck;
} else {
$this->non_standard_postcode = true;
return $orig;
};
}
}
//If we have a post
if($_GET['postcode']) {
$curl = new curl();
$finder = new postcode_finder(array('curl'=>$curl));
$stores = $finder->get_stores_list($_GET['postcode']);
}
?>
<form action="" method="GET">
<fieldset>
<div style="float:left; width:370px; font-size:15px; font-weight:bold; margin-right:18px">Search for your nearest free and independent advice centre. Just enter your postcode or the name of your town to get started.</div>
<input type="text" name="postcode" value="<?= $_GET['postcode'] ?>" style="font-size:20px; padding:4px; font-weight:bold; font-family:arial" size="40" maxlength="80">
<input type="submit" value="SEARCH" style="font-size:15px; padding:4px">
</fieldset>
</form>
<? if ($_GET['postcode']) { ?>
<? if ($stores) { ?>
<h2 style="background-color:padding:2px">The following centres were found near to you</h2>
<?
$docids=array();
foreach($stores AS $store) {
$modx->setPlaceholder($store['id'].'.distance',number_format($store['distance'],2));
$docids[]=$store['id'];
}
$params=array();
$params['documents']=implode(',',$docids);
$params['tpl']='listtpl';
$params['parents']='$parents';
$params['display']='10';
$params['extenders']='customsort,request';
return $modx->runSnippet('Ditto',$params);
} else { ?>
<h2 style="background-color:padding:2px">That postcode was not recognised</h2>
<? } ?>
<? } ?>
</div>
</div>
</body>
</html>
?><?php
/**
* Set variables
*/
$parents= isset($parents) ? $parents: '0'; // Where the documents containing the lat and long coordinates are located
$tpl=isset($tpl) ? $tpl: 'listtpl'; // The template chunk to use when outputting the returned documents
$display=isset($display) ? $display: '50'; // The number of results to display. Default 50.
/**
* The curl class
*/
class curl {
/**
* Constructor
*/
function curl() {
}
function init_curl($ch,$url,$postfields=null,$follow=null,$cookie=null,$referer=null) {
// Set url
curl_setopt($ch, CURLOPT_URL, $url);
// Enable Post
if($postfields) {
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postfields);
}
if($follow) {
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1 );
}
if($referer) {
curl_setopt($ch, CURLOPT_REFERER, $referer);
}
//Enable SSL
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
//Return results as string
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
return $ch;
} // end function
/*
Grabs a page
*/
function get_page($options) {
//Set options
foreach($options AS $key=>$value) {
$$key = $value;
}
$ch = curl_init();
$ch = $this->init_curl($ch,$url,$postfields,$follow,$cookie);
$page = curl_exec($ch);
curl_close($ch);
return $page;
}
} // end class
/**
* A postcode finder class
*
*/
class postcode_finder {
/**
* Constructor
*
*/
function postcode_finder($array) {
if(is_array($array)) {
foreach($array AS $key=>$value) {
$this->$key = $value;
}
}
//DO stuff
}
/**
/**
* Returns a lat / long of a given postcode
*
*/
function get_lat_long($postcode,$domain=null) {
if(!$domain) {
$domain = "co.uk";
}
$url = "http://maps.google.co.uk/maps/geo?q=" . urlencode($postcode) . "&output=json&key=ABQIAAAAdN8m5F_H4c8QnCWINywTCRQismpbGqJLdUil0k2vBhWw9rjFrhQVLNLfqmHZNGHxY1tlASp_Wx06bQ";
$json = $this->curl->get_page(array("url"=>$url));
$store_data = json_decode(str_replace(""","\"",htmlentities($json))); //Take care of accents
$lng = $store_data->Placemark[0]->Point->coordinates[0];
$lat = $store_data->Placemark[0]->Point->coordinates[1];
//Return
if($lng && $lat) {
return array('lat'=>$lat,
'lng'=>$lng
);
} else {
return false;
}
}
/**
* Get a list of our stores, sorted by distance to this postcode
*
*/
function get_stores_list($postcode,$parents) {
//If it's a UK postcode then format correctly
$postcode = $this->checkPostcode($postcode);
$latlng = $this->get_lat_long($postcode);
if(!$latlng) { //Unrecognised postcode
return false;
}
$latitude = $latlng['lat'];
$longitude = $latlng['lng'];
//print_r($latlng);//For Debugging
// print_r($addr);//For Debugging (does not seem to work)
//$parents="304";//MAKE THIS MULTIPLE PARENTS
global $modx;
$query = "SELECT distinct
sc.pagetitle,
sc.content,
sc.id,
(((acos(sin((".$latitude."*pi()/180)) * sin((lat_tvcv.value*pi()/180))
+cos((".$latitude."*pi()/180)) * cos((lat_tvcv.value*pi()/180)) * cos(((".$longitude."- lng_tvcv.value)
*pi()/180))))*180/pi())*60*1.1515)
as distance
FROM (`modx_site_content` AS sc,
`modx_site_tmplvars` AS lat_tv ,
`modx_site_tmplvars` AS lng_tv)
LEFT JOIN `modx_site_tmplvar_contentvalues` AS lat_tvcv
ON lat_tvcv.contentid = sc.id AND lat_tvcv.tmplvarid = lat_tv.id
LEFT JOIN `modx_site_tmplvar_contentvalues` AS lng_tvcv
ON lng_tvcv.contentid = sc.id AND lng_tvcv.tmplvarid = lng_tv.id
WHERE sc.`parent` IN (".$parents.") AND sc.`published`= '1' AND sc.`deleted`= '0'
AND lat_tv.name = 'latitude'
AND lng_tv.name = 'longitude'
ORDER BY distance ASC
LIMIT 0,50
";
$rs=$modx->db->query($query);
$stores=$modx->db->makeArray($rs);
//print_r($stores); //For debugging.
return $stores;
}
/**
* Checks whether supplied postcode is a valid UK postcode
*/
function checkPostcode($toCheck) {
$orig = $toCheck;
// Permitted letters depend upon their position in the postcode.
$alpha1 = "[abcdefghijklmnoprstuwyz]"; // Character 1
$alpha2 = "[abcdefghklmnopqrstuvwxy]"; // Character 2
$alpha3 = "[abcdefghjkstuw]"; // Character 3
$alpha4 = "[abehmnprvwxy]"; // Character 4
$alpha5 = "[abdefghjlnpqrstuwxyz]"; // Character 5
// Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
$pcexp[0] = '^('.$alpha1.'{1}'.$alpha2.'{0,1}[0-9]{1,2})([0-9]{1}'.$alpha5.'{2})$';
// Expression for postcodes: ANA NAA
$pcexp[1] = '^('.$alpha1.'{1}[0-9]{1}'.$alpha3.'{1})([0-9]{1}'.$alpha5.'{2})$';
// Expression for postcodes: AANA NAA
$pcexp[2] = '^('.$alpha1.'{1}'.$alpha2.'[0-9]{1}'.$alpha4.')([0-9]{1}'.$alpha5.'{2})$';
// Exception for the special postcode GIR 0AA
$pcexp[3] = '^(gir)(0aa)$';
// Standard BFPO numbers
$pcexp[4] = '^(bfpo)([0-9]{1,4})$';
// c/o BFPO numbers
$pcexp[5] = '^(bfpo)(c\/o[0-9]{1,3})$';
// Load up the string to check, converting into lowercase and removing spaces
$postcode = strtolower($toCheck);
$postcode = str_replace (' ', '', $postcode);
// Assume we are not going to find a valid postcode
$valid = false;
// Check the string against the six types of postcodes
foreach ($pcexp as $regexp) {
if (ereg($regexp,$postcode, $matches)) {
// Load new postcode back into the form element
$toCheck = strtoupper ($matches[1] . ' ' . $matches [2]);
// Take account of the special BFPO c/o format
$toCheck = ereg_replace ('C\/O', 'c/o ', $toCheck);
// Remember that we have found that the code is valid and break from loop
$valid = true;
break;
}
}
// Return with the reformatted valid postcode in uppercase if the postcode was
// valid
if ($valid){
return $toCheck;
} else {
$this->non_standard_postcode = true;
return $orig;
};
}
}
//If we have a post
if($_GET['postcode']) {
$curl = new curl();
$finder = new postcode_finder(array('curl'=>$curl));
$stores = $finder->get_stores_list($_GET['postcode'],$parents);
}
?>
<form action="" method="GET">
<fieldset>
<div style="float:left; width:370px; font-size:15px; font-weight:bold; margin-right:18px">Search for your nearest free and independent advice centre. Just enter your postcode or the name of your town to get started.</div>
<input type="text" name="postcode" value="<?= $_GET['postcode'] ?>" style="font-size:20px; padding:4px; font-weight:bold; font-family:arial" size="40" maxlength="80">
<input type="submit" value="SEARCH" style="font-size:15px; padding:4px">
</fieldset>
</form>
<? if ($_GET['postcode']) { ?>
<? if ($stores) { ?>
<h2 style="background-color:padding:2px">The following centres were found near to you</h2>
<?
$docids=array();
foreach($stores AS $store) {
$modx->setPlaceholder($store['id'].'.distance',number_format($store['distance'],2));
$docids[]=$store['id'];
}
$params=array();
$params['documents']=implode(',',$docids);
$params['tpl']=$tpl;
//$params['parents']='$parents';you cannot use documents and parents at same time!!!!
$params['display']=$display;
$params['extenders']='customsort,request';
return $modx->runSnippet('Ditto',$params);
} else { ?>
<h2 style="background-color:padding:2px">That postcode was not recognised</h2>
<? } ?>
<? } ?>
</div>
</div>
</body>
</html>
?>