Jump to content

Function will accept string but wont accept vars


shocker-z

Recommended Posts

Morning all,

I'm confused on this one ladiers and gents..

If i call my function with
[code]getdist('postcode', 'postcode');[/code]
oviusly postcode is really a propper postcode, it works fine

but.. if i call the function with
[code]getdist($to, $from);[/code]

it doesn't work.. i've tryed renaming the variables JUST in case somehow they conflicked but with no joy.
I have also echo'ed the variables back into main windows and copied and pasted directly as a string and work so the variables are correct and also the postcodes are located on multimap.

My full code
[code]
<?php
function getdist($to,$from) {
$to=str_replace(' ','',$to);
$from=str_replace(' ','',$from);
//get file content
$multimap=file_get_contents("http://www.multimap.com/map/aproute.cgi?client=public&lang=&rn=GB&input_rt=aproute_pan&startcountry=GB&startrd=&starttown=&startpc=$from&endcountry=GB&endrd=&endtown=&endpc=$to&qs=q&starttime=13%3A52");

//Narrow to sniplet
$place1=strpos($multimap,'<dt>Total distance:</dt>');
$place2=strpos($multimap,'<dt>Number of steps:</dt>');
$distance = substr($multimap, $place1, $place2-$place1);

//get distance
$dist1 = strpos ($distance, '<dd>');
$dist2 = strpos ($distance, '&nbsp;', $dist1);
$totaldist = substr($distance, $dist1+4, $dist2-($dist1+4));
$totaldist=str_replace("\n",'',$totaldist);

//return miles
return $totaldist;
}

include('header.php');

$clientID=mysql_escape_string($_POST['location']);
$getclient=mysql_query("SELECT postcode FROM clients WHERE ID = '$clientID'");
$client=mysql_fetch_array($getclient);
$to=$client['postcode'];

$engineers=mysql_query("SELECT * FROM names");
mysql_query('truncate table tempmiles');
while ($engineer = mysql_fetch_array($engineers)) {

$getclient=mysql_query("SELECT postcode FROM clients WHERE ID = '$engineer[clientID]'");
$client=mysql_fetch_array($getclient);
$from=$client['postcode'];

$tempmiles=getdist($to, $from);

//echo to check that $to and $from are set correctly
echo "Engineer: $engineer[name] Miles: $tempmiles From: $from To: $to <Br>";


mysql_query("INSERT INTO tempmiles (name, miles) VALUES('$engineer[name]','$tempmiles')") or die(mysql_error());
}
?>
[/code]


Regards
Liam
Nope i've tryed that but still not working..


The weird thing is that if i set $to and $from like

$from='NG16 3SB';
$to='NG1 5EJ';
$tempmiles=getdist($to, $from);

which are postcodes directly from the database which I echoed back, then it all works fine :S

Can't understand why as like I say it's a direct copy paste of the postcode!

I even echoed the postcodes back enclosed in [] and it shew that there was a space at the end of the postcode but this still caused no problems when tested as a string



EDIT:

I tryed this

[code] $from=str_replace(' ','',$from);
$to=str_replace(' ','',$to);
$tempmiles=getdist($to, $from);

//echo to check that $to and $from are set correctly
echo "Engineer: $engineer[name] <Br> Miles: $tempmiles <Br>From: [$from] To: [$to] <Br><Br><Br>";
[/code]

and the postcode echoed as From: [NG163SB ] To: [NG15EJ ] so would seem that there is some special char still in the postcode..


ANOTHER EDIT:

[code] $from=mysql_real_escape_string($from);
$to=mysql_real_escape_string($to);[/code]


show's

Engineer: Liam Wheldon
Miles:
From: [NG163SB\r\n] To: [NG15EJ\r\n]

but then the next record show's

From: [NG92LF\r\n] To: [NG15EJ\\r\\n]

and next

From: [NG210RT\r\n] To: [NG15EJ\\\\r\\\\n]


and so on..

any ideas?



Regards
Liam

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.